Skip to content

Commit 546a7e6

Browse files
feat(fork-types): add RegularGas and StateGas NewType wrappers to amsterdam EVM
Applies the RegularGas and StateGas NewType wrappers (introduced in fork_types.py) to all EVM gas fields and constants across the amsterdam fork, preventing accidental mixing of state-gas with regular-gas. - Types all GasCosts constants, ExtendMemory.cost, MessageCallGas fields - Types Evm.gas_left/state_gas_left/regular_gas_used/state_gas_spilled - Types Message.gas/state_gas_reservoir and TransactionEnvironment fields - Types MessageCallOutput gas fields and GenericCall parameters - Updates max_message_call_gas and calculate_delegation_cost return types Closes #3015
1 parent 80cc337 commit 546a7e6

6 files changed

Lines changed: 242 additions & 207 deletions

File tree

src/ethereum/forks/amsterdam/fork.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@
5959
TransactionTypeContractCreationError,
6060
WrongChainIdError,
6161
)
62-
from .fork_types import Authorization, BlockAccessIndex, VersionedHash
62+
from .fork_types import (
63+
Authorization,
64+
BlockAccessIndex,
65+
RegularGas,
66+
StateGas,
67+
VersionedHash,
68+
)
6369
from .requests import (
6470
BUILDER_DEPOSIT_REQUEST_TYPE,
6571
BUILDER_EXIT_REQUEST_TYPE,
@@ -795,8 +801,8 @@ def process_unchecked_system_transaction(
795801
recipient=target_address,
796802
value=U256(0),
797803
gas_price=block_env.base_fee_per_gas,
798-
gas=SYSTEM_TRANSACTION_GAS,
799-
state_gas_reservoir=(
804+
gas=RegularGas(SYSTEM_TRANSACTION_GAS),
805+
state_gas_reservoir=StateGas(
800806
StateGasCosts.STORAGE_SET * SYSTEM_MAX_SSTORES_PER_CALL
801807
),
802808
access_list_addresses=set(),
@@ -806,17 +812,17 @@ def process_unchecked_system_transaction(
806812
authorizations=(),
807813
index_in_block=None,
808814
tx_hash=None,
809-
intrinsic_regular_gas=Uint(0),
810-
intrinsic_state_gas=Uint(0),
815+
intrinsic_regular_gas=RegularGas(Uint(0)),
816+
intrinsic_state_gas=StateGas(Uint(0)),
811817
)
812818

813819
system_tx_message = Message(
814820
block_env=block_env,
815821
tx_env=tx_env,
816822
caller=SYSTEM_ADDRESS,
817823
target=target_address,
818-
gas=SYSTEM_TRANSACTION_GAS,
819-
state_gas_reservoir=(
824+
gas=RegularGas(SYSTEM_TRANSACTION_GAS),
825+
state_gas_reservoir=StateGas(
820826
StateGasCosts.STORAGE_SET * SYSTEM_MAX_SSTORES_PER_CALL
821827
),
822828
value=U256(0),
@@ -1061,8 +1067,8 @@ def process_transaction(
10611067
# budget) and state_gas_reservoir.
10621068
execution_gas = tx.gas - intrinsic_gas
10631069
regular_gas_budget = TX_MAX_GAS_LIMIT - intrinsic.regular
1064-
gas = min(regular_gas_budget, execution_gas)
1065-
state_gas_reservoir = Uint(execution_gas - gas)
1070+
gas = RegularGas(min(regular_gas_budget, execution_gas))
1071+
state_gas_reservoir = StateGas(execution_gas - gas)
10661072

10671073
increment_nonce(tx_state, sender)
10681074

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from ..block_access_lists import BlockAccessList, BlockAccessListBuilder
2828
from ..blocks import Log, Receipt, Withdrawal
29-
from ..fork_types import Authorization, StateGas, VersionedHash
29+
from ..fork_types import Authorization, RegularGas, StateGas, VersionedHash
3030
from ..state_tracker import BlockState, TransactionState
3131
from ..transactions import LegacyTransaction
3232

@@ -123,17 +123,17 @@ class TransactionEnvironment:
123123
recipient: Bytes0 | Address
124124
value: U256
125125
gas_price: Uint
126-
gas: Uint
127-
state_gas_reservoir: Uint
126+
gas: RegularGas
127+
state_gas_reservoir: StateGas
128128
access_list_addresses: Set[Address]
129129
access_list_storage_keys: Set[Tuple[Address, Bytes32]]
130130
state: TransactionState
131131
blob_versioned_hashes: Tuple[VersionedHash, ...]
132132
authorizations: Tuple[Authorization, ...]
133133
index_in_block: Optional[Uint]
134134
tx_hash: Optional[Hash32]
135-
intrinsic_regular_gas: Uint
136-
intrinsic_state_gas: Uint
135+
intrinsic_regular_gas: RegularGas
136+
intrinsic_state_gas: StateGas
137137

138138

139139
@final
@@ -148,8 +148,8 @@ class Message:
148148
caller: Address
149149
target: Bytes0 | Address
150150
current_target: Address
151-
gas: Uint
152-
state_gas_reservoir: Uint
151+
gas: RegularGas
152+
state_gas_reservoir: StateGas
153153
value: U256
154154
data: Bytes
155155
code_address: Optional[Address]
@@ -172,8 +172,8 @@ class Evm:
172172
stack: List[U256]
173173
memory: bytearray
174174
code: Bytes
175-
gas_left: Uint
176-
state_gas_left: Uint
175+
gas_left: RegularGas
176+
state_gas_left: StateGas
177177
valid_jump_destinations: Set[Uint]
178178
logs: Tuple[Log, ...]
179179
refund_counter: int
@@ -185,8 +185,8 @@ class Evm:
185185
error: Optional[EthereumException]
186186
accessed_addresses: Set[Address]
187187
accessed_storage_keys: Set[Tuple[Address, Bytes32]]
188-
regular_gas_used: Uint = Uint(0)
189-
state_gas_spilled: Uint = Uint(0)
188+
regular_gas_used: RegularGas = RegularGas(Uint(0))
189+
state_gas_spilled: StateGas = StateGas(Uint(0))
190190

191191

192192
def credit_state_gas_refund(evm: Evm, amount: StateGas) -> None:
@@ -206,10 +206,10 @@ def credit_state_gas_refund(evm: Evm, amount: StateGas) -> None:
206206
The refund amount to credit.
207207
208208
"""
209-
from_gas_left = min(amount, evm.state_gas_spilled)
210-
evm.gas_left += from_gas_left
211-
evm.state_gas_spilled -= from_gas_left
212-
evm.state_gas_left += amount - from_gas_left
209+
from_gas_left = StateGas(min(amount, evm.state_gas_spilled))
210+
evm.gas_left = RegularGas(evm.gas_left + Uint(from_gas_left))
211+
evm.state_gas_spilled = StateGas(evm.state_gas_spilled - from_gas_left)
212+
evm.state_gas_left = StateGas(evm.state_gas_left + amount - from_gas_left)
213213

214214

215215
def incorporate_child_on_success(evm: Evm, child_evm: Evm) -> None:
@@ -224,15 +224,21 @@ def incorporate_child_on_success(evm: Evm, child_evm: Evm) -> None:
224224
The child evm to incorporate.
225225
226226
"""
227-
evm.gas_left += child_evm.gas_left
228-
evm.state_gas_left += child_evm.state_gas_left
229-
evm.state_gas_spilled += child_evm.state_gas_spilled
227+
evm.gas_left = RegularGas(evm.gas_left + child_evm.gas_left)
228+
evm.state_gas_left = StateGas(
229+
evm.state_gas_left + child_evm.state_gas_left
230+
)
231+
evm.state_gas_spilled = StateGas(
232+
evm.state_gas_spilled + child_evm.state_gas_spilled
233+
)
230234
evm.logs += child_evm.logs
231235
evm.refund_counter += child_evm.refund_counter
232236
evm.accounts_to_delete.update(child_evm.accounts_to_delete)
233237
evm.accessed_addresses.update(child_evm.accessed_addresses)
234238
evm.accessed_storage_keys.update(child_evm.accessed_storage_keys)
235-
evm.regular_gas_used += child_evm.regular_gas_used
239+
evm.regular_gas_used = RegularGas(
240+
evm.regular_gas_used + child_evm.regular_gas_used
241+
)
236242

237243

238244
def refill_frame_state_gas(evm: Evm) -> None:
@@ -249,9 +255,9 @@ def refill_frame_state_gas(evm: Evm) -> None:
249255
The frame whose state gas is rolled back.
250256
251257
"""
252-
evm.gas_left += evm.state_gas_spilled
258+
evm.gas_left = RegularGas(evm.gas_left + Uint(evm.state_gas_spilled))
253259
evm.state_gas_left = evm.message.state_gas_reservoir
254-
evm.state_gas_spilled = Uint(0)
260+
evm.state_gas_spilled = StateGas(Uint(0))
255261

256262

257263
def frame_state_gas_used(evm: Evm) -> int:
@@ -298,9 +304,13 @@ def incorporate_child_on_error(
298304
The child evm to incorporate.
299305
300306
"""
301-
evm.gas_left += child_evm.gas_left
302-
evm.state_gas_left += child_evm.state_gas_left
303-
evm.regular_gas_used += child_evm.regular_gas_used
307+
evm.gas_left = RegularGas(evm.gas_left + child_evm.gas_left)
308+
evm.state_gas_left = StateGas(
309+
evm.state_gas_left + child_evm.state_gas_left
310+
)
311+
evm.regular_gas_used = RegularGas(
312+
evm.regular_gas_used + child_evm.regular_gas_used
313+
)
304314

305315

306316
def emit_transfer_log(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ethereum.exceptions import InvalidBlock, InvalidSignatureError
1414
from ethereum.state import Address
1515

16-
from ..fork_types import Authorization, StateGas
16+
from ..fork_types import Authorization, RegularGas, StateGas
1717
from ..state_tracker import (
1818
account_exists,
1919
get_account,
@@ -123,7 +123,7 @@ def recover_authority(authorization: Authorization) -> Address:
123123

124124
def calculate_delegation_cost(
125125
evm: Evm, address: Address
126-
) -> Tuple[bool, Address, Uint]:
126+
) -> Tuple[bool, Address, RegularGas]:
127127
"""
128128
Get the delegation address and the cost of access from the address.
129129
@@ -136,7 +136,7 @@ def calculate_delegation_cost(
136136
137137
Returns
138138
-------
139-
delegation : `Tuple[bool, Address, Uint]`
139+
delegation : `Tuple[bool, Address, RegularGas]`
140140
The delegation address and access gas cost.
141141
142142
"""
@@ -145,7 +145,7 @@ def calculate_delegation_cost(
145145
code = get_code(tx_state, get_account(tx_state, address).code_hash)
146146

147147
if not is_valid_delegation(code):
148-
return False, address, Uint(0)
148+
return False, address, RegularGas(Uint(0))
149149

150150
delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
151151

0 commit comments

Comments
 (0)