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 17, 2019
1 parent 9230957 commit 7c65f43
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 @@ -131,6 +131,7 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size)
case OP_STATICCALL:
case OP_CREATE:
case OP_CREATE2:
case OP_SSTORE:
instr.arg.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 @@ -486,6 +486,14 @@ const instruction* op_sstore(const instruction* instr, execution_state& state) n
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.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 @@ -199,6 +199,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 7c65f43

Please sign in to comment.