Skip to content

Commit b0fea29

Browse files
gumb0chfast
authored andcommitted
EIP-2200 implementation: throw OOG for SSTORE with gas below stipend
1 parent 2f12eec commit b0fea29

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/evmone/analysis.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size)
131131
case OP_STATICCALL:
132132
case OP_CREATE:
133133
case OP_CREATE2:
134+
case OP_SSTORE:
134135
instr.arg.number = block.gas_cost;
135136
break;
136137

lib/evmone/instructions.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,14 @@ const instruction* op_sstore(const instruction* instr, execution_state& state) n
499499
if (state.msg->flags & EVMC_STATIC)
500500
return state.exit(EVMC_STATIC_MODE_VIOLATION);
501501

502+
if (state.rev >= EVMC_ISTANBUL)
503+
{
504+
const auto correction = state.current_block_cost - instr->arg.number;
505+
const auto gas_left = state.gas_left + correction;
506+
if (gas_left <= 2300)
507+
return state.exit(EVMC_OUT_OF_GAS);
508+
}
509+
502510
const auto key = intx::be::store<evmc::bytes32>(state.stack.pop());
503511
const auto value = intx::be::store<evmc::bytes32>(state.stack.pop());
504512
auto status = state.host.set_storage(state.msg->destination, key, value);

test/unittests/evm_state_test.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ TEST_F(evm_state, sstore_cost)
199199
}
200200
}
201201

202+
TEST_F(evm_state, sstore_below_stipend)
203+
{
204+
const auto code = sstore(0, 0);
205+
206+
rev = EVMC_HOMESTEAD;
207+
execute(2306, code);
208+
EXPECT_EQ(result.status_code, EVMC_OUT_OF_GAS);
209+
210+
rev = EVMC_CONSTANTINOPLE;
211+
execute(2306, code);
212+
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
213+
214+
rev = EVMC_ISTANBUL;
215+
execute(2306, code);
216+
EXPECT_EQ(result.status_code, EVMC_OUT_OF_GAS);
217+
218+
execute(2307, code);
219+
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
220+
}
221+
202222
TEST_F(evm_state, tx_context)
203223
{
204224
rev = EVMC_ISTANBUL;

0 commit comments

Comments
 (0)