Skip to content

Commit 0c56b70

Browse files
committed
re-factor validate_transaction
1 parent 5a32df1 commit 0c56b70

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

src/ethereum/prague/fork.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,12 @@
4646
state_root,
4747
)
4848
from .transactions import (
49-
FLOOR_CALLDATA_COST,
50-
TX_BASE_COST,
5149
AccessListTransaction,
5250
BlobTransaction,
5351
FeeMarketTransaction,
5452
LegacyTransaction,
5553
SetCodeTransaction,
5654
Transaction,
57-
calculate_intrinsic_cost,
5855
decode_transaction,
5956
encode_transaction,
6057
recover_sender,
@@ -955,8 +952,7 @@ def process_transaction(
955952
logs : `Tuple[ethereum.blocks.Log, ...]`
956953
Logs generated during execution.
957954
"""
958-
if not validate_transaction(tx):
959-
raise InvalidBlock
955+
intrinsic_gas, calldata_floor_gas_cost = validate_transaction(tx)
960956

961957
sender = env.origin
962958
sender_account = get_account(env.state, sender)
@@ -968,8 +964,6 @@ def process_transaction(
968964

969965
effective_gas_fee = tx.gas * env.gas_price
970966

971-
intrinsic_gas, tokens_in_calldata = calculate_intrinsic_cost(tx)
972-
973967
gas = tx.gas - intrinsic_gas
974968
increment_nonce(env.state, sender)
975969

@@ -1021,13 +1015,9 @@ def process_transaction(
10211015
)
10221016
execution_gas_used -= gas_refund
10231017

1024-
# EIP-7623 floor price (note: no EVM costs)
1025-
floor_gas_cost = Uint(
1026-
tokens_in_calldata * FLOOR_CALLDATA_COST + TX_BASE_COST
1027-
)
10281018
# Transactions with less execution_gas_used than the floor pay at the
10291019
# floor cost.
1030-
total_gas_used = max(execution_gas_used, floor_gas_cost)
1020+
total_gas_used = max(execution_gas_used, calldata_floor_gas_cost)
10311021

10321022
output.gas_left = tx.gas - total_gas_used
10331023
gas_refund_amount = output.gas_left * env.gas_price

src/ethereum/prague/transactions.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from ethereum.crypto.elliptic_curve import SECP256K1N, secp256k1_recover
1515
from ethereum.crypto.hash import Hash32, keccak256
16-
from ethereum.exceptions import InvalidSignatureError
16+
from ethereum.exceptions import InvalidBlock, InvalidSignatureError
1717

1818
from .exceptions import TransactionTypeError
1919
from .fork_types import Address, Authorization, VersionedHash
@@ -176,7 +176,7 @@ def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
176176
return tx
177177

178178

179-
def validate_transaction(tx: Transaction) -> bool:
179+
def validate_transaction(tx: Transaction) -> Tuple[Uint, Uint]:
180180
"""
181181
Verifies a transaction.
182182
@@ -198,21 +198,28 @@ def validate_transaction(tx: Transaction) -> bool:
198198
199199
Returns
200200
-------
201-
verified : `bool`
202-
True if the transaction can be executed, or False otherwise.
201+
intrinsic_gas : `ethereum.base_types.Uint`
202+
The intrinsic cost of the transaction.
203+
calldata_floor_gas_cost : `ethereum.base_types.Uint`
204+
The eip-7623 minimum gas cost charged to the transaction
205+
based on the calldata size.
206+
207+
Raises
208+
------
209+
InvalidBlock :
210+
If the transaction is not valid.
203211
"""
204212
from .vm.interpreter import MAX_CODE_SIZE
205213

206-
intrinsic_gas, tokens_in_calldata = calculate_intrinsic_cost(tx)
207-
gas_floor = Uint(tokens_in_calldata * FLOOR_CALLDATA_COST + TX_BASE_COST)
208-
if max(intrinsic_gas, gas_floor) > tx.gas:
209-
return False
214+
intrinsic_gas, calldata_floor_gas_cost = calculate_intrinsic_cost(tx)
215+
if max(intrinsic_gas, calldata_floor_gas_cost) > tx.gas:
216+
raise InvalidBlock
210217
if U256(tx.nonce) >= U256(U64.MAX_VALUE):
211-
return False
218+
raise InvalidBlock
212219
if tx.to == Bytes0(b"") and len(tx.data) > 2 * MAX_CODE_SIZE:
213-
return False
220+
raise InvalidBlock
214221

215-
return True
222+
return intrinsic_gas, calldata_floor_gas_cost
216223

217224

218225
def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
@@ -235,10 +242,11 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
235242
236243
Returns
237244
-------
238-
verified : `ethereum.base_types.Uint`
245+
intrinsic_gas : `ethereum.base_types.Uint`
239246
The intrinsic cost of the transaction.
240-
tokens_in_calldata : `ethereum.base_types.Uint`
241-
The eip-7623 calldata tokens used by the transaction.
247+
calldata_floor_gas_cost : `ethereum.base_types.Uint`
248+
The eip-7623 minimum gas cost used by the transaction
249+
based on the calldata size.
242250
"""
243251
from .vm.eoa_delegation import PER_EMPTY_ACCOUNT_COST
244252
from .vm.gas import init_code_cost
@@ -248,9 +256,13 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
248256
if byte == 0:
249257
zero_bytes += 1
250258

251-
tokens_in_calldata = zero_bytes + (len(tx.data) - zero_bytes) * 4
259+
tokens_in_calldata = Uint(zero_bytes + (len(tx.data) - zero_bytes) * 4)
260+
# EIP-7623 floor price (note: no EVM costs)
261+
calldata_floor_gas_cost = (
262+
tokens_in_calldata * FLOOR_CALLDATA_COST + TX_BASE_COST
263+
)
252264

253-
data_cost = Uint(tokens_in_calldata) * STANDARD_CALLDATA_TOKEN_COST
265+
data_cost = tokens_in_calldata * STANDARD_CALLDATA_TOKEN_COST
254266

255267
if tx.to == Bytes0(b""):
256268
create_cost = TX_CREATE_COST + init_code_cost(ulen(tx.data))
@@ -283,7 +295,7 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
283295
+ access_list_cost
284296
+ auth_cost
285297
),
286-
Uint(tokens_in_calldata),
298+
calldata_floor_gas_cost,
287299
)
288300

289301

0 commit comments

Comments
 (0)