Skip to content

Commit aa7e019

Browse files
gumb0chfast
authored andcommitted
Implement EIP-1344 ChainID opcode
1 parent 6326761 commit aa7e019

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

lib/evmone/execution.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include "execution.hpp"
66
#include "analysis.hpp"
7+
8+
#include <ethash/keccak.hpp>
9+
#include <evmc/instructions.h>
10+
711
#include <memory>
812

913
namespace evmone

lib/evmone/instructions.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include "analysis.hpp"
66
#include <ethash/keccak.hpp>
7+
#include <evmc/instructions.h>
8+
9+
#include <cassert>
710

811
namespace evmone
912
{
@@ -330,6 +333,12 @@ const instruction* op_balance(const instruction* instr, execution_state& state)
330333
return ++instr;
331334
}
332335

336+
const instruction* op_chainid(const instruction* instr, execution_state& state) noexcept
337+
{
338+
state.stack.push(intx::be::load<uint256>(state.host.get_tx_context().chain_id));
339+
return ++instr;
340+
}
341+
333342
const instruction* op_origin(const instruction* instr, execution_state& state) noexcept
334343
{
335344
state.stack.push(intx::be::load<uint256>(state.host.get_tx_context().tx_origin));
@@ -1378,7 +1387,7 @@ constexpr op_table create_op_table_istanbul() noexcept
13781387
{
13791388
auto table = create_op_table_constantinople();
13801389
table[OP_BALANCE] = {op_balance, 700, 1, 0};
1381-
table[OP_CHAINID] = {op_undefined, 2, 0, 1};
1390+
table[OP_CHAINID] = {op_chainid, 2, 0, 1};
13821391
table[OP_EXTCODEHASH] = {op_extcodehash, 700, 1, 0};
13831392
table[OP_SELFBALANCE] = {op_undefined, 5, 0, 1};
13841393
table[OP_SLOAD] = {op_sload, 800, 1, 0};

test/unittests/evm_state_test.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,28 @@ TEST_F(evm_state, sstore_cost)
171171

172172
TEST_F(evm_state, tx_context)
173173
{
174+
rev = EVMC_ISTANBUL;
175+
174176
host.tx_context.block_timestamp = 0xdd;
175-
host.tx_context.block_coinbase.bytes[1] = 0xcc;
176177
host.tx_context.block_number = 0x1100;
177-
host.tx_context.block_difficulty.bytes[1] = 0xdd;
178178
host.tx_context.block_gas_limit = 0x990000;
179-
host.tx_context.tx_gas_price.bytes[2] = 0x66;
179+
host.tx_context.chain_id.bytes[28] = 0xaa;
180+
host.tx_context.block_coinbase.bytes[1] = 0xcc;
180181
host.tx_context.tx_origin.bytes[2] = 0x55;
182+
host.tx_context.block_difficulty.bytes[1] = 0xdd;
183+
host.tx_context.tx_gas_price.bytes[2] = 0x66;
181184

182-
std::string s;
183-
s += "4241173a17"; // TIMESTAMP COINBASE OR GASPRICE OR
184-
s += "4317441745173217"; // NUMBER OR DIFFICULTY OR GASLIMIT OR ORIGIN OR
185-
s += "600052"; // m[0..] =
186-
s += "60206000f3"; // RETURN(0,32)
187-
execute(47, s);
185+
auto const code = bytecode{} + OP_TIMESTAMP + OP_COINBASE + OP_OR + OP_GASPRICE + OP_OR +
186+
OP_NUMBER + OP_OR + OP_DIFFICULTY + OP_OR + OP_GASLIMIT + OP_OR + OP_ORIGIN +
187+
OP_OR + OP_CHAINID + OP_OR + ret_top();
188+
execute(52, code);
188189
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
189190
EXPECT_EQ(result.gas_left, 0);
190191
ASSERT_EQ(result.output_size, 32);
191192
EXPECT_EQ(result.output_data[31], 0xdd);
192193
EXPECT_EQ(result.output_data[30], 0x11);
193194
EXPECT_EQ(result.output_data[29], 0x99);
195+
EXPECT_EQ(result.output_data[28], 0xaa);
194196
EXPECT_EQ(result.output_data[14], 0x55);
195197
EXPECT_EQ(result.output_data[13], 0xcc);
196198
EXPECT_EQ(result.output_data[2], 0x66);

0 commit comments

Comments
 (0)