Move bytecodeSansMetadata(bytes) helper to test/Metadata

This commit is contained in:
Alex Beregszaszi 2019-02-05 22:37:02 +00:00
parent be22032141
commit 26de5684a2
3 changed files with 18 additions and 5 deletions

View File

@ -31,6 +31,17 @@ 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 (metadataSize != 0x29 || size < (metadataSize + 2))
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)

View File

@ -19,6 +19,7 @@
* Metadata processing helpers.
*/
#include <libdevcore/CommonData.h>
#include <string>
namespace dev
@ -26,6 +27,9 @@ namespace dev
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.
std::string bytecodeSansMetadata(std::string const& _bytecode);

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)