Skip to content

Commit 4dab63c

Browse files
marioevzfselmo
authored andcommitted
feat(test-forks): EIP-8037 add SELFDESTRUCT refund
1 parent 75409a1 commit 4dab63c

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

packages/testing/src/execution_testing/forks/forks/eips/amsterdam/eip_8037.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,10 @@ def opcode_refund_calculator(cls) -> OpcodeGasCalculator:
183183

184184
def fn(opcode: OpcodeBase) -> int:
185185
# Get the gas refund or calculator
186+
state_refund = opcode_state_refund_calculator(opcode)
186187
if opcode not in opcode_refund_map:
187188
# Most opcodes don't provide refunds
188-
return 0
189+
return state_refund
189190
refund_or_calculator = opcode_refund_map[opcode]
190191

191192
# If it's a callable, call it with the opcode
@@ -196,7 +197,7 @@ def fn(opcode: OpcodeBase) -> int:
196197
regular_refund = refund_or_calculator
197198

198199
# EIP-8037 adds the state refund on top of the regular refund.
199-
return regular_refund + opcode_state_refund_calculator(opcode)
200+
return regular_refund + state_refund
200201

201202
return fn
202203

@@ -217,6 +218,11 @@ def opcode_state_refund_map(
217218
Opcodes.SSTORE: lambda op: cls._calculate_sstore_state_refund(
218219
op, gas_costs
219220
),
221+
Opcodes.SELFDESTRUCT: (
222+
lambda op: cls._calculate_selfdestruct_state_refund(
223+
op, gas_costs
224+
)
225+
),
220226
}
221227

222228
@classmethod
@@ -386,6 +392,37 @@ def _calculate_sstore_state_refund(
386392
return 32 * cpsb
387393
return 0
388394

395+
@classmethod
396+
def _calculate_selfdestruct_state_refund(
397+
cls, opcode: OpcodeBase, gas_costs: GasCosts
398+
) -> int:
399+
"""
400+
Calculate SELFDESTRUCT state gas refund.
401+
402+
Account creation: 112 × cost_per_state_byte
403+
Created storage slots: 32 × cost_per_state_byte per non-zero slot
404+
Code deposit: len(code) × cost_per_state_byte
405+
"""
406+
del gas_costs
407+
metadata = opcode.metadata
408+
cpsb = cls.cost_per_state_byte()
409+
410+
self_destructed_account = metadata["self_destructed_account"]
411+
self_destructed_account_storage_slot_count = metadata[
412+
"self_destructed_account_storage_slot_count"
413+
]
414+
self_destructed_account_code_deposit = metadata[
415+
"self_destructed_account_code_deposit"
416+
]
417+
state_refund = 0
418+
if self_destructed_account:
419+
state_refund = 112 * cpsb
420+
state_refund += (
421+
32 * cpsb * self_destructed_account_storage_slot_count
422+
)
423+
state_refund += cpsb * self_destructed_account_code_deposit
424+
return state_refund
425+
389426
@classmethod
390427
def _calculate_return_gas(
391428
cls, opcode: OpcodeBase, gas_costs: GasCosts

packages/testing/src/execution_testing/vm/opcodes.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5617,7 +5617,13 @@ class Opcodes(Opcode, Enum):
56175617
0xFF,
56185618
popped_stack_items=1,
56195619
kwargs=["address"],
5620-
metadata={"address_warm": False, "account_new": False},
5620+
metadata={
5621+
"address_warm": False,
5622+
"account_new": False,
5623+
"self_destructed_account": False,
5624+
"self_destructed_account_storage_slot_count": 0,
5625+
"self_destructed_account_code_deposit": 0,
5626+
},
56215627
)
56225628
"""
56235629
SELFDESTRUCT(address)
@@ -5645,6 +5651,12 @@ class Opcodes(Opcode, Enum):
56455651
(default: False)
56465652
- account_new: whether creating a new beneficiary account, requires
56475653
non-zero balance in the source account (default: False)
5654+
- self_destructed_account: whether the execution results in an account
5655+
self-destructing (default: False)
5656+
- self_destructed_account_storage_slot_count: amount of storage slots that
5657+
were created in the self-destructing account (default: 0)
5658+
- self_destructed_account_code_deposit: amount of bytes that comprised the
5659+
code of the self-destructing account (default: 0)
56485660
56495661
Source: [evm.codes/#FF](https://www.evm.codes/#FF)
56505662
"""

0 commit comments

Comments
 (0)