Skip to content

Commit

Permalink
EIP-2200 implementation: throw OOG for SSTORE with gas below stipend
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Sep 4, 2019
1 parent eb2247e commit 6bead9b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/evmone/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ code_analysis analyze(
break;

case OP_GAS:
case OP_SSTORE:
instr.arg.p.number = block->gas_cost;
break;

Expand Down
8 changes: 8 additions & 0 deletions lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ const instr_info* op_sstore(const instr_info* instr, execution_state& state) noe
if (state.msg->flags & EVMC_STATIC)
return state.exit(EVMC_STATIC_MODE_VIOLATION);

if (state.rev >= EVMC_ISTANBUL)
{
const auto correction = state.current_block_cost - instr->arg.p.number;
const auto gas_left = state.gas_left + correction;
if (gas_left <= 2300)
return state.exit(EVMC_OUT_OF_GAS);
}

const auto key = intx::be::store<evmc::bytes32>(state.stack.pop());
const auto value = intx::be::store<evmc::bytes32>(state.stack.pop());
auto status = state.host.set_storage(state.msg->destination, key, value);
Expand Down
20 changes: 20 additions & 0 deletions test/unittests/evm_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ TEST_F(evm_state, sstore_cost)
}
}

TEST_F(evm_state, sstore_below_stipend)
{
const auto code = sstore(0, 0);

rev = EVMC_HOMESTEAD;
execute(2306, code);
EXPECT_EQ(result.status_code, EVMC_OUT_OF_GAS);

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

rev = EVMC_ISTANBUL;
execute(2306, code);
EXPECT_EQ(result.status_code, EVMC_OUT_OF_GAS);

execute(2307, code);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
}

TEST_F(evm_state, tx_context)
{
host.tx_context.block_timestamp = 0xdd;
Expand Down

0 comments on commit 6bead9b

Please sign in to comment.