Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libsolidity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions libsolidity/interface/Common.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once

#include <liblangutil/Exceptions.h>

#include <array>

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<std::string, 3> 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<MetadataHash>(std::ranges::distance(METADATA_HASH_NAMES.begin(), it));
}

inline constexpr std::string metadataHashToString(MetadataHash const& _metadataHash)
{
return METADATA_HASH_NAMES[static_cast<std::size_t>(_metadataHash)];
}

}




4 changes: 1 addition & 3 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
* @date 2014
* Full-stack compiler that converts a source code string to bytecode.
*/


#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/ImportRemapper.h>

Expand Down Expand Up @@ -1833,7 +1831,7 @@ std::string CompilerStack::createMetadata(Contract const& _contract, bool _forIR
meta["settings"]["metadata"]["useLiteralContent"] = true;

static std::vector<std::string> 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;
Expand Down
7 changes: 1 addition & 6 deletions libsolidity/interface/CompilerStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma once

#include <libsolidity/analysis/FunctionCallGraph.h>
#include <libsolidity/interface/Common.h>
#include <libsolidity/interface/ReadFile.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/interface/OptimiserSettings.h>
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions libsolidity/interface/StandardCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* @date 2016
* Standard JSON compiler interface.
*/

#include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/ImportRemapper.h>

Expand Down Expand Up @@ -1022,13 +1021,8 @@ std::variant<StandardCompiler::InputsAndSettings, Json> StandardCompiler::parseI
if (metadataSettings.contains("bytecodeHash"))
{
auto metadataHash = metadataSettings["bytecodeHash"].get<std::string>();
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 \"" +
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/interface/StandardCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class StandardCompiler
std::map<std::string, util::h160> 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;
Expand Down
8 changes: 4 additions & 4 deletions solc/CommandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,11 +1413,11 @@ void CommandLineParser::processArgs()
{
std::string hashStr = m_args[g_strMetadataHash].as<std::string>();
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);
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion solc/CommandLineParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
24 changes: 12 additions & 12 deletions test/libsolidity/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
CompilerStack::MetadataFormat::WithReleaseVersionTag,
CompilerStack::MetadataFormat::WithPrereleaseVersionTag
})
for (auto metadataHash: std::set<CompilerStack::MetadataHash>{
CompilerStack::MetadataHash::IPFS,
CompilerStack::MetadataHash::Bzzr1,
CompilerStack::MetadataHash::None
for (auto metadataHash: std::set<MetadataHash>{
MetadataHash::IPFS,
MetadataHash::Bzzr1,
MetadataHash::None
})
{
CompilerStack compilerStack;
Expand All @@ -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);
Expand Down Expand Up @@ -166,10 +166,10 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
CompilerStack::MetadataFormat::WithReleaseVersionTag,
CompilerStack::MetadataFormat::WithPrereleaseVersionTag
})
for (auto metadataHash: std::set<CompilerStack::MetadataHash>{
CompilerStack::MetadataHash::IPFS,
CompilerStack::MetadataHash::Bzzr1,
CompilerStack::MetadataHash::None
for (auto metadataHash: std::set<MetadataHash>{
MetadataHash::IPFS,
MetadataHash::Bzzr1,
MetadataHash::None
})
{
CompilerStack compilerStack;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/SemanticTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/SolidityEndToEndTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/SolidityExecutionFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/SyntaxTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/solc/CommandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down