From d32661c0afa181a587734b99e7dae49d0dc29ab1 Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Mon, 25 Jul 2022 00:22:56 +0200 Subject: [PATCH] Added tests for --no-append-metadata - Command line tests - Documented the standard json - Changelog - A Change in SolidityExecutionFramework to prevent flaky gas tests - A boost test for --no-append-metadata - Removed an outdated comment --- Changelog.md | 2 ++ docs/metadata.rst | 4 +++ docs/using-the-compiler.rst | 3 ++ libsolidity/interface/CompilerStack.h | 2 -- .../metadata_append_standard_json/args | 1 + .../metadata_append_standard_json/input.json | 28 +++++++++++++++++ .../metadata_append_standard_json/output.json | 30 +++++++++++++++++++ .../no_append_incorrect_combination/args | 1 + .../no_append_incorrect_combination/err | 1 + .../no_append_incorrect_combination/exit | 1 + .../no_append_incorrect_combination/input.sol | 10 +++++++ test/cmdlineTests/no_append_metadata/args | 1 + .../cmdlineTests/no_append_metadata/input.sol | 10 +++++++ test/cmdlineTests/no_append_metadata/output | 4 +++ test/libsolidity/GasCosts.cpp | 1 + test/libsolidity/SemanticTest.cpp | 2 +- .../SolidityExecutionFramework.cpp | 3 ++ test/libsolidity/SolidityExecutionFramework.h | 11 +++++-- test/solc/CommandLineParser.cpp | 9 ++++++ 19 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 test/cmdlineTests/metadata_append_standard_json/args create mode 100644 test/cmdlineTests/metadata_append_standard_json/input.json create mode 100644 test/cmdlineTests/metadata_append_standard_json/output.json create mode 100644 test/cmdlineTests/no_append_incorrect_combination/args create mode 100644 test/cmdlineTests/no_append_incorrect_combination/err create mode 100644 test/cmdlineTests/no_append_incorrect_combination/exit create mode 100644 test/cmdlineTests/no_append_incorrect_combination/input.sol create mode 100644 test/cmdlineTests/no_append_metadata/args create mode 100644 test/cmdlineTests/no_append_metadata/input.sol create mode 100644 test/cmdlineTests/no_append_metadata/output diff --git a/Changelog.md b/Changelog.md index 84aa53023..98b36aab8 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,8 @@ Language Features: Compiler Features: +* Commandline Interface: Add `--no-cbor-metadata` that skips CBOR metadata from getting appended at the end of the bytecode. +* Standard JSON: Add a boolean field `settings.metadata.appendCBOR` that skips CBOR metadata from getting appended at the end of the bytecode. * Yul Optimizer: Allow replacing the previously hard-coded cleanup sequence by specifying custom steps after a colon delimiter (``:``) in the sequence string. diff --git a/docs/metadata.rst b/docs/metadata.rst index 317ad2204..f2fffc0dd 100644 --- a/docs/metadata.rst +++ b/docs/metadata.rst @@ -220,6 +220,10 @@ Whereas release builds of solc use a 3 byte encoding of the version as shown above (one byte each for major, minor and patch version number), prerelease builds will instead use a complete version string including commit hash and build date. +The commandline flag ``--no-cbor-metadata`` can be used to skip metadata +from getting appended at the end of the deployed bytecode. Equivalently, the +boolean field ``settings.metadata.appendCBOR`` in Standard JSON input can be set to false. + .. note:: The CBOR mapping can also contain other keys, so it is better to fully decode the data instead of relying on it starting with ``0xa264``. diff --git a/docs/using-the-compiler.rst b/docs/using-the-compiler.rst index d6bf94a2c..cf5e1b768 100644 --- a/docs/using-the-compiler.rst +++ b/docs/using-the-compiler.rst @@ -323,6 +323,9 @@ Input Description }, // Metadata settings (optional) "metadata": { + // The CBOR metadata is appended at the end of the bytecode by default. + // Setting this to false omits the metadata from the runtime and deploy time code. + "appendCBOR": true, // Use only literal content and not URLs (false by default) "useLiteralContent": true, // Use the given hash method for the metadata hash that is appended to the bytecode. diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index e267152df..2f9524659 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -345,8 +345,6 @@ public: Json::Value gasEstimates(std::string const& _contractName) const; /// Changes the format of the metadata appended at the end of the bytecode. - /// This is mostly a workaround to avoid bytecode and gas differences between compiler builds - /// caused by differences in metadata. Should only be used for testing. void setMetadataFormat(MetadataFormat _metadataFormat) { m_metadataFormat = _metadataFormat; } static MetadataFormat defaultMetadataFormat() diff --git a/test/cmdlineTests/metadata_append_standard_json/args b/test/cmdlineTests/metadata_append_standard_json/args new file mode 100644 index 000000000..a905f1fe6 --- /dev/null +++ b/test/cmdlineTests/metadata_append_standard_json/args @@ -0,0 +1 @@ +--pretty-json --json-indent 4 diff --git a/test/cmdlineTests/metadata_append_standard_json/input.json b/test/cmdlineTests/metadata_append_standard_json/input.json new file mode 100644 index 000000000..493325506 --- /dev/null +++ b/test/cmdlineTests/metadata_append_standard_json/input.json @@ -0,0 +1,28 @@ +{ + "language": "Solidity", + "sources": + { + "A": + { + "content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity >=0.0;\n\ncontract test {}" + } + }, + "settings": + { + "viaIR": true, + "optimizer": { + "enabled": true + }, + "metadata": + { + "appendCBOR": false + }, + "outputSelection": + { + "A": + { + "test": ["evm.bytecode"] + } + } + } +} diff --git a/test/cmdlineTests/metadata_append_standard_json/output.json b/test/cmdlineTests/metadata_append_standard_json/output.json new file mode 100644 index 000000000..990217c9e --- /dev/null +++ b/test/cmdlineTests/metadata_append_standard_json/output.json @@ -0,0 +1,30 @@ +{ + "contracts": + { + "A": + { + "test": + { + "evm": + { + "bytecode": + { + "functionDebugData": {}, + "generatedSources": [], + "linkReferences": {}, + "object": "608080604052346013576004908160198239f35b600080fdfe600080fd", + "opcodes": "PUSH1 0x80 DUP1 PUSH1 0x40 MSTORE CALLVALUE PUSH1 0x13 JUMPI PUSH1 0x4 SWAP1 DUP2 PUSH1 0x19 DUP3 CODECOPY RETURN JUMPDEST PUSH1 0x0 DUP1 REVERT INVALID PUSH1 0x0 DUP1 REVERT ", + "sourceMap": "60:16:0:-:0;;;;;;;;;;;;;;;;;" + } + } + } + } + }, + "sources": + { + "A": + { + "id": 0 + } + } +} diff --git a/test/cmdlineTests/no_append_incorrect_combination/args b/test/cmdlineTests/no_append_incorrect_combination/args new file mode 100644 index 000000000..eb524931d --- /dev/null +++ b/test/cmdlineTests/no_append_incorrect_combination/args @@ -0,0 +1 @@ +--no-cbor-metadata --metadata-hash ipfs diff --git a/test/cmdlineTests/no_append_incorrect_combination/err b/test/cmdlineTests/no_append_incorrect_combination/err new file mode 100644 index 000000000..e1b8c7d35 --- /dev/null +++ b/test/cmdlineTests/no_append_incorrect_combination/err @@ -0,0 +1 @@ +Cannot specify a metadata hashing method when --no-cbor-metadata is set. diff --git a/test/cmdlineTests/no_append_incorrect_combination/exit b/test/cmdlineTests/no_append_incorrect_combination/exit new file mode 100644 index 000000000..d00491fd7 --- /dev/null +++ b/test/cmdlineTests/no_append_incorrect_combination/exit @@ -0,0 +1 @@ +1 diff --git a/test/cmdlineTests/no_append_incorrect_combination/input.sol b/test/cmdlineTests/no_append_incorrect_combination/input.sol new file mode 100644 index 000000000..f22a20f18 --- /dev/null +++ b/test/cmdlineTests/no_append_incorrect_combination/input.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >= 0.0.0; + +contract Empty {} + +contract C { + function f() external returns (bytes memory, bytes memory){ + return (type(Empty).creationCode, type(Empty).runtimeCode); + } +} diff --git a/test/cmdlineTests/no_append_metadata/args b/test/cmdlineTests/no_append_metadata/args new file mode 100644 index 000000000..428810db5 --- /dev/null +++ b/test/cmdlineTests/no_append_metadata/args @@ -0,0 +1 @@ +--no-cbor-metadata --bin --via-ir --optimize diff --git a/test/cmdlineTests/no_append_metadata/input.sol b/test/cmdlineTests/no_append_metadata/input.sol new file mode 100644 index 000000000..4ed3a2455 --- /dev/null +++ b/test/cmdlineTests/no_append_metadata/input.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >= 0.0.0; + +// NOTE: The expected output would be the binary with no metadata. +// The commandline tests would replace bytecode with metadata in it with +// +// Therefore, not having that means success! + +contract C { +} diff --git a/test/cmdlineTests/no_append_metadata/output b/test/cmdlineTests/no_append_metadata/output new file mode 100644 index 000000000..8691c5fae --- /dev/null +++ b/test/cmdlineTests/no_append_metadata/output @@ -0,0 +1,4 @@ + +======= no_append_metadata/input.sol:C ======= +Binary: +608080604052346013576004908160198239f35b600080fdfe600080fd diff --git a/test/libsolidity/GasCosts.cpp b/test/libsolidity/GasCosts.cpp index b3fb9d8b7..1d67281cc 100644 --- a/test/libsolidity/GasCosts.cpp +++ b/test/libsolidity/GasCosts.cpp @@ -92,6 +92,7 @@ BOOST_AUTO_TEST_CASE(string_storage) } )"; m_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata); + m_appendCBORMetadata = false; compileAndRun(sourceCode); auto evmVersion = solidity::test::CommonOptions::get().evmVersion(); diff --git a/test/libsolidity/SemanticTest.cpp b/test/libsolidity/SemanticTest.cpp index 9cf2b8d52..0749f5714 100644 --- a/test/libsolidity/SemanticTest.cpp +++ b/test/libsolidity/SemanticTest.cpp @@ -53,7 +53,7 @@ SemanticTest::SemanticTest( bool _enforceGasCost, u256 _enforceGasCostMinValue ): - SolidityExecutionFramework(_evmVersion, _vmPaths), + SolidityExecutionFramework(_evmVersion, _vmPaths, false), EVMVersionRestrictedTestCase(_filename), m_sources(m_reader.sources()), m_lineOffset(m_reader.lineNumber()), diff --git a/test/libsolidity/SolidityExecutionFramework.cpp b/test/libsolidity/SolidityExecutionFramework.cpp index cb47cf45e..0450f290a 100644 --- a/test/libsolidity/SolidityExecutionFramework.cpp +++ b/test/libsolidity/SolidityExecutionFramework.cpp @@ -62,6 +62,9 @@ bytes SolidityExecutionFramework::multiSourceCompileContract( m_compiler.enableEvmBytecodeGeneration(!m_compileViaYul); m_compiler.enableIRGeneration(m_compileViaYul); m_compiler.setRevertStringBehaviour(m_revertStrings); + if (!m_appendCBORMetadata) { + m_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata); + } m_compiler.setMetadataHash(m_metadataHash); if (!m_compiler.compile()) { diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index dfa495152..a273f2ad1 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -40,8 +40,14 @@ class SolidityExecutionFramework: public solidity::test::ExecutionFramework public: SolidityExecutionFramework(): m_showMetadata(solidity::test::CommonOptions::get().showMetadata) {} - explicit SolidityExecutionFramework(langutil::EVMVersion _evmVersion, std::vector const& _vmPaths): - ExecutionFramework(_evmVersion, _vmPaths), m_showMetadata(solidity::test::CommonOptions::get().showMetadata) + explicit SolidityExecutionFramework( + langutil::EVMVersion _evmVersion, + std::vector const& _vmPaths, + bool _appendCBORMetadata = true + ): + ExecutionFramework(_evmVersion, _vmPaths), + m_showMetadata(solidity::test::CommonOptions::get().showMetadata), + m_appendCBORMetadata(_appendCBORMetadata) {} bytes const& compileAndRunWithoutCheck( @@ -80,6 +86,7 @@ protected: bool m_compileViaYul = false; bool m_compileToEwasm = false; bool m_showMetadata = false; + bool m_appendCBORMetadata = true; CompilerStack::MetadataHash m_metadataHash = CompilerStack::MetadataHash::IPFS; RevertStrings m_revertStrings = RevertStrings::Default; }; diff --git a/test/solc/CommandLineParser.cpp b/test/solc/CommandLineParser.cpp index 544021886..61e99f042 100644 --- a/test/solc/CommandLineParser.cpp +++ b/test/solc/CommandLineParser.cpp @@ -224,6 +224,15 @@ BOOST_AUTO_TEST_CASE(cli_mode_options) } } +BOOST_AUTO_TEST_CASE(no_cbor_metadata) +{ + vector commandLine = {"solc", "--no-cbor-metadata", "contract.sol"}; + CommandLineOptions parsedOptions = parseCommandLine(commandLine); + bool assert = parsedOptions.metadata.format == CompilerStack::MetadataFormat::NoMetadata; + + BOOST_TEST(assert); +} + BOOST_AUTO_TEST_CASE(via_ir_options) { BOOST_TEST(!parseCommandLine({"solc", "contract.sol"}).output.viaIR);