Skip to content

Commit 80ddcb3

Browse files
committed
feat(spec-specs, tests): EIP-8037 SSTORE refund clamp and initcode fixture guard (#2729)
1 parent 0272ae1 commit 80ddcb3

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/ethereum/forks/amsterdam/vm/instructions/storage.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ def sstore(evm: Evm) -> None:
127127
if original_value == new_value:
128128
# Storage slot being restored to its original value
129129
if original_value == 0:
130-
# Slot set then cleared: refund state gas to the
131-
# reservoir and regular cost via refund_counter. The
132-
# state-gas credit is tracked in state_gas_refund so it
133-
# can be unwound on revert or exceptional halt of this
134-
# frame or any ancestor that inherits it.
135-
evm.state_gas_left += state_gas_storage_set
136-
evm.state_gas_used -= state_gas_storage_set
130+
# Slot set then cleared: credit refund, clamped to this
131+
# frame's state_gas_used since the 0 to N SSTORE may
132+
# have charged state gas in an ancestor sharing storage
133+
# via CALLCODE/DELEGATECALL.
134+
state_gas_refund_applied = min(
135+
state_gas_storage_set, evm.state_gas_used
136+
)
137+
evm.state_gas_left += state_gas_refund_applied
138+
evm.state_gas_used -= state_gas_refund_applied
137139
evm.state_gas_refund += state_gas_storage_set
138140
evm.refund_counter += int(
139141
GAS_STORAGE_UPDATE

tests/shanghai/eip3860_initcode/test_initcode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def code_deposit_gas(self, fork: Fork, initcode: Initcode) -> int:
296296
cost_per_state_byte and a code hash cost is added.
297297
"""
298298
code_size = len(bytes(initcode.deploy_code))
299-
if hasattr(fork, "cost_per_state_byte"):
299+
if fork.is_eip_enabled(8037):
300300
gas_costs = fork.gas_costs()
301301
cpsb = fork.cost_per_state_byte()
302302
return (
@@ -597,7 +597,7 @@ def code_deposit_gas(self, fork: Fork, initcode: Initcode) -> int:
597597
cost_per_state_byte and a code hash cost is added.
598598
"""
599599
code_size = len(bytes(initcode.deploy_code))
600-
if hasattr(fork, "cost_per_state_byte"):
600+
if fork.is_eip_enabled(8037):
601601
gas_costs = fork.gas_costs()
602602
cpsb = fork.cost_per_state_byte()
603603
return (

0 commit comments

Comments
 (0)