mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #5940 from ethereum/test-metadata
Clean up metadata helpers in tests
This commit is contained in:
commit
e5bf1f1dd4
@ -22,6 +22,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <libdevcore/JSON.h>
|
#include <libdevcore/JSON.h>
|
||||||
|
#include <test/Metadata.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -30,21 +31,24 @@ namespace dev
|
|||||||
namespace test
|
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)
|
string bytecodeSansMetadata(string const& _bytecode)
|
||||||
{
|
{
|
||||||
/// The metadata hash takes up 43 bytes (or 86 characters in hex)
|
return toHex(bytecodeSansMetadata(fromHex(_bytecode, WhenError::Throw)));
|
||||||
/// /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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValidMetadata(string const& _metadata)
|
bool isValidMetadata(string const& _metadata)
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
* Metadata processing helpers.
|
* Metadata processing helpers.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <libdevcore/CommonData.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace dev
|
namespace dev
|
||||||
@ -27,6 +28,10 @@ namespace test
|
|||||||
{
|
{
|
||||||
|
|
||||||
/// Returns the bytecode with the metadata hash stripped out.
|
/// 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);
|
std::string bytecodeSansMetadata(std::string const& _bytecode);
|
||||||
|
|
||||||
/// Expects a serialised metadata JSON and returns true if the
|
/// Expects a serialised metadata JSON and returns true if the
|
||||||
|
@ -25,9 +25,6 @@
|
|||||||
#include <libsolidity/interface/Version.h>
|
#include <libsolidity/interface/Version.h>
|
||||||
#include <libsolc/libsolc.h>
|
#include <libsolc/libsolc.h>
|
||||||
|
|
||||||
#include <test/Metadata.h>
|
|
||||||
#include <test/Options.h>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace dev
|
namespace dev
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
* Tests for the Solidity optimizer.
|
* Tests for the Solidity optimizer.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <test/Metadata.h>
|
||||||
#include <test/libsolidity/SolidityExecutionFramework.h>
|
#include <test/libsolidity/SolidityExecutionFramework.h>
|
||||||
|
|
||||||
#include <libevmasm/Instruction.h>
|
#include <libevmasm/Instruction.h>
|
||||||
@ -105,11 +106,8 @@ public:
|
|||||||
/// into account.
|
/// into account.
|
||||||
size_t numInstructions(bytes const& _bytecode, boost::optional<Instruction> _which = boost::optional<Instruction>{})
|
size_t numInstructions(bytes const& _bytecode, boost::optional<Instruction> _which = boost::optional<Instruction>{})
|
||||||
{
|
{
|
||||||
BOOST_REQUIRE(_bytecode.size() > 5);
|
bytes realCode = bytecodeSansMetadata(_bytecode);
|
||||||
size_t metadataSize = (_bytecode[_bytecode.size() - 2] << 8) + _bytecode[_bytecode.size() - 1];
|
BOOST_REQUIRE_MESSAGE(!realCode.empty(), "Invalid or missing metadata in bytecode.");
|
||||||
BOOST_REQUIRE_MESSAGE(metadataSize == 0x29, "Invalid metadata size");
|
|
||||||
BOOST_REQUIRE(_bytecode.size() >= metadataSize + 2);
|
|
||||||
bytes realCode = bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
|
|
||||||
size_t instructions = 0;
|
size_t instructions = 0;
|
||||||
solidity::eachInstruction(realCode, [&](Instruction _instr, u256 const&) {
|
solidity::eachInstruction(realCode, [&](Instruction _instr, u256 const&) {
|
||||||
if (!_which || *_which == _instr)
|
if (!_which || *_which == _instr)
|
||||||
|
@ -23,8 +23,7 @@
|
|||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <libsolidity/interface/StandardCompiler.h>
|
#include <libsolidity/interface/StandardCompiler.h>
|
||||||
#include <libdevcore/JSON.h>
|
#include <libdevcore/JSON.h>
|
||||||
|
#include <test/Metadata.h>
|
||||||
#include "../Metadata.h"
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace dev::eth;
|
using namespace dev::eth;
|
||||||
|
Loading…
Reference in New Issue
Block a user