Skip to content

Commit af5d4ef

Browse files
committed
chore(spec-specs): cleanup BAL logic; organize gas check for SSTORE
1 parent 06d9476 commit af5d4ef

File tree

6 files changed

+36
-50
lines changed

6 files changed

+36
-50
lines changed

src/ethereum/forks/amsterdam/fork.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,9 +1093,6 @@ def process_transaction(
10931093

10941094
block_output.block_logs += tx_output.logs
10951095

1096-
# EIP-7928: Handle in-transaction self-destruct BEFORE normalization
1097-
# Destroy accounts first so normalization sees correct post-tx state
1098-
# Only accounts created in same tx are in accounts_to_delete per EIP-6780
10991096
for address in tx_output.accounts_to_delete:
11001097
destroy_account(block_env.state, address)
11011098

@@ -1130,7 +1127,6 @@ def process_withdrawals(
11301127
"""
11311128
Increase the balance of the withdrawing account.
11321129
"""
1133-
# Get block access index for withdrawals (post-exec phase)
11341130
block_access_index = get_block_access_index(block_env.block_state_changes)
11351131

11361132
# Capture pre-state for withdrawal balance filtering

src/ethereum/forks/amsterdam/state.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,7 @@ def increase_recipient_balance(recipient: Account) -> None:
538538
modify_state(state, recipient_address, increase_recipient_balance)
539539

540540

541-
def set_account_balance(
542-
state: State,
543-
address: Address,
544-
amount: U256,
545-
) -> None:
541+
def set_account_balance(state: State, address: Address, amount: U256) -> None:
546542
"""
547543
Sets the balance of an account.
548544

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,17 @@ def set_delegation(message: Message) -> U256:
261261
message.block_env.block_state_changes
262262
)
263263

264-
# Capture pre-code before any changes (first-write-wins)
264+
# EIP-7928: Capture pre-code before any changes
265265
capture_pre_code(tx_frame, authority, authority_code)
266266

267-
# Set delegation code
268267
set_authority_code(state, authority, code_to_set)
269268

270-
# Track code change if different from current
271269
if authority_code != code_to_set:
270+
# Track code change if different from current
272271
track_code_change(
273272
tx_frame, authority, code_to_set, block_access_index
274273
)
275274

276-
# Track nonce increment
277275
increment_nonce(state, authority)
278276
nonce_after = get_account(state, authority).nonce
279277
track_nonce_change(

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
track_storage_write,
2828
)
2929
from .. import Evm
30-
from ..exceptions import OutOfGasError, WriteInStaticContext
30+
from ..exceptions import WriteInStaticContext
3131
from ..gas import (
3232
GAS_CALL_STIPEND,
3333
GAS_COLD_SLOAD,
@@ -56,25 +56,26 @@ def sload(evm: Evm) -> None:
5656
key = pop(evm.stack).to_be_bytes32()
5757

5858
# GAS
59-
gas_cost = (
60-
GAS_WARM_ACCESS
61-
if (evm.message.current_target, key) in evm.accessed_storage_keys
62-
else GAS_COLD_SLOAD
63-
)
64-
check_gas(evm, gas_cost)
65-
if (evm.message.current_target, key) not in evm.accessed_storage_keys:
66-
evm.accessed_storage_keys.add((evm.message.current_target, key))
67-
track_storage_read(
68-
evm.state_changes,
59+
is_cold_access = (
6960
evm.message.current_target,
7061
key,
71-
)
62+
) not in evm.accessed_storage_keys
63+
gas_cost = GAS_COLD_SLOAD if is_cold_access else GAS_WARM_ACCESS
64+
7265
charge_gas(evm, gas_cost)
7366

7467
# OPERATION
7568
state = evm.message.block_env.state
7669
value = get_storage(state, evm.message.current_target, key)
7770

71+
if is_cold_access:
72+
evm.accessed_storage_keys.add((evm.message.current_target, key))
73+
track_storage_read(
74+
evm.state_changes,
75+
evm.message.current_target,
76+
key,
77+
)
78+
7879
push(evm.stack, value)
7980

8081
# PROGRAM COUNTER
@@ -94,19 +95,14 @@ def sstore(evm: Evm) -> None:
9495
# STACK
9596
key = pop(evm.stack).to_be_bytes32()
9697
new_value = pop(evm.stack)
97-
if evm.gas_left <= GAS_CALL_STIPEND:
98-
raise OutOfGasError
9998

100-
# Check static context before accessing storage
99+
# check we have at least the stipend gas
100+
check_gas(evm, GAS_CALL_STIPEND + Uint(1))
101+
102+
# check static context before accessing storage
101103
if evm.message.is_static:
102104
raise WriteInStaticContext
103105

104-
state = evm.message.block_env.state
105-
original_value = get_storage_original(
106-
state, evm.message.current_target, key
107-
)
108-
current_value = get_storage(state, evm.message.current_target, key)
109-
110106
# GAS
111107
gas_cost = Uint(0)
112108
is_cold_access = (
@@ -117,16 +113,15 @@ def sstore(evm: Evm) -> None:
117113
if is_cold_access:
118114
gas_cost += GAS_COLD_SLOAD
119115

120-
if original_value == current_value and current_value != new_value:
121-
if original_value == 0:
122-
gas_cost += GAS_STORAGE_SET
123-
else:
124-
gas_cost += GAS_STORAGE_UPDATE - GAS_COLD_SLOAD
125-
else:
126-
gas_cost += GAS_WARM_ACCESS
116+
state = evm.message.block_env.state
117+
original_value = get_storage_original(
118+
state, evm.message.current_target, key
119+
)
120+
current_value = get_storage(state, evm.message.current_target, key)
121+
122+
if is_cold_access:
123+
evm.accessed_storage_keys.add((evm.message.current_target, key))
127124

128-
# Track storage access BEFORE checking gas (EIP-7928)
129-
# Even if we run out of gas, the access attempt should be tracked
130125
capture_pre_storage(
131126
evm.message.tx_env.state_changes,
132127
evm.message.current_target,
@@ -138,10 +133,14 @@ def sstore(evm: Evm) -> None:
138133
evm.message.current_target,
139134
key,
140135
)
141-
check_gas(evm, gas_cost)
142136

143-
if is_cold_access:
144-
evm.accessed_storage_keys.add((evm.message.current_target, key))
137+
if original_value == current_value and current_value != new_value:
138+
if original_value == 0:
139+
gas_cost += GAS_STORAGE_SET
140+
else:
141+
gas_cost += GAS_STORAGE_UPDATE - GAS_COLD_SLOAD
142+
else:
143+
gas_cost += GAS_WARM_ACCESS
145144

146145
charge_gas(evm, gas_cost)
147146

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ def generic_create(
130130
if account_has_code_or_nonce(
131131
state, contract_address
132132
) or account_has_storage(state, contract_address):
133-
# Track nonce increment even on collision
134133
increment_nonce(state, evm.message.current_target)
135134
nonce_after = get_account(state, evm.message.current_target).nonce
136135
track_nonce_change(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ def process_message_call(message: Message) -> MessageCallOutput:
145145
message.accessed_addresses.add(delegated_address)
146146
message.code = get_account(block_env.state, delegated_address).code
147147
message.code_address = delegated_address
148-
149-
# EIP-7928: Track delegation target when loaded as call target
150148
track_address(
151149
message.block_env.block_state_changes, delegated_address
152150
)
@@ -215,7 +213,6 @@ def process_create_message(message: Message) -> Evm:
215213
message.block_env.block_state_changes
216214
)
217215

218-
# Track nonce increment for contract creation
219216
increment_nonce(state, message.current_target)
220217
nonce_after = get_account(state, message.current_target).nonce
221218
track_nonce_change(
@@ -284,6 +281,7 @@ def process_message(message: Message) -> Evm:
284281
if message.depth > STACK_DEPTH_LIMIT:
285282
raise StackDepthLimitError("Stack depth limit reached")
286283

284+
# take snapshot of state before processing the message
287285
begin_transaction(state, transient_storage)
288286

289287
block_access_index = get_block_access_index(

0 commit comments

Comments
 (0)