From f96e802e7418cc52581de58128b31284c56fe1a3 Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Tue, 12 Jul 2022 00:50:32 +0200 Subject: [PATCH] Add `--no-append-metadata` in CLI and `metadata.append` in JSON Skips appending metadata to the binary --- libsolidity/interface/CompilerStack.cpp | 4 ++++ libsolidity/interface/CompilerStack.h | 7 ++++++- libsolidity/interface/StandardCompiler.cpp | 11 ++++++++++- libsolidity/interface/StandardCompiler.h | 1 + solc/CommandLineInterface.cpp | 1 + solc/CommandLineParser.cpp | 22 ++++++++++++++++++++++ solc/CommandLineParser.h | 1 + 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 109650130..41574ce07 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -308,6 +308,7 @@ void CompilerStack::reset(bool _keepSettings) m_revertStrings = RevertStrings::Default; m_optimiserSettings = OptimiserSettings::minimal(); m_metadataLiteralSources = false; + m_metadataFormat = defaultMetadataFormat(); m_metadataHash = MetadataHash::IPFS; m_stopAfter = State::CompilationSuccessful; } @@ -1548,6 +1549,9 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con if (m_revertStrings != RevertStrings::Default) meta["settings"]["debug"]["revertStrings"] = revertStringsToString(m_revertStrings); + if (m_metadataFormat == MetadataFormat::NoMetadata) + meta["settings"]["metadata"]["appendCBOR"] = false; + if (m_metadataLiteralSources) meta["settings"]["metadata"]["useLiteralContent"] = true; diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index e5bc9b345..e267152df 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -349,6 +349,11 @@ public: /// caused by differences in metadata. Should only be used for testing. void setMetadataFormat(MetadataFormat _metadataFormat) { m_metadataFormat = _metadataFormat; } + static MetadataFormat defaultMetadataFormat() + { + return VersionIsRelease ? MetadataFormat::WithReleaseVersionTag : MetadataFormat::WithPrereleaseVersionTag; + } + private: /// The state per source unit. Filled gradually during parsing. struct Source @@ -515,7 +520,7 @@ private: /// Whether or not there has been an error during processing. /// If this is true, the stack will refuse to generate code. bool m_hasError = false; - MetadataFormat m_metadataFormat = VersionIsRelease ? MetadataFormat::WithReleaseVersionTag : MetadataFormat::WithPrereleaseVersionTag; + MetadataFormat m_metadataFormat = defaultMetadataFormat(); }; } diff --git a/libsolidity/interface/StandardCompiler.cpp b/libsolidity/interface/StandardCompiler.cpp index 551bd6af7..15e713d5b 100644 --- a/libsolidity/interface/StandardCompiler.cpp +++ b/libsolidity/interface/StandardCompiler.cpp @@ -509,6 +509,8 @@ std::optional checkMetadataKeys(Json::Value const& _input) { if (_input.isObject()) { + if (_input.isMember("appendCBOR") && !_input["appendCBOR"].isBool()) + return formatFatalError(Error::Type::JSONError, "\"settings.metadata.appendCBOR\" must be Boolean"); if (_input.isMember("useLiteralContent") && !_input["useLiteralContent"].isBool()) return formatFatalError(Error::Type::JSONError, "\"settings.metadata.useLiteralContent\" must be Boolean"); @@ -516,7 +518,7 @@ std::optional checkMetadataKeys(Json::Value const& _input) if (_input.isMember("bytecodeHash") && !hashes.count(_input["bytecodeHash"].asString())) return formatFatalError(Error::Type::JSONError, "\"settings.metadata.bytecodeHash\" must be \"ipfs\", \"bzzr1\" or \"none\""); } - static set keys{"useLiteralContent", "bytecodeHash"}; + static set keys{"appendCBOR", "useLiteralContent", "bytecodeHash"}; return checkKeys(_input, keys, "settings.metadata"); } @@ -911,6 +913,12 @@ std::variant StandardCompiler: if (auto result = checkMetadataKeys(metadataSettings)) return *result; + solAssert(CompilerStack::defaultMetadataFormat() != CompilerStack::MetadataFormat::NoMetadata, ""); + ret.metadataFormat = + metadataSettings.get("appendCBOR", Json::Value(true)).asBool() ? + CompilerStack::defaultMetadataFormat() : + CompilerStack::MetadataFormat::NoMetadata; + ret.metadataLiteralSources = metadataSettings.get("useLiteralContent", Json::Value(false)).asBool(); if (metadataSettings.isMember("bytecodeHash")) { @@ -1086,6 +1094,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting compilerStack.selectDebugInfo(_inputsAndSettings.debugInfoSelection.value()); compilerStack.setLibraries(_inputsAndSettings.libraries); compilerStack.useMetadataLiteralSources(_inputsAndSettings.metadataLiteralSources); + compilerStack.setMetadataFormat(_inputsAndSettings.metadataFormat); compilerStack.setMetadataHash(_inputsAndSettings.metadataHash); compilerStack.setRequestedContractNames(requestedContractNames(_inputsAndSettings.outputSelection)); compilerStack.setModelCheckerSettings(_inputsAndSettings.modelCheckerSettings); diff --git a/libsolidity/interface/StandardCompiler.h b/libsolidity/interface/StandardCompiler.h index 679e731a2..3e94f0231 100644 --- a/libsolidity/interface/StandardCompiler.h +++ b/libsolidity/interface/StandardCompiler.h @@ -83,6 +83,7 @@ private: std::optional debugInfoSelection; std::map libraries; bool metadataLiteralSources = false; + CompilerStack::MetadataFormat metadataFormat = CompilerStack::defaultMetadataFormat(); CompilerStack::MetadataHash metadataHash = CompilerStack::MetadataHash::IPFS; Json::Value outputSelection; ModelCheckerSettings modelCheckerSettings = ModelCheckerSettings{}; diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index ccaa8df94..4bc8d5bb4 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -689,6 +689,7 @@ void CommandLineInterface::compile() { if (m_options.metadata.literalSources) m_compiler->useMetadataLiteralSources(true); + m_compiler->setMetadataFormat(m_options.metadata.format); m_compiler->setMetadataHash(m_options.metadata.hash); if (m_options.modelChecker.initialize) m_compiler->setModelCheckerSettings(m_options.modelChecker.settings); diff --git a/solc/CommandLineParser.cpp b/solc/CommandLineParser.cpp index c6ddfec67..a2e4a0d66 100644 --- a/solc/CommandLineParser.cpp +++ b/solc/CommandLineParser.cpp @@ -62,6 +62,7 @@ static string const g_strLibraries = "libraries"; static string const g_strLink = "link"; static string const g_strLSP = "lsp"; static string const g_strMachine = "machine"; +static string const g_strNoCBORMetadata = "no-cbor-metadata"; static string const g_strMetadataHash = "metadata-hash"; static string const g_strMetadataLiteral = "metadata-literal"; static string const g_strModelCheckerContracts = "model-checker-contracts"; @@ -240,6 +241,7 @@ bool CommandLineOptions::operator==(CommandLineOptions const& _other) const noex compiler.outputs == _other.compiler.outputs && compiler.estimateGas == _other.compiler.estimateGas && compiler.combinedJsonRequests == _other.compiler.combinedJsonRequests && + metadata.format == _other.metadata.format && metadata.hash == _other.metadata.hash && metadata.literalSources == _other.metadata.literalSources && optimizer.enabled == _other.optimizer.enabled && @@ -748,6 +750,10 @@ General Information)").c_str(), po::options_description metadataOptions("Metadata Options"); metadataOptions.add_options() + ( + g_strNoCBORMetadata.c_str(), + "Do not append CBOR metadata to the end of the bytecode." + ) ( g_strMetadataHash.c_str(), po::value()->value_name(util::joinHumanReadable(g_metadataHashArgs, ",")), @@ -922,6 +928,7 @@ void CommandLineParser::processArgs() {g_strExperimentalViaIR, {InputMode::Compiler, InputMode::CompilerWithASTImport}}, {g_strViaIR, {InputMode::Compiler, InputMode::CompilerWithASTImport}}, {g_strMetadataLiteral, {InputMode::Compiler, InputMode::CompilerWithASTImport}}, + {g_strNoCBORMetadata, {InputMode::Compiler, InputMode::CompilerWithASTImport}}, {g_strMetadataHash, {InputMode::Compiler, InputMode::CompilerWithASTImport}}, {g_strModelCheckerShowUnproved, {InputMode::Compiler, InputMode::CompilerWithASTImport}}, {g_strModelCheckerDivModNoSlacks, {InputMode::Compiler, InputMode::CompilerWithASTImport}}, @@ -1221,6 +1228,21 @@ void CommandLineParser::processArgs() solThrow(CommandLineValidationError, "Invalid option for --" + g_strMetadataHash + ": " + hashStr); } + if (m_args.count(g_strNoCBORMetadata)) + { + if ( + m_args.count(g_strMetadataHash) && + m_options.metadata.hash != CompilerStack::MetadataHash::None + ) + solThrow( + CommandLineValidationError, + "Cannot specify a metadata hashing method when --" + + g_strNoCBORMetadata + " is set." + ); + + m_options.metadata.format = CompilerStack::MetadataFormat::NoMetadata; + } + if (m_args.count(g_strModelCheckerContracts)) { string contractsStr = m_args[g_strModelCheckerContracts].as(); diff --git a/solc/CommandLineParser.h b/solc/CommandLineParser.h index 108a16cd2..d695a6eb6 100644 --- a/solc/CommandLineParser.h +++ b/solc/CommandLineParser.h @@ -215,6 +215,7 @@ struct CommandLineOptions struct { + CompilerStack::MetadataFormat format = CompilerStack::defaultMetadataFormat(); CompilerStack::MetadataHash hash = CompilerStack::MetadataHash::IPFS; bool literalSources = false; } metadata;