Skip to content

Commit bb826c6

Browse files
committed
simplify txs
1 parent 7292acd commit bb826c6

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

src/ethereum_spec_tools/evm_tools/t8n/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def read_withdrawals(self, data: Any, t8n: "T8N") -> None:
196196
"""
197197
self.withdrawals = None
198198
if t8n.fork.is_after_fork("ethereum.shanghai"):
199-
raw_withdrawals = getattr(data, "withdrawals", None)
199+
raw_withdrawals = data.withdrawals
200200
if raw_withdrawals:
201201
def to_canonical_withdrawal(raw):
202202
return t8n.fork.Withdrawal(

src/ethereum_spec_tools/evm_tools/t8n/t8n_types.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dataclasses import dataclass
55
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple
66

7-
from ethereum_rlp import Simple, rlp
7+
from ethereum_rlp import rlp
88
from ethereum_types.bytes import Bytes, Bytes20, Bytes0
99
from ethereum.exceptions import StateWithEmptyAccount
1010
from ethereum_types.numeric import U8, U64, U256, Uint
@@ -110,13 +110,16 @@ def __init__(self, t8n: "T8N", stdin: List[Transaction] = None):
110110
self.all_txs = []
111111

112112
if stdin is None:
113-
self.data: Simple = []
113+
self.data: List[Transaction] = []
114114
else:
115115
self.data = stdin
116116

117117
for idx, raw_tx in enumerate(self.data):
118118
try:
119-
fork_tx = self.pydantic_to_fork_transaction(raw_tx)
119+
fork_tx = convert_pydantic_tx_to_canonical(
120+
raw_tx,
121+
self.t8n.fork,
122+
)
120123
self.transactions.append(fork_tx)
121124
self.successfully_parsed.append(idx)
122125
self.all_txs.append(fork_tx)
@@ -191,9 +194,6 @@ def parse_json_tx(self, raw_tx: Any) -> Any:
191194

192195
return transaction
193196

194-
def pydantic_to_fork_transaction(self, tx_model):
195-
return convert_pydantic_tx_to_canonical(tx_model, self.t8n.fork)
196-
197197
def sign_transaction(self, json_tx: Any) -> None:
198198
"""
199199
Sign a transaction. This function will be invoked if a `secretKey`
@@ -291,29 +291,16 @@ def convert_authorizations(auth_list):
291291
)
292292
return result
293293

294-
# determine tx type
295-
tx_type = getattr(tx, "ty", 0)
296-
if hasattr(fork, "SetCodeTransaction") and tx_type == 4:
297-
tx_cls = fork.SetCodeTransaction
298-
elif hasattr(fork, "BlobTransaction") and tx_type == 3:
299-
tx_cls = fork.BlobTransaction
300-
elif hasattr(fork, "FeeMarketTransaction") and tx_type == 2:
301-
tx_cls = fork.FeeMarketTransaction
302-
elif hasattr(fork, "AccessListTransaction") and tx_type == 1:
303-
tx_cls = fork.AccessListTransaction
304-
else:
305-
tx_cls = getattr(fork, "LegacyTransaction", None)
306-
if tx_cls is None:
307-
tx_cls = getattr(fork, "Transaction")
308-
309294
def to_bytes20(val):
310295
if val is None:
311296
return Bytes0()
312297
return Bytes20(val)
313298

