Merge pull request #5940 from ethereum/test-metadata

Clean up metadata helpers in tests
This commit is contained in:
Alex Beregszaszi 2019-02-06 12:34:27 +00:00 committed by GitHub
commit e5bf1f1dd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 23 deletions

View File

@ -22,6 +22,7 @@
#include <string>
#include <iostream>
#include <libdevcore/JSON.h>
#include <test/Metadata.h>
using namespace std;
@ -30,21 +31,24 @@ namespace dev
namespace test
{
bytes bytecodeSansMetadata(bytes const& _bytecode)
{
unsigned size = _bytecode.size();
if (size < 5)
return bytes{};
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 1 or 2 entries
unsigned char firstByte = _bytecode[size - metadataSize - 2];
if (firstByte != 0xa1 && firstByte != 0xa2)
return bytes{};
return bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
}
string bytecodeSansMetadata(string const& _bytecode)
{
/// The metadata hash takes up 43 bytes (or 86 characters in hex)
/// /a165627a7a72305820([0-9a-f]{64})0029$/
if (_bytecode.size() < 88)
return _bytecode;
if (_bytecode.substr(_bytecode.size() - 4, 4) != "0029")
return _bytecode;
if (_bytecode.substr(_bytecode.size() - 86, 18) != "a165627a7a72305820")
return _bytecode;
return _bytecode.substr(0, _bytecode.size() - 86);
return toHex(bytecodeSansMetadata(fromHex(_bytecode, WhenError::Throw)));
}
bool isValidMetadata(string const& _metadata)

View File

@ -19,6 +19,7 @@
* Metadata processing helpers.
*/
#include <libdevcore/CommonData.h>
#include <string>
namespace dev
@ -27,6 +28,10 @@ namespace test
{
/// Returns the bytecode with the metadata hash stripped out.
bytes bytecodeSansMetadata(bytes const& _bytecode);
/// Returns the bytecode with the metadata hash stripped out.
/// Throws exception on invalid hex string.
std::string bytecodeSansMetadata(std::string const& _bytecode);
/// Expects a serialised metadata JSON and returns true if the

View File

@ -25,9 +25,6 @@
#include <libsolidity/interface/Version.h>
#include <libsolc/libsolc.h>
#include <test/Metadata.h>
#include <test/Options.h>
using namespace std;
namespace dev

View File

@ -20,6 +20,7 @@
* Tests for the Solidity optimizer.
*/
#include <test/Metadata.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
#include <libevmasm/Instruction.h>
@ -105,11 +106,8 @@ public:
/// into account.
size_t numInstructions(bytes const& _bytecode, boost::optional<Instruction> _which = boost::optional<Instruction>{})
{
BOOST_REQUIRE(_bytecode.size() > 5);
size_t metadataSize = (_bytecode[_bytecode.size() - 2] << 8) + _bytecode[_bytecode.size() - 1];
BOOST_REQUIRE_MESSAGE(metadataSize == 0x29, "Invalid metadata size");
BOOST_REQUIRE(_bytecode.size() >= metadataSize + 2);
bytes realCode = bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
bytes realCode = bytecodeSansMetadata(_bytecode);
BOOST_REQUIRE_MESSAGE(!realCode.empty(), "Invalid or missing metadata in bytecode.");
size_t instructions = 0;
solidity::eachInstruction(realCode, [&](Instruction _instr, u256 const&) {
if (!_which || *_which == _instr)

View File

@ -23,8 +23,7 @@
#include <boost/test/unit_test.hpp>
#include <libsolidity/interface/StandardCompiler.h>
#include <libdevcore/JSON.h>
#include "../Metadata.h"
#include <test/Metadata.h>
using namespace std;
using namespace dev::eth;