mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Create option for metadata hash
This commit is contained in:
+5
-3
@@ -41,9 +41,9 @@ bytes onlyMetadata(bytes const& _bytecode)
|
||||
size_t metadataSize = (_bytecode[size - 2] << 8) + _bytecode[size - 1];
|
||||
if (size < (metadataSize + 2))
|
||||
return bytes{};
|
||||
// Sanity check: assume the first byte is a fixed-size CBOR array with either 2 or 3 entries
|
||||
// Sanity check: assume the first byte is a fixed-size CBOR array with 1, 2 or 3 entries
|
||||
unsigned char firstByte = _bytecode[size - metadataSize - 2];
|
||||
if (firstByte != 0xa2 && firstByte != 0xa3)
|
||||
if (firstByte != 0xa1 && firstByte != 0xa2 && firstByte != 0xa3)
|
||||
return bytes{};
|
||||
return bytes(_bytecode.end() - metadataSize - 2, _bytecode.end() - 2);
|
||||
}
|
||||
@@ -185,7 +185,9 @@ bool isValidMetadata(string const& _metadata)
|
||||
!metadata.isMember("settings") ||
|
||||
!metadata.isMember("sources") ||
|
||||
!metadata.isMember("output") ||
|
||||
!metadata["settings"].isMember("evmVersion")
|
||||
!metadata["settings"].isMember("evmVersion") ||
|
||||
!metadata["settings"].isMember("metadata") ||
|
||||
!metadata["settings"]["metadata"].isMember("bytecodeHash")
|
||||
)
|
||||
return false;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
*/
|
||||
|
||||
#include <test/libsolidity/SolidityExecutionFramework.h>
|
||||
#include <libdevcore/SwarmHash.h>
|
||||
#include <libdevcore/IpfsHash.h>
|
||||
#include <libevmasm/GasMeter.h>
|
||||
|
||||
#include <cmath>
|
||||
@@ -40,18 +40,18 @@ namespace test
|
||||
#define CHECK_DEPLOY_GAS(_gasNoOpt, _gasOpt) \
|
||||
do \
|
||||
{ \
|
||||
u256 bzzr1Cost = GasMeter::dataGas(dev::bzzr1Hash(m_compiler.metadata(m_compiler.lastContractName())).asBytes(), true); \
|
||||
u256 ipfsCost = GasMeter::dataGas(dev::ipfsHash(m_compiler.metadata(m_compiler.lastContractName())), true); \
|
||||
u256 gasOpt{_gasOpt}; \
|
||||
u256 gasNoOpt{_gasNoOpt}; \
|
||||
u256 gas = m_optimiserSettings == OptimiserSettings::minimal() ? gasNoOpt : gasOpt; \
|
||||
BOOST_CHECK_MESSAGE( \
|
||||
m_gasUsed >= bzzr1Cost, \
|
||||
m_gasUsed >= ipfsCost, \
|
||||
"Gas used: " + \
|
||||
m_gasUsed.str() + \
|
||||
" is less than the data cost for the bzzr1 hash: " + \
|
||||
u256(bzzr1Cost).str() \
|
||||
" is less than the data cost for the IPFS hash: " + \
|
||||
u256(ipfsCost).str() \
|
||||
); \
|
||||
u256 gasUsed = m_gasUsed - bzzr1Cost; \
|
||||
u256 gasUsed = m_gasUsed - ipfsCost; \
|
||||
BOOST_CHECK_MESSAGE( \
|
||||
gas == gasUsed, \
|
||||
"Gas used: " + \
|
||||
@@ -96,17 +96,18 @@ BOOST_AUTO_TEST_CASE(string_storage)
|
||||
compileAndRun(sourceCode);
|
||||
|
||||
if (Options::get().evmVersion() <= EVMVersion::byzantium())
|
||||
CHECK_DEPLOY_GAS(134071, 130763);
|
||||
CHECK_DEPLOY_GAS(134209, 130895);
|
||||
// This is only correct on >=Constantinople.
|
||||
else if (Options::get().useABIEncoderV2)
|
||||
{
|
||||
if (Options::get().optimizeYul)
|
||||
CHECK_DEPLOY_GAS(151455, 127653);
|
||||
CHECK_DEPLOY_GAS(127785, 127785);
|
||||
else
|
||||
CHECK_DEPLOY_GAS(151455, 135371);
|
||||
CHECK_DEPLOY_GAS(151587, 135371);
|
||||
}
|
||||
else
|
||||
CHECK_DEPLOY_GAS(126861, 119591);
|
||||
CHECK_DEPLOY_GAS(126993, 119723);
|
||||
|
||||
if (Options::get().evmVersion() >= EVMVersion::byzantium())
|
||||
{
|
||||
callContractFunction("f()");
|
||||
|
||||
+102
-52
@@ -24,6 +24,7 @@
|
||||
#include <libsolidity/interface/CompilerStack.h>
|
||||
#include <libsolidity/interface/Version.h>
|
||||
#include <libdevcore/SwarmHash.h>
|
||||
#include <libdevcore/IpfsHash.h>
|
||||
#include <libdevcore/JSON.h>
|
||||
|
||||
using namespace std;
|
||||
@@ -60,28 +61,54 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
|
||||
}
|
||||
)";
|
||||
for (auto release: std::set<bool>{true, VersionIsRelease})
|
||||
{
|
||||
CompilerStack compilerStack;
|
||||
compilerStack.overwriteReleaseFlag(release);
|
||||
compilerStack.setSources({{"", std::string(sourceCode)}});
|
||||
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
||||
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
||||
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
||||
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
||||
std::string const& metadata = compilerStack.metadata("test");
|
||||
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
||||
bytes hash = dev::bzzr1Hash(metadata).asBytes();
|
||||
BOOST_REQUIRE(hash.size() == 32);
|
||||
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
||||
BOOST_CHECK(cborMetadata.size() == 2);
|
||||
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
||||
if (release)
|
||||
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
|
||||
else
|
||||
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
||||
BOOST_CHECK(cborMetadata.count("bzzr1") == 1);
|
||||
BOOST_CHECK(cborMetadata.at("bzzr1") == toHex(hash));
|
||||
}
|
||||
for (auto metadataHash: set<CompilerStack::MetadataHash>{
|
||||
CompilerStack::MetadataHash::IPFS,
|
||||
CompilerStack::MetadataHash::Bzzr1,
|
||||
CompilerStack::MetadataHash::None
|
||||
})
|
||||
{
|
||||
CompilerStack compilerStack;
|
||||
compilerStack.overwriteReleaseFlag(release);
|
||||
compilerStack.setSources({{"", std::string(sourceCode)}});
|
||||
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
||||
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
||||
compilerStack.setMetadataHash(metadataHash);
|
||||
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
||||
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
||||
std::string const& metadata = compilerStack.metadata("test");
|
||||
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
||||
|
||||
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
||||
if (metadataHash == CompilerStack::MetadataHash::None)
|
||||
BOOST_CHECK(cborMetadata.size() == 1);
|
||||
else
|
||||
{
|
||||
bytes hash;
|
||||
string hashMethod;
|
||||
if (metadataHash == CompilerStack::MetadataHash::IPFS)
|
||||
{
|
||||
hash = dev::ipfsHash(metadata);
|
||||
BOOST_REQUIRE(hash.size() == 34);
|
||||
hashMethod = "ipfs";
|
||||
}
|
||||
else
|
||||
{
|
||||
hash = dev::bzzr1Hash(metadata).asBytes();
|
||||
BOOST_REQUIRE(hash.size() == 32);
|
||||
hashMethod = "bzzr1";
|
||||
}
|
||||
|
||||
BOOST_CHECK(cborMetadata.size() == 2);
|
||||
BOOST_CHECK(cborMetadata.count(hashMethod) == 1);
|
||||
BOOST_CHECK(cborMetadata.at(hashMethod) == toHex(hash));
|
||||
}
|
||||
|
||||
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
||||
if (release)
|
||||
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
|
||||
else
|
||||
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
|
||||
@@ -94,31 +121,57 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
|
||||
function g(function(uint) external returns (uint) x) public {}
|
||||
}
|
||||
)";
|
||||
for(auto release: std::set<bool>{true, VersionIsRelease})
|
||||
{
|
||||
CompilerStack compilerStack;
|
||||
compilerStack.overwriteReleaseFlag(release);
|
||||
compilerStack.setSources({{"", std::string(sourceCode)}});
|
||||
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
||||
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
||||
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
||||
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
||||
std::string const& metadata = compilerStack.metadata("test");
|
||||
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
||||
bytes hash = dev::bzzr1Hash(metadata).asBytes();
|
||||
BOOST_REQUIRE(hash.size() == 32);
|
||||
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
||||
BOOST_CHECK(cborMetadata.size() == 3);
|
||||
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
||||
if (release)
|
||||
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
|
||||
else
|
||||
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
||||
BOOST_CHECK(cborMetadata.count("bzzr1") == 1);
|
||||
BOOST_CHECK(cborMetadata.at("bzzr1") == toHex(hash));
|
||||
BOOST_CHECK(cborMetadata.count("experimental") == 1);
|
||||
BOOST_CHECK(cborMetadata.at("experimental") == "true");
|
||||
}
|
||||
for (auto release: set<bool>{true, VersionIsRelease})
|
||||
for (auto metadataHash: set<CompilerStack::MetadataHash>{
|
||||
CompilerStack::MetadataHash::IPFS,
|
||||
CompilerStack::MetadataHash::Bzzr1,
|
||||
CompilerStack::MetadataHash::None
|
||||
})
|
||||
{
|
||||
CompilerStack compilerStack;
|
||||
compilerStack.overwriteReleaseFlag(release);
|
||||
compilerStack.setSources({{"", std::string(sourceCode)}});
|
||||
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
||||
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
||||
compilerStack.setMetadataHash(metadataHash);
|
||||
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
||||
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
||||
std::string const& metadata = compilerStack.metadata("test");
|
||||
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
||||
|
||||
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
||||
if (metadataHash == CompilerStack::MetadataHash::None)
|
||||
BOOST_CHECK(cborMetadata.size() == 2);
|
||||
else
|
||||
{
|
||||
bytes hash;
|
||||
string hashMethod;
|
||||
if (metadataHash == CompilerStack::MetadataHash::IPFS)
|
||||
{
|
||||
hash = dev::ipfsHash(metadata);
|
||||
BOOST_REQUIRE(hash.size() == 34);
|
||||
hashMethod = "ipfs";
|
||||
}
|
||||
else
|
||||
{
|
||||
hash = dev::bzzr1Hash(metadata).asBytes();
|
||||
BOOST_REQUIRE(hash.size() == 32);
|
||||
hashMethod = "bzzr1";
|
||||
}
|
||||
|
||||
BOOST_CHECK(cborMetadata.size() == 3);
|
||||
BOOST_CHECK(cborMetadata.count(hashMethod) == 1);
|
||||
BOOST_CHECK(cborMetadata.at(hashMethod) == toHex(hash));
|
||||
}
|
||||
|
||||
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
||||
if (release)
|
||||
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
|
||||
else
|
||||
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
||||
BOOST_CHECK(cborMetadata.count("experimental") == 1);
|
||||
BOOST_CHECK(cborMetadata.at("experimental") == "true");
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(metadata_relevant_sources)
|
||||
@@ -218,16 +271,13 @@ BOOST_AUTO_TEST_CASE(metadata_useLiteralContent)
|
||||
jsonParse(metadata_str, metadata);
|
||||
BOOST_CHECK(dev::test::isValidMetadata(metadata_str));
|
||||
BOOST_CHECK(metadata.isMember("settings"));
|
||||
BOOST_CHECK(metadata["settings"].isMember("metadata"));
|
||||
BOOST_CHECK(metadata["settings"]["metadata"].isMember("bytecodeHash"));
|
||||
if (_literal)
|
||||
{
|
||||
BOOST_CHECK(metadata["settings"].isMember("metadata"));
|
||||
BOOST_CHECK(metadata["settings"]["metadata"].isMember("useLiteralContent"));
|
||||
BOOST_CHECK(metadata["settings"]["metadata"]["useLiteralContent"].asBool());
|
||||
}
|
||||
else
|
||||
{
|
||||
BOOST_CHECK(!metadata["settings"].isMember("metadata"));
|
||||
}
|
||||
};
|
||||
|
||||
check(sourceCode, true);
|
||||
|
||||
@@ -364,7 +364,7 @@ BOOST_AUTO_TEST_CASE(basic_compilation)
|
||||
BOOST_CHECK_EQUAL(
|
||||
dev::test::bytecodeSansMetadata(contract["evm"]["bytecode"]["object"].asString()),
|
||||
string("6080604052348015600f57600080fd5b5060") +
|
||||
(VersionIsRelease ? "3e" : toHex(bytes{uint8_t(60 + VersionStringStrict.size())})) +
|
||||
(VersionIsRelease ? "3f" : toHex(bytes{uint8_t(61 + VersionStringStrict.size())})) +
|
||||
"80601d6000396000f3fe6080604052600080fdfe"
|
||||
);
|
||||
BOOST_CHECK(contract["evm"]["assembly"].isString());
|
||||
@@ -377,7 +377,7 @@ BOOST_AUTO_TEST_CASE(basic_compilation)
|
||||
"tag_1:\n /* \"fileA\":0:14 contract A { } */\n pop\n dataSize(sub_0)\n dup1\n "
|
||||
"dataOffset(sub_0)\n 0x00\n codecopy\n 0x00\n return\nstop\n\nsub_0: assembly {\n "
|
||||
"/* \"fileA\":0:14 contract A { } */\n mstore(0x40, 0x80)\n 0x00\n "
|
||||
"dup1\n revert\n\n auxdata: 0xa265627a7a72315820"
|
||||
"dup1\n revert\n\n auxdata: 0xa26469706673582212"
|
||||
) == 0);
|
||||
BOOST_CHECK(contract["evm"]["gasEstimates"].isObject());
|
||||
BOOST_CHECK_EQUAL(contract["evm"]["gasEstimates"].size(), 1);
|
||||
|
||||
@@ -14,9 +14,9 @@ contract C {
|
||||
}
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 1122600
|
||||
// codeDepositCost: 1122800
|
||||
// executionCost: 1167
|
||||
// totalCost: 1123767
|
||||
// totalCost: 1123967
|
||||
// external:
|
||||
// a(): 530
|
||||
// b(uint256): infinite
|
||||
|
||||
@@ -17,9 +17,9 @@ contract C {
|
||||
// optimize-yul: true
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 624200
|
||||
// codeDepositCost: 624400
|
||||
// executionCost: 657
|
||||
// totalCost: 624857
|
||||
// totalCost: 625057
|
||||
// external:
|
||||
// a(): 429
|
||||
// b(uint256): 884
|
||||
|
||||
@@ -13,8 +13,8 @@ contract C {
|
||||
}
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 256800
|
||||
// codeDepositCost: 257000
|
||||
// executionCost: 300
|
||||
// totalCost: 257100
|
||||
// totalCost: 257300
|
||||
// external:
|
||||
// f(): 252
|
||||
|
||||
@@ -24,9 +24,9 @@ contract Large {
|
||||
}
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 636800
|
||||
// codeDepositCost: 637000
|
||||
// executionCost: 670
|
||||
// totalCost: 637470
|
||||
// totalCost: 637670
|
||||
// external:
|
||||
// a(): 451
|
||||
// b(uint256): 846
|
||||
|
||||
@@ -27,9 +27,9 @@ contract Large {
|
||||
// optimize-runs: 2
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 260400
|
||||
// codeDepositCost: 260600
|
||||
// executionCost: 300
|
||||
// totalCost: 260700
|
||||
// totalCost: 260900
|
||||
// external:
|
||||
// a(): 398
|
||||
// b(uint256): 1105
|
||||
|
||||
@@ -11,9 +11,9 @@ contract Medium {
|
||||
}
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 253000
|
||||
// codeDepositCost: 253200
|
||||
// executionCost: 294
|
||||
// totalCost: 253294
|
||||
// totalCost: 253494
|
||||
// external:
|
||||
// a(): 428
|
||||
// b(uint256): 846
|
||||
|
||||
@@ -14,9 +14,9 @@ contract Medium {
|
||||
// optimize-runs: 2
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 140800
|
||||
// executionCost: 183
|
||||
// totalCost: 140983
|
||||
// codeDepositCost: 141000
|
||||
// executionCost: 190
|
||||
// totalCost: 141190
|
||||
// external:
|
||||
// a(): 398
|
||||
// b(uint256): 863
|
||||
|
||||
@@ -6,9 +6,9 @@ contract Small {
|
||||
}
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 83600
|
||||
// codeDepositCost: 83800
|
||||
// executionCost: 135
|
||||
// totalCost: 83735
|
||||
// totalCost: 83935
|
||||
// external:
|
||||
// fallback: 118
|
||||
// a(): 383
|
||||
|
||||
@@ -9,9 +9,9 @@ contract Small {
|
||||
// optimize-runs: 2
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 60400
|
||||
// codeDepositCost: 60600
|
||||
// executionCost: 111
|
||||
// totalCost: 60511
|
||||
// totalCost: 60711
|
||||
// external:
|
||||
// fallback: 118
|
||||
// a(): 376
|
||||
|
||||
Reference in New Issue
Block a user