Skip to content

Commit

Permalink
bytecode: Add push(opcode, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Jan 16, 2021
1 parent 1e739f4 commit 2174f86
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
20 changes: 20 additions & 0 deletions test/unittests/bytecode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ TEST(bytecode, push_int)
EXPECT_EQ(push(0xffffffffffffffff), "67ffffffffffffffff");
}

TEST(bytecode, push_explicit_opcode)
{
EXPECT_THROW(push(OP_JUMPDEST, {}), std::invalid_argument);
EXPECT_THROW(push(OP_DUP1, {}), std::invalid_argument);

EXPECT_THROW(push(OP_PUSH1, "0000"), std::invalid_argument);

EXPECT_EQ(push(OP_PUSH1, {}), "6000");
EXPECT_EQ(push(OP_PUSH1, "ff"), "60ff");
EXPECT_EQ(push(OP_PUSH2, ""), "610000");
EXPECT_EQ(push(OP_PUSH2, "01"), "610001");
EXPECT_EQ(push(OP_PUSH2, "8001"), "618001");
EXPECT_EQ(
push(OP_PUSH32, ""), "7f0000000000000000000000000000000000000000000000000000000000000000");
EXPECT_EQ(push(OP_PUSH32, "ab"),
"7f00000000000000000000000000000000000000000000000000000000000000ab");
EXPECT_EQ(push(OP_PUSH32, "fe000000000000000000000000000000000000000000000000000000000000ef"),
"7ffe000000000000000000000000000000000000000000000000000000000000ef");
}

TEST(bytecode, add)
{
auto e = "6007600d0160005260206000f3";
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ TEST_P(evm, swapsn_push)
// EIP-663 variants B and C.
// When SWAPSN is implemented execution will succeed, considering PUSH an argument of SWAPSN.
const auto swapsn = "b3";
const auto code = push(5) + OP_JUMP + swapsn + push(OP_JUMPDEST) + push(0) + ret_top();
const auto code = push(5) + OP_JUMP + swapsn + push(uint8_t{OP_JUMPDEST}) + push(0) + ret_top();

rev = EVMC_PETERSBURG;
execute(code);
Expand Down
15 changes: 15 additions & 0 deletions test/utils/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <evmc/instructions.h>
#include <test/utils/utils.hpp>
#include <algorithm>
#include <ostream>
#include <stdexcept>

struct bytecode;
Expand Down Expand Up @@ -76,6 +77,20 @@ inline bytecode push(std::string_view hex_data)
return push(from_hex(hex_data));
}

bytecode push(evmc_opcode opcode) = delete;

inline bytecode push(evmc_opcode opcode, const bytecode& data)
{
if (opcode < OP_PUSH1 || opcode > OP_PUSH32)
throw std::invalid_argument{"invalid push opcode " + std::to_string(opcode)};

const auto num_instr_bytes = static_cast<size_t>(opcode) - OP_PUSH1 + 1;
if (data.size() > num_instr_bytes)
throw std::invalid_argument{"push data too long"};

const auto instr_bytes = bytes(num_instr_bytes - data.size(), 0) + bytes{data};
return opcode + instr_bytes;
}

inline bytecode push(uint64_t n)
{
Expand Down

0 comments on commit 2174f86

Please sign in to comment.