Skip to content

Commit f96e802

Browse files
committed
Add --no-append-metadata in CLI and metadata.append in JSON
Skips appending metadata to the binary
1 parent 8b70108 commit f96e802

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

libsolidity/interface/CompilerStack.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ void CompilerStack::reset(bool _keepSettings)
308308
m_revertStrings = RevertStrings::Default;
309309
m_optimiserSettings = OptimiserSettings::minimal();
310310
m_metadataLiteralSources = false;
311+
m_metadataFormat = defaultMetadataFormat();
311312
m_metadataHash = MetadataHash::IPFS;
312313
m_stopAfter = State::CompilationSuccessful;
313314
}
@@ -1548,6 +1549,9 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con
15481549
if (m_revertStrings != RevertStrings::Default)
15491550
meta["settings"]["debug"]["revertStrings"] = revertStringsToString(m_revertStrings);
15501551

1552+
if (m_metadataFormat == MetadataFormat::NoMetadata)
1553+
meta["settings"]["metadata"]["appendCBOR"] = false;
1554+
15511555
if (m_metadataLiteralSources)
15521556
meta["settings"]["metadata"]["useLiteralContent"] = true;
15531557

libsolidity/interface/CompilerStack.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ class CompilerStack: public langutil::CharStreamProvider
349349
/// caused by differences in metadata. Should only be used for testing.
350350
void setMetadataFormat(MetadataFormat _metadataFormat) { m_metadataFormat = _metadataFormat; }
351351

352+
static MetadataFormat defaultMetadataFormat()
353+
{
354+
return VersionIsRelease ? MetadataFormat::WithReleaseVersionTag : MetadataFormat::WithPrereleaseVersionTag;
355+
}
356+
352357
private:
353358
/// The state per source unit. Filled gradually during parsing.
354359
struct Source
@@ -515,7 +520,7 @@ class CompilerStack: public langutil::CharStreamProvider
515520
/// Whether or not there has been an error during processing.
516521
/// If this is true, the stack will refuse to generate code.
517522
bool m_hasError = false;
518-
MetadataFormat m_metadataFormat = VersionIsRelease ? MetadataFormat::WithReleaseVersionTag : MetadataFormat::WithPrereleaseVersionTag;
523+
MetadataFormat m_metadataFormat = defaultMetadataFormat();
519524
};
520525

521526
}

libsolidity/interface/StandardCompiler.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,16 @@ std::optional<Json::Value> checkMetadataKeys(Json::Value const& _input)
509509
{
510510
if (_input.isObject())
511511
{
512+
if (_input.isMember("appendCBOR") && !_input["appendCBOR"].isBool())
513+
return formatFatalError(Error::Type::JSONError, "\"settings.metadata.appendCBOR\" must be Boolean");
512514
if (_input.isMember("useLiteralContent") && !_input["useLiteralContent"].isBool())
513515
return formatFatalError(Error::Type::JSONError, "\"settings.metadata.useLiteralContent\" must be Boolean");
514516

515517
static set<string> hashes{"ipfs", "bzzr1", "none"};
516518
if (_input.isMember("bytecodeHash") && !hashes.count(_input["bytecodeHash"].asString()))
517519
return formatFatalError(Error::Type::JSONError, "\"settings.metadata.bytecodeHash\" must be \"ipfs\", \"bzzr1\" or \"none\"");
518520
}
519-
static set<string> keys{"useLiteralContent", "bytecodeHash"};
521+
static set<string> keys{"appendCBOR", "useLiteralContent", "bytecodeHash"};
520522
return checkKeys(_input, keys, "settings.metadata");
521523
}
522524

@@ -911,6 +913,12 @@ std::variant<StandardCompiler::InputsAndSettings, Json::Value> StandardCompiler:
911913
if (auto result = checkMetadataKeys(metadataSettings))
912914
return *result;
913915

