Rework metadata and cover with tests

This commit is contained in:
Nikola Matic 2022-09-05 09:52:17 +02:00
parent 314a1cc92f
commit feba1bfeff
3 changed files with 53 additions and 5 deletions

View File

@ -1539,8 +1539,7 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con
{ {
details["yulDetails"] = Json::objectValue; details["yulDetails"] = Json::objectValue;
details["yulDetails"]["stackAllocation"] = m_optimiserSettings.optimizeStackAllocation; details["yulDetails"]["stackAllocation"] = m_optimiserSettings.optimizeStackAllocation;
details["yulDetails"]["optimizerSteps"] = m_optimiserSettings.yulOptimiserSteps; details["yulDetails"]["optimizerSteps"] = m_optimiserSettings.yulOptimiserSteps + ":" + m_optimiserSettings.yulOptimiserCleanupSteps;
details["yulDetails"]["optimizerCleanupSteps"] = m_optimiserSettings.yulOptimiserCleanupSteps;
} }
meta["settings"]["optimizer"]["details"] = std::move(details); meta["settings"]["optimizer"]["details"] = std::move(details);

View File

@ -400,6 +400,55 @@ BOOST_AUTO_TEST_CASE(metadata_revert_strings)
BOOST_CHECK_EQUAL(metadata["settings"]["debug"]["revertStrings"], "strip"); BOOST_CHECK_EQUAL(metadata["settings"]["debug"]["revertStrings"], "strip");
} }
BOOST_AUTO_TEST_CASE(metadata_optimiser_sequence)
{
char const* sourceCode = R"(
pragma solidity >=0.0;
contract test {
}
)";
vector<tuple<string, string>> sequences =
{
// { "<optimizer sequence>", "<optimizer cleanup sequence>" }
{ "", "" },
{ "", "fDn" },
{ "dhfoDgvulfnTUtnIf", "" },
{ "dhfoDgvulfnTUtnIf", "fDn" }
};
auto check = [sourceCode](string const& optimizerSequence, string const& optimizerCleanupSequence)
{
OptimiserSettings optimizerSettings = OptimiserSettings::minimal();
optimizerSettings.runYulOptimiser = true;
optimizerSettings.yulOptimiserSteps = optimizerSequence;
optimizerSettings.yulOptimiserCleanupSteps = optimizerCleanupSequence;
CompilerStack compilerStack;
compilerStack.setSources({{"", std::string(sourceCode)}});
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
compilerStack.setOptimiserSettings(optimizerSettings);
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
std::string const& serialisedMetadata = compilerStack.metadata("test");
Json::Value metadata;
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
BOOST_CHECK(metadata["settings"]["optimizer"].isMember("details"));
BOOST_CHECK(metadata["settings"]["optimizer"]["details"].isMember("yulDetails"));
BOOST_CHECK(metadata["settings"]["optimizer"]["details"]["yulDetails"].isMember("optimizerSteps"));
string const metadataOptimizerSteps = metadata["settings"]["optimizer"]["details"]["yulDetails"]["optimizerSteps"].asString();
string const expectedMetadataOptimiserSteps = optimizerSequence + ":" + optimizerCleanupSequence;
BOOST_CHECK_EQUAL(metadataOptimizerSteps, expectedMetadataOptimiserSteps);
};
for (auto const& [sequence, cleanupSequence] : sequences)
{
check(sequence, cleanupSequence);
}
}
BOOST_AUTO_TEST_CASE(metadata_license_missing) BOOST_AUTO_TEST_CASE(metadata_license_missing)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(

View File

@ -1244,11 +1244,11 @@ BOOST_AUTO_TEST_CASE(optimizer_settings_details_different)
BOOST_CHECK(optimizer["details"]["yulDetails"].isObject()); BOOST_CHECK(optimizer["details"]["yulDetails"].isObject());
BOOST_CHECK( BOOST_CHECK(
util::convertContainer<set<string>>(optimizer["details"]["yulDetails"].getMemberNames()) == util::convertContainer<set<string>>(optimizer["details"]["yulDetails"].getMemberNames()) ==
(set<string>{"stackAllocation", "optimizerSteps", "optimizerCleanupSteps"}) (set<string>{"stackAllocation", "optimizerSteps"})
); );
BOOST_CHECK(optimizer["details"]["yulDetails"]["stackAllocation"].asBool() == true); BOOST_CHECK(optimizer["details"]["yulDetails"]["stackAllocation"].asBool() == true);
BOOST_CHECK(optimizer["details"]["yulDetails"]["optimizerSteps"].asString() == OptimiserSettings::DefaultYulOptimiserSteps); BOOST_CHECK(optimizer["details"]["yulDetails"]["optimizerSteps"].asString() ==
BOOST_CHECK(optimizer["details"]["yulDetails"]["optimizerCleanupSteps"].asString() == OptimiserSettings::DefaultYulOptimiserCleanupSteps); string{OptimiserSettings::DefaultYulOptimiserSteps} + ":" + string{OptimiserSettings::DefaultYulOptimiserCleanupSteps});
BOOST_CHECK_EQUAL(optimizer["details"].getMemberNames().size(), 9); BOOST_CHECK_EQUAL(optimizer["details"].getMemberNames().size(), 9);
BOOST_CHECK(optimizer["runs"].asUInt() == 600); BOOST_CHECK(optimizer["runs"].asUInt() == 600);
} }