diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2017-08-12 00:21:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-12 00:21:08 +0800 |
commit | e3d1137d2b7c1a626d2b2bb977d9574a6c5c91ca (patch) | |
tree | 26e6d568acd7bbed2e11228de9a439e04b34d6c4 | |
parent | 92b535f2ac125fe7c4404e6c91c9d245bbf7bda7 (diff) | |
parent | 2d1bab0de8d6e2637b3827fd3c988ae538b1d9ef (diff) | |
download | dexon-solidity-e3d1137d2b7c1a626d2b2bb977d9574a6c5c91ca.tar.gz dexon-solidity-e3d1137d2b7c1a626d2b2bb977d9574a6c5c91ca.tar.zst dexon-solidity-e3d1137d2b7c1a626d2b2bb977d9574a6c5c91ca.zip |
Merge pull request #2712 from ethereum/experimental-metadata
Store experimental flag in metadata CBOR
-rw-r--r-- | Changelog.md | 2 | ||||
-rw-r--r-- | libsolidity/ast/ExperimentalFeatures.h | 17 | ||||
-rw-r--r-- | libsolidity/interface/CompilerStack.cpp | 32 | ||||
-rw-r--r-- | test/libsolidity/Metadata.cpp | 30 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 14 |
5 files changed, 81 insertions, 14 deletions
diff --git a/Changelog.md b/Changelog.md index 0381d434..3f3d2e98 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,8 @@ Features: * Parser: Display previous visibility specifier in error if multiple are found. + * Syntax Checker: Support ``pragma experimental <feature>;`` to turn on experimental features. + * Metadata: Store experimental flag in metadata CBOR. Bugfixes: diff --git a/libsolidity/ast/ExperimentalFeatures.h b/libsolidity/ast/ExperimentalFeatures.h index b0a07142..04b26300 100644 --- a/libsolidity/ast/ExperimentalFeatures.h +++ b/libsolidity/ast/ExperimentalFeatures.h @@ -27,9 +27,20 @@ namespace dev namespace solidity { -enum class ExperimentalFeature {}; - -static const std::map<std::string, ExperimentalFeature> ExperimentalFeatureNames = {}; +enum class ExperimentalFeature +{ + Test, + TestOnlyAnalysis +}; + +static const std::map<ExperimentalFeature, bool> ExperimentalFeatureOnlyAnalysis = { + { ExperimentalFeature::TestOnlyAnalysis, true }, +}; + +static const std::map<std::string, ExperimentalFeature> ExperimentalFeatureNames = { + { "__test", ExperimentalFeature::Test }, + { "__testOnlyAnalysis", ExperimentalFeature::TestOnlyAnalysis }, +}; } } diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 7c66a843..70bebfa5 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -632,6 +632,17 @@ string CompilerStack::absolutePath(string const& _path, string const& _reference return result.generic_string(); } +namespace +{ +bool onlySafeExperimentalFeaturesActivated(set<ExperimentalFeature> const& features) +{ + for (auto const feature: features) + if (!ExperimentalFeatureOnlyAnalysis.count(feature)) + return false; + return true; +} +} + void CompilerStack::compileContract( ContractDefinition const& _contract, map<ContractDefinition const*, eth::Assembly const*>& _compiledContracts @@ -649,10 +660,23 @@ void CompilerStack::compileContract( shared_ptr<Compiler> compiler = make_shared<Compiler>(m_optimize, m_optimizeRuns); Contract& compiledContract = m_contracts.at(_contract.fullyQualifiedName()); string metadata = createMetadata(compiledContract); - bytes cborEncodedMetadata = - // CBOR-encoding of {"bzzr0": dev::swarmHash(metadata)} - bytes{0xa1, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} + - dev::swarmHash(metadata).asBytes(); + bytes cborEncodedHash = + // CBOR-encoding of the key "bzzr0" + bytes{0x65, 'b', 'z', 'z', 'r', '0'}+ + // CBOR-encoding of the hash + bytes{0x58, 0x20} + dev::swarmHash(metadata).asBytes(); + bytes cborEncodedMetadata; + if (onlySafeExperimentalFeaturesActivated(_contract.sourceUnit().annotation().experimentalFeatures)) + cborEncodedMetadata = + // CBOR-encoding of {"bzzr0": dev::swarmHash(metadata)} + bytes{0xa1} + + cborEncodedHash; + else + cborEncodedMetadata = + // CBOR-encoding of {"bzzr0": dev::swarmHash(metadata), "experimental": true} + bytes{0xa2} + + cborEncodedHash + + bytes{0x6c, 'e', 'x', 'p', 'e', 'r', 'i', 'm', 'e', 'n', 't', 'a', 'l', 0xf5}; solAssert(cborEncodedMetadata.size() <= 0xffff, "Metadata too large"); // 16-bit big endian length cborEncodedMetadata += toCompactBigEndian(cborEncodedMetadata.size(), 2); diff --git a/test/libsolidity/Metadata.cpp b/test/libsolidity/Metadata.cpp index 0d3caddd..c46e3160 100644 --- a/test/libsolidity/Metadata.cpp +++ b/test/libsolidity/Metadata.cpp @@ -38,6 +38,7 @@ BOOST_AUTO_TEST_CASE(metadata_stamp) // Check that the metadata stamp is at the end of the runtime bytecode. char const* sourceCode = R"( pragma solidity >=0.0; + pragma experimental __testOnlyAnalysis; contract test { function g(function(uint) external returns (uint) x) {} } @@ -58,6 +59,35 @@ BOOST_AUTO_TEST_CASE(metadata_stamp) BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2)); } +BOOST_AUTO_TEST_CASE(metadata_stamp_experimental) +{ + // Check that the metadata stamp is at the end of the runtime bytecode. + char const* sourceCode = R"( + pragma solidity >=0.0; + pragma experimental __test; + contract test { + function g(function(uint) external returns (uint) x) {} + } + )"; + CompilerStack compilerStack; + compilerStack.addSource("", std::string(sourceCode)); + compilerStack.setOptimiserSettings(dev::test::Options::get().optimize); + ETH_TEST_REQUIRE_NO_THROW(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::swarmHash(metadata).asBytes(); + BOOST_REQUIRE(hash.size() == 32); + BOOST_REQUIRE(bytecode.size() >= 2); + size_t metadataCBORSize = (size_t(bytecode.end()[-2]) << 8) + size_t(bytecode.end()[-1]); + BOOST_REQUIRE(metadataCBORSize < bytecode.size() - 2); + bytes expectation = + bytes{0xa2, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} + + hash + + bytes{0x6c, 'e', 'x', 'p', 'e', 'r', 'i', 'm', 'e', 'n', 't', 'a', 'l', 0xf5}; + BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2)); +} + BOOST_AUTO_TEST_CASE(metadata_relevant_sources) { CompilerStack compilerStack; diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index a0c9ed50..f7648bd7 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -6584,15 +6584,15 @@ BOOST_AUTO_TEST_CASE(experimental_pragma) pragma experimental unsupportedName unsupportedName; )"; CHECK_ERROR(text, SyntaxError, "Stray arguments."); + text = R"( + pragma experimental __test; + )"; + CHECK_WARNING(text, "Experimental features are turned on. Do not use experimental features on live deployments."); // text = R"( -// pragma experimental supportedName; -// )"; -// CHECK_WARNING(text, "Experimental features are turned on. Do not use experimental features on live deployments."); -// text = R"( -// pragma experimental supportedName; -// pragma experimental supportedName; +// pragma experimental __test; +// pragma experimental __test; // )"; -// CHECK_ERROR(text, SyntaxError, "Duplicate experimental feature name."); +// CHECK_ERROR_ALLOW_MULTI(text, SyntaxError, "Duplicate experimental feature name."); } BOOST_AUTO_TEST_SUITE_END() |