Skip to content

Commit

Permalink
Merge pull request #24 from axic/selfbalance
Browse files Browse the repository at this point in the history
Implement SELFBALANCE opcode from EIP-1884
  • Loading branch information
chfast authored Oct 31, 2019
2 parents 1f17fb1 + 81ec0f9 commit b15dd08
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning].
[#191](https://github.com/ethereum/evmone/pull/191)
- Implementation of CHAINID instruction from the **Istanbul** EVM revision ([EIP-1344]).
[#190](https://github.com/ethereum/evmone/pull/190)
- Implementation of SELFBALANCE instruction from the **Istanbul** EVM revision ([EIP-1884]).
[#24](https://github.com/ethereum/evmone/pull/24)


## [0.2.0] - 2019-09-24
Expand Down
9 changes: 8 additions & 1 deletion lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ const instruction* op_chainid(const instruction* instr, execution_state& state)
return ++instr;
}

const instruction* op_selfbalance(const instruction* instr, execution_state& state) noexcept
{
// TODO: introduce selfbalance in EVMC?
state.stack.push(intx::be::load<uint256>(state.host.get_balance(state.msg->destination)));
return ++instr;
}

const instruction* op_origin(const instruction* instr, execution_state& state) noexcept
{
state.stack.push(intx::be::load<uint256>(state.host.get_tx_context().tx_origin));
Expand Down Expand Up @@ -1386,7 +1393,7 @@ constexpr op_table create_op_table_istanbul() noexcept
table[OP_BALANCE] = {op_balance, 700, 1, 0};
table[OP_CHAINID] = {op_chainid, 2, 0, 1};
table[OP_EXTCODEHASH] = {op_extcodehash, 700, 1, 0};
table[OP_SELFBALANCE] = {op_undefined, 5, 0, 1};
table[OP_SELFBALANCE] = {op_selfbalance, 5, 0, 1};
table[OP_SLOAD] = {op_sload, 800, 1, 0};
return table;
}
Expand Down
23 changes: 23 additions & 0 deletions test/unittests/evm_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,29 @@ TEST_F(evm_state, balance)
EXPECT_EQ(result.output_data[5], 0x01);
}

TEST_F(evm_state, selfbalance)
{
host.accounts[msg.destination].set_balance(0x0504030201);
// NOTE: adding push here to balance out the stack pre-Istanbul (needed to get undefined
// instruction as a result)
auto code = bytecode{} + push(1) + OP_SELFBALANCE + mstore(0) + ret(32 - 6, 6);

rev = EVMC_CONSTANTINOPLE;
execute(code);
EXPECT_EQ(result.status_code, EVMC_UNDEFINED_INSTRUCTION);

rev = EVMC_ISTANBUL;
execute(code);
EXPECT_GAS_USED(EVMC_SUCCESS, 23);
ASSERT_EQ(result.output_size, 6);
EXPECT_EQ(result.output_data[0], 0);
EXPECT_EQ(result.output_data[1], 0x05);
EXPECT_EQ(result.output_data[2], 0x04);
EXPECT_EQ(result.output_data[3], 0x03);
EXPECT_EQ(result.output_data[4], 0x02);
EXPECT_EQ(result.output_data[5], 0x01);
}

TEST_F(evm_state, log)
{
for (auto op : {OP_LOG0, OP_LOG1, OP_LOG2, OP_LOG3, OP_LOG4})
Expand Down
2 changes: 2 additions & 0 deletions test/unittests/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ TEST_F(evm, caller_callvalue)
auto a = from_hex("0000ddee000000000000");
EXPECT_EQ(bytes(&result.output_data[0], 10), a);
}

TEST_F(evm, undefined)
{
execute(1, "2a");
Expand All @@ -582,6 +583,7 @@ TEST_F(evm, invalid)
EXPECT_EQ(result.status_code, EVMC_INVALID_INSTRUCTION);
EXPECT_EQ(result.gas_left, 0);
}

TEST_F(evm, sha3)
{
execute("6108006103ff2060005260206000f3");
Expand Down

0 comments on commit b15dd08

Please sign in to comment.