Create option for metadata hash

This commit is contained in:
Leonardo Alt
2019-09-13 18:59:12 +02:00
parent c72d1ffb65
commit d685554980
22 changed files with 272 additions and 117 deletions
+11 -10
View File
@@ -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
View File
@@ -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);
+2 -2
View File
@@ -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);
+2 -2
View File
@@ -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
+2 -2
View File
@@ -13,8 +13,8 @@ contract C {
}
// ----
// creation:
// codeDepositCost: 256800
// codeDepositCost: 257000
// executionCost: 300
// totalCost: 257100
// totalCost: 257300
// external:
// f(): 252
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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