From feba1bfeffc224eab781cc165091b750553bec28 Mon Sep 17 00:00:00 2001 From: Nikola Matic Date: Mon, 5 Sep 2022 09:52:17 +0200 Subject: [PATCH] Rework metadata and cover with tests --- libsolidity/interface/CompilerStack.cpp | 3 +- test/libsolidity/Metadata.cpp | 49 +++++++++++++++++++++++++ test/libsolidity/StandardCompiler.cpp | 6 +-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 167ef6e28..109650130 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -1539,8 +1539,7 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con { details["yulDetails"] = Json::objectValue; details["yulDetails"]["stackAllocation"] = m_optimiserSettings.optimizeStackAllocation; - details["yulDetails"]["optimizerSteps"] = m_optimiserSettings.yulOptimiserSteps; - details["yulDetails"]["optimizerCleanupSteps"] = m_optimiserSettings.yulOptimiserCleanupSteps; + details["yulDetails"]["optimizerSteps"] = m_optimiserSettings.yulOptimiserSteps + ":" + m_optimiserSettings.yulOptimiserCleanupSteps; } meta["settings"]["optimizer"]["details"] = std::move(details); diff --git a/test/libsolidity/Metadata.cpp b/test/libsolidity/Metadata.cpp index 9cbf73f84..ea13286f1 100644 --- a/test/libsolidity/Metadata.cpp +++ b/test/libsolidity/Metadata.cpp @@ -400,6 +400,55 @@ BOOST_AUTO_TEST_CASE(metadata_revert_strings) 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> sequences = + { + // { "", "" } + { "", "" }, + { "", "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) { char const* sourceCode = R"( diff --git a/test/libsolidity/StandardCompiler.cpp b/test/libsolidity/StandardCompiler.cpp index 9480e7049..c2011e409 100644 --- a/test/libsolidity/StandardCompiler.cpp +++ b/test/libsolidity/StandardCompiler.cpp @@ -1244,11 +1244,11 @@ BOOST_AUTO_TEST_CASE(optimizer_settings_details_different) BOOST_CHECK(optimizer["details"]["yulDetails"].isObject()); BOOST_CHECK( util::convertContainer>(optimizer["details"]["yulDetails"].getMemberNames()) == - (set{"stackAllocation", "optimizerSteps", "optimizerCleanupSteps"}) + (set{"stackAllocation", "optimizerSteps"}) ); BOOST_CHECK(optimizer["details"]["yulDetails"]["stackAllocation"].asBool() == true); - BOOST_CHECK(optimizer["details"]["yulDetails"]["optimizerSteps"].asString() == OptimiserSettings::DefaultYulOptimiserSteps); - BOOST_CHECK(optimizer["details"]["yulDetails"]["optimizerCleanupSteps"].asString() == OptimiserSettings::DefaultYulOptimiserCleanupSteps); + BOOST_CHECK(optimizer["details"]["yulDetails"]["optimizerSteps"].asString() == + string{OptimiserSettings::DefaultYulOptimiserSteps} + ":" + string{OptimiserSettings::DefaultYulOptimiserCleanupSteps}); BOOST_CHECK_EQUAL(optimizer["details"].getMemberNames().size(), 9); BOOST_CHECK(optimizer["runs"].asUInt() == 600); }