mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add onlyMetadata helper in test/Metadata
This commit is contained in:
parent
4d8c57006b
commit
12f34c8229
@ -31,7 +31,7 @@ namespace dev
|
||||
namespace test
|
||||
{
|
||||
|
||||
bytes bytecodeSansMetadata(bytes const& _bytecode)
|
||||
bytes onlyMetadata(bytes const& _bytecode)
|
||||
{
|
||||
unsigned size = _bytecode.size();
|
||||
if (size < 5)
|
||||
@ -43,6 +43,14 @@ bytes bytecodeSansMetadata(bytes const& _bytecode)
|
||||
unsigned char firstByte = _bytecode[size - metadataSize - 2];
|
||||
if (firstByte != 0xa1 && firstByte != 0xa2)
|
||||
return bytes{};
|
||||
return bytes(_bytecode.end() - metadataSize - 2, _bytecode.end() - 2);
|
||||
}
|
||||
|
||||
bytes bytecodeSansMetadata(bytes const& _bytecode)
|
||||
{
|
||||
unsigned metadataSize = onlyMetadata(_bytecode).size();
|
||||
if (metadataSize == 0)
|
||||
return bytes{};
|
||||
return bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,9 @@ namespace dev
|
||||
namespace test
|
||||
{
|
||||
|
||||
/// Returns only the CBOR metadata.
|
||||
bytes onlyMetadata(bytes const& _bytecode);
|
||||
|
||||
/// Returns the bytecode with the metadata hash stripped out.
|
||||
bytes bytecodeSansMetadata(bytes const& _bytecode);
|
||||
|
||||
|
@ -54,11 +54,10 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
|
||||
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 cborMetadata = dev::test::onlyMetadata(bytecode);
|
||||
BOOST_REQUIRE(!cborMetadata.empty());
|
||||
bytes expectation = bytes{0xa1, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} + hash;
|
||||
BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2));
|
||||
BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), cborMetadata.begin()));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
|
||||
@ -81,14 +80,13 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
|
||||
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 cborMetadata = dev::test::onlyMetadata(bytecode);
|
||||
BOOST_REQUIRE(!cborMetadata.empty());
|
||||
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_CHECK(std::equal(expectation.begin(), expectation.end(), cborMetadata.begin()));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(metadata_relevant_sources)
|
||||
|
Loading…
Reference in New Issue
Block a user