Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SELFBALANCE opcode from EIP-1884 #24

Merged
merged 2 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no argument to selfbalanace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But when OP_SELFBALANCE is undefined, there is an argument missing to mstore(0).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But shouldn't it just abort because of invalid instruction?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in naive implementation of EVM. Here we check stack requirements for the whole block of instructions before executing them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that, but the concept is wrong then. evmone fails with a strange error code on invalid input then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this concept you mean, but yes, it can return other error code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggest to me that evmone cannot handle badly formatted EVM input, which has a stack underflow.


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