Skip to content

Commit d4aa994

Browse files
committed
chore(test): cleanup / review for 8037 byte value updates
1 parent 1c7d28e commit d4aa994

22 files changed

Lines changed: 176 additions & 90 deletions

File tree

packages/testing/src/execution_testing/cli/pytest_commands/plugins/shared/transaction_fixtures.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ def type_3_default_transaction(sender: EOA) -> Transaction:
9191

9292

9393
@pytest.fixture
94-
def type_4_default_transaction(
95-
sender: EOA, pre: Alloc, fork: Fork
96-
) -> Transaction:
94+
def type_4_default_transaction(sender: EOA, pre: Alloc) -> Transaction:
9795
"""Type 4 (set code) default transaction introduced in Prague fork."""
9896
# Create authorized accounts with funds
9997
auth_signer1 = pre.fund_eoa(amount=10**18)
@@ -103,14 +101,12 @@ def type_4_default_transaction(
103101
target1 = pre.deploy_contract(Op.SSTORE(0, 1))
104102
target2 = pre.deploy_contract(Op.SSTORE(0, 1))
105103

106-
# Recipient may be a nonexistent account (e.g., EIP-7708 transfer
107-
# log tests), so cover one NEW_ACCOUNT state charge.
108104
return Transaction(
109105
ty=4,
110106
sender=sender,
111107
max_fee_per_gas=10**10,
112108
max_priority_fee_per_gas=10**9,
113-
gas_limit=500_000 + fork.gas_costs().NEW_ACCOUNT,
109+
gas_limit=500_000,
114110
data=b"\x00" * 200,
115111
access_list=[
116112
AccessList(address=0x4567, storage_keys=[1000, 2000, 3000]),

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
from ....base_fork import BaseFork
2020
from ....gas_costs import GasCosts
2121

22+
# EIP-8037 state byte sizes (mirrors EELS amsterdam/vm/gas.py).
23+
STATE_BYTES_PER_NEW_ACCOUNT = 120
24+
STATE_BYTES_PER_STORAGE_SET = 64
25+
STATE_BYTES_PER_AUTH_BASE = 23
26+
27+
# EIP-8037 regular gas base costs.
28+
PER_AUTH_BASE_COST = 7_500
29+
REGULAR_GAS_CREATE = 9_000
30+
2231

2332
class EIP8037(BaseFork):
2433
"""EIP-8037 class."""
@@ -33,7 +42,6 @@ def cost_per_state_byte(cls) -> int:
3342
@classmethod
3443
def sstore_state_gas(cls) -> int:
3544
"""Return state gas for a zero-to-nonzero SSTORE (EIP-8037)."""
36-
STATE_BYTES_PER_STORAGE_SET = 64 # noqa: N806
3745
return STATE_BYTES_PER_STORAGE_SET * cls.cost_per_state_byte()
3846

3947
@classmethod
@@ -57,13 +65,6 @@ def gas_costs(cls) -> GasCosts:
5765
"""
5866
cpsb = cls.cost_per_state_byte()
5967
parent = super(EIP8037, cls).gas_costs()
60-
# EIP-8037 state byte sizes (EELS amsterdam/vm/gas.py)
61-
STATE_BYTES_PER_STORAGE_SET = 64 # noqa: N806
62-
STATE_BYTES_PER_NEW_ACCOUNT = 120 # noqa: N806
63-
STATE_BYTES_PER_AUTH_BASE = 23 # noqa: N806
64-
# EIP-8037 regular gas base costs
65-
PER_AUTH_BASE_COST = 7_500 # noqa: N806
66-
REGULAR_GAS_CREATE = 9_000 # noqa: N806
6768
new_acct = STATE_BYTES_PER_NEW_ACCOUNT * cpsb
6869
return replace(
6970
parent,
@@ -258,8 +259,6 @@ def transaction_intrinsic_state_gas(
258259
- Auth: (NEW_ACCOUNT + AUTH_BASE) * cpsb
259260
"""
260261
cpsb = cls.cost_per_state_byte()
261-
STATE_BYTES_PER_NEW_ACCOUNT = 120 # noqa: N806
262-
STATE_BYTES_PER_AUTH_BASE = 23 # noqa: N806
263262
state_gas = 0
264263
if contract_creation:
265264
state_gas += STATE_BYTES_PER_NEW_ACCOUNT * cpsb
@@ -324,7 +323,7 @@ def _calculate_sstore_state_gas(
324323
and current_value != new_value
325324
and original_value == 0
326325
):
327-
return 64 * cpsb
326+
return STATE_BYTES_PER_STORAGE_SET * cpsb
328327
return 0
329328

330329
@classmethod
@@ -384,7 +383,7 @@ def _calculate_sstore_state_refund(
384383
if current_value != new_value:
385384
if original_value == new_value:
386385
if original_value == 0:
387-
return 64 * cpsb
386+
return STATE_BYTES_PER_STORAGE_SET * cpsb
388387
return 0
389388

390389
@classmethod
@@ -412,9 +411,11 @@ def _calculate_selfdestruct_state_refund(
412411
]
413412
state_refund = 0
414413
if self_destructed_account:
415-
state_refund = 120 * cpsb
414+
state_refund = STATE_BYTES_PER_NEW_ACCOUNT * cpsb
416415
state_refund += (
417-
64 * cpsb * self_destructed_account_storage_slot_count
416+
STATE_BYTES_PER_STORAGE_SET
417+
* cpsb
418+
* self_destructed_account_storage_slot_count
418419
)
419420
state_refund += cpsb * self_destructed_account_code_deposit
420421
return state_refund

src/ethereum/forks/amsterdam/vm/eoa_delegation.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,9 @@ def set_delegation(message: Message) -> Uint:
205205
if authority_nonce != auth.nonce:
206206
continue
207207

208-
# For existing accounts no account creation happens, so the
209-
# NEW_ACCOUNT portion of the auth's intrinsic state cost is
210-
# refunded. The reservoir credit lowers `tx_gas_used` for the
211-
# sender; debiting `intrinsic_state_gas` lowers
212-
# `block_state_gas_used` so the block accounting matches the
213-
# state work that actually occurred.
208+
# For existing accounts, no account creation needed.
209+
# Refund the account creation state gas to the reservoir.
210+
# intrinsic_state_gas is immutable after validation.
214211
if account_exists(tx_state, authority):
215212
refund = STATE_BYTES_PER_NEW_ACCOUNT * COST_PER_STATE_BYTE
216213
message.state_gas_reservoir += refund

tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_selfdestruct_to_self_same_tx(
8383
state_test: StateTestFiller,
8484
env: Environment,
8585
pre: Alloc,
86+
fork: Fork,
8687
sender: EOA,
8788
contract_balance: int,
8889
create_opcode: Op,
@@ -125,7 +126,9 @@ def test_selfdestruct_to_self_same_tx(
125126
sender=sender,
126127
to=factory,
127128
value=contract_balance,
128-
gas_limit=300_000,
129+
# Same-tx CREATE+SELFDESTRUCT charges NEW_ACCOUNT state gas
130+
# under EIP-8037 (0 otherwise).
131+
gas_limit=200_000 + fork.gas_costs().NEW_ACCOUNT,
129132
expected_receipt=TransactionReceipt(logs=expected_logs),
130133
)
131134

@@ -144,6 +147,7 @@ def test_selfdestruct_to_different_address_same_tx(
144147
state_test: StateTestFiller,
145148
env: Environment,
146149
pre: Alloc,
150+
fork: Fork,
147151
sender: EOA,
148152
contract_balance: int,
149153
create_opcode: Op,
@@ -189,7 +193,9 @@ def test_selfdestruct_to_different_address_same_tx(
189193
sender=sender,
190194
to=factory,
191195
value=contract_balance,
192-
gas_limit=300_000,
196+
# Same-tx CREATE+SELFDESTRUCT charges NEW_ACCOUNT state gas
197+
# under EIP-8037 (0 otherwise).
198+
gas_limit=200_000 + fork.gas_costs().NEW_ACCOUNT,
193199
expected_receipt=TransactionReceipt(logs=expected_logs),
194200
)
195201

@@ -222,6 +228,7 @@ def test_selfdestruct_same_tx_via_call(
222228
state_test: StateTestFiller,
223229
env: Environment,
224230
pre: Alloc,
231+
fork: Fork,
225232
sender: EOA,
226233
to_self: bool,
227234
call_twice: bool,
@@ -303,7 +310,9 @@ def test_selfdestruct_same_tx_via_call(
303310
sender=sender,
304311
to=factory,
305312
value=0,
306-
gas_limit=300_000,
313+
# CREATE-then-CALL with same-tx SELFDESTRUCT charges
314+
# NEW_ACCOUNT state gas under EIP-8037 (0 otherwise).
315+
gas_limit=200_000 + fork.gas_costs().NEW_ACCOUNT,
307316
expected_receipt=TransactionReceipt(logs=expected_logs),
308317
)
309318

tests/amsterdam/eip7708_eth_transfer_logs/test_fork_transition.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,14 @@ def test_burn_log_at_fork_transition(
119119
beneficiary: Account(balance=contract_balance * 3),
120120
}
121121

122-
# Same-tx CREATE+SELFDESTRUCT charges NEW_ACCOUNT state gas; the
123-
# pre-transition block has plenty of headroom. `fork` is a
124-
# TransitionFork here, so resolve to the post-transition fork
125-
# (timestamp 15_001) where the larger NEW_ACCOUNT applies.
126-
post_fork = fork.fork_at(timestamp=15_001)
122+
# `fork` is a TransitionFork here; resolve to the post-transition
123+
# fork (where the larger NEW_ACCOUNT applies) so the gas budget
124+
# covers the same-tx CREATE+SELFDESTRUCT on the post-transition
125+
# block. The pre-transition block has plenty of headroom.
126+
pre_transition_timestamp = 14_999
127+
transition_timestamp = 15_000
128+
post_transition_timestamp = 15_001
129+
post_fork = fork.fork_at(timestamp=post_transition_timestamp)
127130
gas_limit = 200_000 + post_fork.gas_costs().NEW_ACCOUNT
128131
blocks = [
129132
Block(
@@ -137,7 +140,13 @@ def test_burn_log_at_fork_transition(
137140
)
138141
],
139142
)
140-
for i, ts in enumerate([14_999, 15_000, 15_001])
143+
for i, ts in enumerate(
144+
[
145+
pre_transition_timestamp,
146+
transition_timestamp,
147+
post_transition_timestamp,
148+
]
149+
)
141150
]
142151

143152
blockchain_test(pre=pre, blocks=blocks, post=post)

tests/amsterdam/eip7708_eth_transfer_logs/test_transfer_logs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,16 +1095,20 @@ def test_transfer_with_all_tx_types(
10951095
state_test: StateTestFiller,
10961096
env: Environment,
10971097
pre: Alloc,
1098+
fork: Fork,
10981099
sender: EOA,
10991100
typed_transaction: Transaction,
11001101
) -> None:
11011102
"""Test that ETH transfers emit logs for all transaction types."""
11021103
recipient = pre.nonexistent_account()
11031104
transfer_amount = 1000
11041105

1106+
# Sending value to a nonexistent recipient charges NEW_ACCOUNT
1107+
# state gas under EIP-8037 (0 otherwise).
11051108
tx = typed_transaction.copy(
11061109
to=recipient,
11071110
value=transfer_amount,
1111+
gas_limit=typed_transaction.gas_limit + fork.gas_costs().NEW_ACCOUNT,
11081112
expected_receipt=TransactionReceipt(
11091113
logs=[transfer_log(sender, recipient, transfer_amount)]
11101114
),

tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_cross_index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
BlockchainTestFiller,
2323
Bytecode,
2424
Fork,
25+
Header,
2526
Op,
2627
Transaction,
2728
)
@@ -275,9 +276,17 @@ def test_bal_noop_write_filtering(
275276
}
276277
)
277278

279+
# Header `gas_used = max(regular, state)` for the single tx; the
280+
# SSTORE metadata pins each transition so `regular_cost`/`state_cost`
281+
# return the actual fork-priced amount.
282+
expected_regular = intrinsic_cost + test_code.regular_cost(fork)
283+
expected_state = test_code.state_cost(fork)
278284
block = Block(
279285
txs=[tx],
280286
expected_block_access_list=expected_block_access_list,
287+
header_verify=Header(
288+
gas_used=max(expected_regular, expected_state),
289+
),
281290
)
282291

283292
blockchain_test(

tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,12 +2466,18 @@ def test_create_collision_burned_gas_counted_in_block_regular(
24662466

24672467
# CPSB-agnostic baseline: block_state_gas is zero for this tx, so
24682468
# header.gas_used equals the regular-gas total. The forwarded
2469-
# create_message_gas is `(gas_limit - NEW_ACCOUNT) * 63 // 64`,
2470-
# plus a CPSB-independent constant covering intrinsic + factory
2471-
# opcodes. A mutation that drops the burned create_message_gas
2472-
# from regular accounting would reduce this value.
2469+
# create_message_gas is `(gas_limit - NEW_ACCOUNT) * 63 // 64`
2470+
# (the regular gas the inner frame burns on collision); the
2471+
# remaining cpsb-independent term captures intrinsic + factory
2472+
# frame regular gas + the parts of CREATE not absorbed by the
2473+
# 63/64 forwarding split. A mutation that drops the burned
2474+
# create_message_gas from regular accounting would reduce this
2475+
# value.
24732476
new_account = fork.gas_costs().NEW_ACCOUNT
2474-
baseline_gas_used = (gas_limit - new_account) * 63 // 64 + 472
2477+
cpsb_independent_overhead = 472
2478+
baseline_gas_used = (
2479+
gas_limit - new_account
2480+
) * 63 // 64 + cpsb_independent_overhead
24752481

24762482
blockchain_test(
24772483
pre=pre,

tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ def test_multi_block_beacon_root_timestamp_calls(
451451
all_timestamps: List[int] = []
452452

453453
sender = pre.fund_eoa()
454+
intrinsic_calc = fork.transaction_intrinsic_cost_calculator()
454455

455456
for timestamp, beacon_root, _i in zip(
456457
timestamps,
@@ -509,7 +510,6 @@ def test_multi_block_beacon_root_timestamp_calls(
509510
post[current_call_account_address] = Account(
510511
storage=current_call_account_expected_storage,
511512
)
512-
intrinsic_calc = fork.transaction_intrinsic_cost_calculator()
513513
# Bytecode's regular+state cost + N forwarded call_gas envelopes
514514
# (one per `t` in all_timestamps) + EIP-1706 stipend slack.
515515
block_gas_limit = (

tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,10 @@ def test_blobhash_opcode_contexts(
313313
case _:
314314
raise Exception(f"Unknown test case {test_case}")
315315

316-
# The 500k figure pre-dates EIP-8037 and was empirically chosen to cover
317-
# all branches of this test (simple SSTOREs through to CREATE/CREATE2
318-
# initcode + deploy). Under EIP-8037 each branch's per-blob SSTOREs add a
319-
# `sstore_state_gas()` of state work; bump by that per blob so the budget
320-
# remains a precise envelope rather than a rough magic number.
316+
# Budget covers all branches (simple SSTOREs, CREATE / CREATE2
317+
# initcode + deploy) plus per-blob SSTOREs whose state cost
318+
# scales with cpsb under EIP-8037 (`sstore_state_gas()` is 0
319+
# otherwise).
321320
gas_limit = 500_000 + max_blobs_per_tx * fork.sstore_state_gas()
322321
state_test(
323322
pre=pre,

0 commit comments

Comments
 (0)