From 7d0ce74ffebf3507dab2c6f3b51829d790f20fdf Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Fri, 13 Mar 2026 22:46:53 +0100 Subject: [PATCH] Make `MetadataHash` common --- libsolidity/CMakeLists.txt | 1 + libsolidity/interface/Common.h | 49 +++++++++++++++++++ libsolidity/interface/CompilerStack.cpp | 4 +- libsolidity/interface/CompilerStack.h | 7 +-- libsolidity/interface/StandardCompiler.cpp | 10 +--- libsolidity/interface/StandardCompiler.h | 2 +- solc/CommandLineParser.cpp | 8 +-- solc/CommandLineParser.h | 2 +- test/libsolidity/Metadata.cpp | 24 ++++----- test/libsolidity/SemanticTest.cpp | 2 +- test/libsolidity/SolidityEndToEndTest.cpp | 2 +- test/libsolidity/SolidityExecutionFramework.h | 2 +- test/libsolidity/SyntaxTest.cpp | 2 +- test/solc/CommandLineParser.cpp | 2 +- 14 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 libsolidity/interface/Common.h diff --git a/libsolidity/CMakeLists.txt b/libsolidity/CMakeLists.txt index 62f0576dc59f..03dfd1e9e90f 100644 --- a/libsolidity/CMakeLists.txt +++ b/libsolidity/CMakeLists.txt @@ -143,6 +143,7 @@ set(sources formal/Z3SMTLib2Interface.h interface/ABI.cpp interface/ABI.h + interface/Common.h interface/CompilerStack.cpp interface/CompilerStack.h interface/DebugSettings.h diff --git a/libsolidity/interface/Common.h b/libsolidity/interface/Common.h new file mode 100644 index 000000000000..286cc8bfe70c --- /dev/null +++ b/libsolidity/interface/Common.h @@ -0,0 +1,49 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +// SPDX-License-Identifier: GPL-3.0 +#pragma once + +#include + +#include + +namespace solidity::frontend +{ + +/// Use the given hash method for the metadata hash that is appended to the bytecode. +enum class MetadataHash { IPFS, Bzzr1, None }; + +/// Metadata hash names used for conversion from and to std::string. +static constexpr std::array METADATA_HASH_NAMES = { "ipfs", "bzzr1", "none" }; + +inline constexpr MetadataHash metadataHashFromString(std::string const& _metadataHash) +{ + auto it = std::ranges::find(METADATA_HASH_NAMES, _metadataHash); + solAssert(it != METADATA_HASH_NAMES.end(), "Unknown metadata hash: \"" + _metadataHash + "\""); + return static_cast(std::ranges::distance(METADATA_HASH_NAMES.begin(), it)); +} + +inline constexpr std::string metadataHashToString(MetadataHash const& _metadataHash) +{ + return METADATA_HASH_NAMES[static_cast(_metadataHash)]; +} + +} + + + + diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 152a0059adf6..294f9d21c399 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -21,8 +21,6 @@ * @date 2014 * Full-stack compiler that converts a source code string to bytecode. */ - - #include #include @@ -1833,7 +1831,7 @@ std::string CompilerStack::createMetadata(Contract const& _contract, bool _forIR meta["settings"]["metadata"]["useLiteralContent"] = true; static std::vector hashes{"ipfs", "bzzr1", "none"}; - meta["settings"]["metadata"]["bytecodeHash"] = hashes.at(unsigned(m_metadataHash)); + meta["settings"]["metadata"]["bytecodeHash"] = metadataHashToString(m_metadataHash); if (_forIR) meta["settings"]["viaIR"] = _forIR; diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index 41f61ae4a2b2..94b2ab7602f9 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -25,6 +25,7 @@ #pragma once #include +#include #include #include #include @@ -120,12 +121,6 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac NoMetadata }; - enum class MetadataHash { - IPFS, - Bzzr1, - None - }; - enum class CompilationSourceType { /// Regular compilation from Solidity source files. Solidity, diff --git a/libsolidity/interface/StandardCompiler.cpp b/libsolidity/interface/StandardCompiler.cpp index a556c74db1b1..86e442780948 100644 --- a/libsolidity/interface/StandardCompiler.cpp +++ b/libsolidity/interface/StandardCompiler.cpp @@ -20,7 +20,6 @@ * @date 2016 * Standard JSON compiler interface. */ - #include #include @@ -1022,13 +1021,8 @@ std::variant StandardCompiler::parseI if (metadataSettings.contains("bytecodeHash")) { auto metadataHash = metadataSettings["bytecodeHash"].get(); - ret.metadataHash = - metadataHash == "ipfs" ? - CompilerStack::MetadataHash::IPFS : - metadataHash == "bzzr1" ? - CompilerStack::MetadataHash::Bzzr1 : - CompilerStack::MetadataHash::None; - if (ret.metadataFormat == CompilerStack::MetadataFormat::NoMetadata && ret.metadataHash != CompilerStack::MetadataHash::None) + ret.metadataHash = metadataHashFromString(metadataHash); + if (ret.metadataFormat == CompilerStack::MetadataFormat::NoMetadata && ret.metadataHash != MetadataHash::None) return formatFatalError( Error::Type::JSONError, "When the parameter \"appendCBOR\" is set to false, the parameter \"bytecodeHash\" cannot be set to \"" + diff --git a/libsolidity/interface/StandardCompiler.h b/libsolidity/interface/StandardCompiler.h index 0d184e139ec6..aa5f2f6998bb 100644 --- a/libsolidity/interface/StandardCompiler.h +++ b/libsolidity/interface/StandardCompiler.h @@ -85,7 +85,7 @@ class StandardCompiler std::map libraries; bool metadataLiteralSources = false; CompilerStack::MetadataFormat metadataFormat = CompilerStack::defaultMetadataFormat(); - CompilerStack::MetadataHash metadataHash = CompilerStack::MetadataHash::IPFS; + MetadataHash metadataHash = MetadataHash::IPFS; Json outputSelection; ModelCheckerSettings modelCheckerSettings = ModelCheckerSettings{}; bool viaIR = false; diff --git a/solc/CommandLineParser.cpp b/solc/CommandLineParser.cpp index c40840a07567..885c7e6f8c4d 100644 --- a/solc/CommandLineParser.cpp +++ b/solc/CommandLineParser.cpp @@ -1413,11 +1413,11 @@ void CommandLineParser::processArgs() { std::string hashStr = m_args[g_strMetadataHash].as(); if (hashStr == g_strIPFS) - m_options.metadata.hash = CompilerStack::MetadataHash::IPFS; + m_options.metadata.hash = MetadataHash::IPFS; else if (hashStr == g_strSwarm) - m_options.metadata.hash = CompilerStack::MetadataHash::Bzzr1; + m_options.metadata.hash = MetadataHash::Bzzr1; else if (hashStr == g_strNone) - m_options.metadata.hash = CompilerStack::MetadataHash::None; + m_options.metadata.hash = MetadataHash::None; else solThrow(CommandLineValidationError, "Invalid option for --" + g_strMetadataHash + ": " + hashStr); } @@ -1426,7 +1426,7 @@ void CommandLineParser::processArgs() { if ( m_args.count(g_strMetadataHash) && - m_options.metadata.hash != CompilerStack::MetadataHash::None + m_options.metadata.hash != MetadataHash::None ) solThrow( CommandLineValidationError, diff --git a/solc/CommandLineParser.h b/solc/CommandLineParser.h index 5f5527c619f2..e1a915e50839 100644 --- a/solc/CommandLineParser.h +++ b/solc/CommandLineParser.h @@ -250,7 +250,7 @@ struct CommandLineOptions bool operator!=(Metadata const&) const noexcept = default; CompilerStack::MetadataFormat format = CompilerStack::defaultMetadataFormat(); - CompilerStack::MetadataHash hash = CompilerStack::MetadataHash::IPFS; + MetadataHash hash = MetadataHash::IPFS; bool literalSources = false; } metadata; diff --git a/test/libsolidity/Metadata.cpp b/test/libsolidity/Metadata.cpp index 476fcd3a00ab..1f79d50b90c8 100644 --- a/test/libsolidity/Metadata.cpp +++ b/test/libsolidity/Metadata.cpp @@ -92,10 +92,10 @@ BOOST_AUTO_TEST_CASE(metadata_stamp) CompilerStack::MetadataFormat::WithReleaseVersionTag, CompilerStack::MetadataFormat::WithPrereleaseVersionTag }) - for (auto metadataHash: std::set{ - CompilerStack::MetadataHash::IPFS, - CompilerStack::MetadataHash::Bzzr1, - CompilerStack::MetadataHash::None + for (auto metadataHash: std::set{ + MetadataHash::IPFS, + MetadataHash::Bzzr1, + MetadataHash::None }) { CompilerStack compilerStack; @@ -110,13 +110,13 @@ BOOST_AUTO_TEST_CASE(metadata_stamp) BOOST_CHECK(solidity::test::isValidMetadata(metadata)); auto const cborMetadata = requireParsedCBORMetadata(bytecode, metadataFormat); - if (metadataHash == CompilerStack::MetadataHash::None) + if (metadataHash == MetadataHash::None) BOOST_CHECK(cborMetadata.size() == (metadataFormat == CompilerStack::MetadataFormat::NoMetadata ? 0 : 1)); else { bytes hash; std::string hashMethod; - if (metadataHash == CompilerStack::MetadataHash::IPFS) + if (metadataHash == MetadataHash::IPFS) { hash = util::ipfsHash(metadata); BOOST_REQUIRE(hash.size() == 34); @@ -166,10 +166,10 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental) CompilerStack::MetadataFormat::WithReleaseVersionTag, CompilerStack::MetadataFormat::WithPrereleaseVersionTag }) - for (auto metadataHash: std::set{ - CompilerStack::MetadataHash::IPFS, - CompilerStack::MetadataHash::Bzzr1, - CompilerStack::MetadataHash::None + for (auto metadataHash: std::set{ + MetadataHash::IPFS, + MetadataHash::Bzzr1, + MetadataHash::None }) { CompilerStack compilerStack; @@ -185,13 +185,13 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental) BOOST_CHECK(solidity::test::isValidMetadata(metadata)); auto const cborMetadata = requireParsedCBORMetadata(bytecode, metadataFormat); - if (metadataHash == CompilerStack::MetadataHash::None) + if (metadataHash == MetadataHash::None) BOOST_CHECK(cborMetadata.size() == (metadataFormat == CompilerStack::MetadataFormat::NoMetadata ? 0 : 2)); else { bytes hash; std::string hashMethod; - if (metadataHash == CompilerStack::MetadataHash::IPFS) + if (metadataHash == MetadataHash::IPFS) { hash = util::ipfsHash(metadata); BOOST_REQUIRE(hash.size() == 34); diff --git a/test/libsolidity/SemanticTest.cpp b/test/libsolidity/SemanticTest.cpp index d9d44e628911..b335dfc017e8 100644 --- a/test/libsolidity/SemanticTest.cpp +++ b/test/libsolidity/SemanticTest.cpp @@ -121,7 +121,7 @@ SemanticTest::SemanticTest( if (m_enforceGasCost) { m_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata); - m_compiler.setMetadataHash(CompilerStack::MetadataHash::None); + m_compiler.setMetadataHash(MetadataHash::None); } } diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 57422dbb8437..f6e8ccf452b7 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -105,7 +105,7 @@ BOOST_AUTO_TEST_CASE(creation_code_optimizer) } )"; - m_metadataHash = CompilerStack::MetadataHash::None; + m_metadataHash = MetadataHash::None; ALSO_VIA_YUL({ bytes bytecodeC = compileContract(codeC); reset(); diff --git a/test/libsolidity/SolidityExecutionFramework.h b/test/libsolidity/SolidityExecutionFramework.h index 306ac80ade7c..397b78bfc397 100644 --- a/test/libsolidity/SolidityExecutionFramework.h +++ b/test/libsolidity/SolidityExecutionFramework.h @@ -87,7 +87,7 @@ class SolidityExecutionFramework: public solidity::test::ExecutionFramework bool m_compileViaSSACFG = false; bool m_showMetadata = false; bool m_appendCBORMetadata = true; - CompilerStack::MetadataHash m_metadataHash = CompilerStack::MetadataHash::IPFS; + MetadataHash m_metadataHash = MetadataHash::IPFS; RevertStrings m_revertStrings = RevertStrings::Default; }; diff --git a/test/libsolidity/SyntaxTest.cpp b/test/libsolidity/SyntaxTest.cpp index 23b92bcb1d51..9b4bb553a769 100644 --- a/test/libsolidity/SyntaxTest.cpp +++ b/test/libsolidity/SyntaxTest.cpp @@ -84,7 +84,7 @@ void SyntaxTest::setupCompiler(CompilerStack& _compiler) _compiler.setViaIR(m_compileViaYul == "true"); _compiler.setExperimental(m_experimental); _compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata); - _compiler.setMetadataHash(CompilerStack::MetadataHash::None); + _compiler.setMetadataHash(MetadataHash::None); } void SyntaxTest::parseAndAnalyze() diff --git a/test/solc/CommandLineParser.cpp b/test/solc/CommandLineParser.cpp index 1e5896604a41..bf53e55a68a9 100644 --- a/test/solc/CommandLineParser.cpp +++ b/test/solc/CommandLineParser.cpp @@ -207,7 +207,7 @@ BOOST_AUTO_TEST_CASE(cli_mode_options) true, true, true, true, true, true, true, true, }; - expectedOptions.metadata.hash = CompilerStack::MetadataHash::Bzzr1; + expectedOptions.metadata.hash = MetadataHash::Bzzr1; expectedOptions.metadata.literalSources = true; expectedOptions.optimizer.optimizeEvmasm = true; expectedOptions.optimizer.optimizeYul = true;