916+
solAssert(CompilerStack::defaultMetadataFormat() != CompilerStack::MetadataFormat::NoMetadata, "");
917+
ret.metadataFormat =
918+
metadataSettings.get("appendCBOR", Json::Value(true)).asBool() ?
919+
CompilerStack::defaultMetadataFormat() :
920+
CompilerStack::MetadataFormat::NoMetadata;
921+
914922
ret.metadataLiteralSources = metadataSettings.get("useLiteralContent", Json::Value(false)).asBool();
915923
if (metadataSettings.isMember("bytecodeHash"))
916924
{
@@ -1086,6 +1094,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
10861094
compilerStack.selectDebugInfo(_inputsAndSettings.debugInfoSelection.value());
10871095
compilerStack.setLibraries(_inputsAndSettings.libraries);
10881096
compilerStack.useMetadataLiteralSources(_inputsAndSettings.metadataLiteralSources);
1097+
compilerStack.setMetadataFormat(_inputsAndSettings.metadataFormat);
10891098
compilerStack.setMetadataHash(_inputsAndSettings.metadataHash);
10901099
compilerStack.setRequestedContractNames(requestedContractNames(_inputsAndSettings.outputSelection));
10911100
compilerStack.setModelCheckerSettings(_inputsAndSettings.modelCheckerSettings);

libsolidity/interface/StandardCompiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class StandardCompiler
8383
std::optional<langutil::DebugInfoSelection> debugInfoSelection;
8484
std::map<std::string, util::h160> libraries;
8585
bool metadataLiteralSources = false;
86+
CompilerStack::MetadataFormat metadataFormat = CompilerStack::defaultMetadataFormat();
8687
CompilerStack::MetadataHash metadataHash = CompilerStack::MetadataHash::IPFS;
8788
Json::Value outputSelection;
8889
ModelCheckerSettings modelCheckerSettings = ModelCheckerSettings{};

solc/CommandLineInterface.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ void CommandLineInterface::compile()
689689
{
690690
if (m_options.metadata.literalSources)
691691
m_compiler->useMetadataLiteralSources(true);
692+
m_compiler->setMetadataFormat(m_options.metadata.format);
692693
m_compiler->setMetadataHash(m_options.metadata.hash);
693694
if (m_options.modelChecker.initialize)
694695
m_compiler->setModelCheckerSettings(m_options.modelChecker.settings);

solc/CommandLineParser.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static string const g_strLibraries = "libraries";
6262
static string const g_strLink = "link";
6363
static string const g_strLSP = "lsp";
6464
static string const g_strMachine = "machine";
65+
static string const g_strNoCBORMetadata = "no-cbor-metadata";
6566
static string const g_strMetadataHash = "metadata-hash";
6667
static string const g_strMetadataLiteral = "metadata-literal";
6768
static string const g_strModelCheckerContracts = "model-checker-contracts";
@@ -240,6 +241,7 @@ bool CommandLineOptions::operator==(CommandLineOptions const& _other) const noex
240241
compiler.outputs == _other.compiler.outputs &&
241242
compiler.estimateGas == _other.compiler.estimateGas &&
242243
compiler.combinedJsonRequests == _other.compiler.combinedJsonRequests &&
244+
metadata.format == _other.metadata.format &&
243245
metadata.hash == _other.metadata.hash &&
244246
metadata.literalSources == _other.metadata.literalSources &&
245247
optimizer.enabled == _other.optimizer.enabled &&
@@ -748,6 +750,10 @@ General Information)").c_str(),
748750

749751
po::options_description metadataOptions("Metadata Options");
750752
metadataOptions.add_options()
753+
(
754+
g_strNoCBORMetadata.c_str(),
755+
"Do not append CBOR metadata to the end of the bytecode."
756+
)
751757
(
752758
g_strMetadataHash.c_str(),
753759
po::value<string>()->value_name(util::joinHumanReadable(g_metadataHashArgs, ",")),
@@ -922,6 +928,7 @@ void CommandLineParser::processArgs()
922928
{g_strExperimentalViaIR, {InputMode::Compiler, InputMode::CompilerWithASTImport}},
923929
{g_strViaIR, {InputMode::Compiler, InputMode::CompilerWithASTImport}},
924930
{g_strMetadataLiteral, {InputMode::Compiler, InputMode::CompilerWithASTImport}},
931+
{g_strNoCBORMetadata, {InputMode::Compiler, InputMode::CompilerWithASTImport}},
925932
{g_strMetadataHash, {InputMode::Compiler, InputMode::CompilerWithASTImport}},
926933
{g_strModelCheckerShowUnproved, {InputMode::Compiler, InputMode::CompilerWithASTImport}},
927934
{g_strModelCheckerDivModNoSlacks, {InputMode::Compiler, InputMode::CompilerWithASTImport}},
@@ -1221,6 +1228,21 @@ void CommandLineParser::processArgs()
12211228
solThrow(CommandLineValidationError, "Invalid option for --" + g_strMetadataHash + ": " + hashStr);
12221229
}
12231230

1231+
if (m_args.count(g_strNoCBORMetadata))
1232+
{
1233+
if (
1234+
m_args.count(g_strMetadataHash) &&
1235+
m_options.metadata.hash != CompilerStack::MetadataHash::None
1236+
)
1237+
solThrow(
1238+
CommandLineValidationError,
1239+
"Cannot specify a metadata hashing method when --" +
1240+
g_strNoCBORMetadata + " is set."
1241+
);
1242+
1243+
m_options.metadata.format = CompilerStack::MetadataFormat::NoMetadata;
1244+
}
1245+
12241246
if (m_args.count(g_strModelCheckerContracts))
12251247
{
12261248
string contractsStr = m_args[g_strModelCheckerContracts].as<string>();

solc/CommandLineParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ struct CommandLineOptions
215215

216216
struct
217217
{
218+
CompilerStack::MetadataFormat format = CompilerStack::defaultMetadataFormat();
218219
CompilerStack::MetadataHash hash = CompilerStack::MetadataHash::IPFS;
219220
bool literalSources = false;
220221
} metadata;

0 commit comments

Comments
 (0)