314-
# build the canonical transaction
315-
if tx_cls.__name__ == "FeeMarketTransaction":
316-
return tx_cls(
299+
tx_type = tx.ty or 0
300+
301+
# SetCodeTransaction (Type 4)
302+
if hasattr(fork, "SetCodeTransaction") and tx_type == 4:
303+
return fork.SetCodeTransaction(
317304
chain_id=U64(tx.chain_id),
318305
nonce=U256(tx.nonce),
319306
max_priority_fee_per_gas=Uint(tx.max_priority_fee_per_gas or 0),
@@ -323,26 +310,34 @@ def to_bytes20(val):
323310
value=U256(tx.value),
324311
data=tx.data,
325312
access_list=convert_access_list(tx.access_list),
313+
authorizations=convert_authorizations(tx.authorization_list or []),
326314
y_parity=U256(tx.v),
327315
r=U256(tx.r),
328316
s=U256(tx.s),
329317
)
330-
elif tx_cls.__name__ == "AccessListTransaction":
331-
return tx_cls(
318+
319+
# BlobTransaction (Type 3)
320+
elif hasattr(fork, "BlobTransaction") and tx_type == 3:
321+
return fork.BlobTransaction(
332322
chain_id=U64(tx.chain_id),
333323
nonce=U256(tx.nonce),
334-
gas_price=Uint(tx.gas_price or 0),
324+
max_priority_fee_per_gas=Uint(tx.max_priority_fee_per_gas or 0),
325+
max_fee_per_gas=Uint(tx.max_fee_per_gas or 0),
335326
gas=Uint(tx.gas_limit),
336327
to=to_bytes20(tx.to),
337328
value=U256(tx.value),
338329
data=tx.data,
339330
access_list=convert_access_list(tx.access_list),
331+
max_fee_per_blob_gas=Uint(tx.max_fee_per_blob_gas or 0),
332+
blob_versioned_hashes=tx.blob_versioned_hashes or (),
340333
y_parity=U256(tx.v),
341334
r=U256(tx.r),
342335
s=U256(tx.s),
343336
)
344-
elif tx_cls.__name__ == "BlobTransaction":
345-
return tx_cls(
337+
338+
# FeeMarketTransaction (Type 2)
339+
elif hasattr(fork, "FeeMarketTransaction") and tx_type == 2:
340+
return fork.FeeMarketTransaction(
346341
chain_id=U64(tx.chain_id),
347342
nonce=U256(tx.nonce),
348343
max_priority_fee_per_gas=Uint(tx.max_priority_fee_per_gas or 0),
@@ -352,29 +347,33 @@ def to_bytes20(val):
352347
value=U256(tx.value),
353348
data=tx.data,
354349
access_list=convert_access_list(tx.access_list),
355-
max_fee_per_blob_gas=Uint(getattr(tx, "max_fee_per_blob_gas", 0)),
356-
blob_versioned_hashes=getattr(tx, "blob_versioned_hashes", ()),
357350
y_parity=U256(tx.v),
358351
r=U256(tx.r),
359352
s=U256(tx.s),
360353
)
361-
elif tx_cls.__name__ == "SetCodeTransaction":
362-
return tx_cls(
354+
355+
# AccessListTransaction (Type 1)
356+
elif hasattr(fork, "AccessListTransaction") and tx_type == 1:
357+
return fork.AccessListTransaction(
363358
chain_id=U64(tx.chain_id),
364359
nonce=U256(tx.nonce),
365-
max_priority_fee_per_gas=Uint(tx.max_priority_fee_per_gas or 0),
366-
max_fee_per_gas=Uint(tx.max_fee_per_gas or 0),
360+
gas_price=Uint(tx.gas_price or 0),
367361
gas=Uint(tx.gas_limit),
368362
to=to_bytes20(tx.to),
369363
value=U256(tx.value),
370364
data=tx.data,
371365
access_list=convert_access_list(tx.access_list),
372-
authorizations=convert_authorizations(getattr(tx, "authorization_list", ())),
373366
y_parity=U256(tx.v),
374367
r=U256(tx.r),
375368
s=U256(tx.s),
376369
)
370+
371+
# Legacy Transaction (Type 0)
377372
else:
373+
tx_cls = getattr(fork, "LegacyTransaction", None)
374+
if tx_cls is None:
375+
tx_cls = getattr(fork, "Transaction")
376+
378377
return tx_cls(
379378
nonce=U256(tx.nonce),
380379
gas_price=Uint(tx.gas_price or 0),

0 commit comments

Comments
 (0)