From 68a4efb2e738118088065187adb7ca315941240a Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Thu, 11 Feb 2021 09:45:28 +0100 Subject: [PATCH] Refactor overwriteRelease flag. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kamil ƚliwak --- libsolidity/interface/CompilerStack.cpp | 11 ++++++++++- libsolidity/interface/CompilerStack.h | 14 +++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index d518c50d3..1995c7750 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -1580,6 +1580,9 @@ private: bytes CompilerStack::createCBORMetadata(Contract const& _contract) const { + if (m_metadataFormat == MetadataFormat::NoMetadata) + return bytes{}; + bool const experimentalMode = !onlySafeExperimentalFeaturesActivated( _contract.contract->sourceUnit().annotation().experimentalFeatures ); @@ -1597,10 +1600,16 @@ bytes CompilerStack::createCBORMetadata(Contract const& _contract) const if (experimentalMode || m_viaIR) encoder.pushBool("experimental", true); - if (m_release) + if (m_metadataFormat == MetadataFormat::WithReleaseVersionTag) encoder.pushBytes("solc", VersionCompactBytes); else + { + solAssert( + m_metadataFormat == MetadataFormat::WithPrereleaseVersionTag, + "Invalid metadata format." + ); encoder.pushString("solc", VersionStringStrict); + } return encoder.serialise(); } diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index 5049561c4..db087a663 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -98,6 +98,12 @@ public: CompilationSuccessful }; + enum class MetadataFormat { + WithReleaseVersionTag, + WithPrereleaseVersionTag, + NoMetadata + }; + enum class MetadataHash { IPFS, Bzzr1, @@ -336,8 +342,10 @@ public: /// @returns a JSON representing the estimated gas usage for contract creation, internal and external functions Json::Value gasEstimates(std::string const& _contractName) const; - /// Overwrites the release/prerelease flag. Should only be used for testing. - void overwriteReleaseFlag(bool release) { m_release = release; } + /// 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; } private: /// The state per source unit. Filled gradually during parsing. struct Source @@ -496,7 +504,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; - bool m_release = VersionIsRelease; + MetadataFormat m_metadataFormat = VersionIsRelease ? MetadataFormat::WithReleaseVersionTag : MetadataFormat::WithPrereleaseVersionTag; }; }