ethereum.forks.bogota.transactions

Transactions are atomic units of work created externally to Ethereum and submitted to be executed. If Ethereum is viewed as a state machine, transactions are the events that move between states.

IntrinsicGasCost

Intrinsic gas costs for a transaction, split by gas type.

42
@final
43
@dataclass
class IntrinsicGasCost:

execution

Execution gas (calldata, base cost, access list, etc.).

47
    execution: ExecutionGas

calldata_floor

Minimum gas cost based on calldata size per EIP-7623, including the access list data surcharge per EIP-7981.

50
    calldata_floor: ExecutionGas

BLOB_COUNT_LIMIT

Maximum number of blobs a single transaction may carry.

60
BLOB_COUNT_LIMIT = 6

VERSIONED_HASH_VERSION_KZG

Version byte that every blob versioned hash must start with.

65
VERSIONED_HASH_VERSION_KZG = b"\x01"

ACCESS_LIST_ADDRESS_FLOOR_TOKENS

Floor data tokens contributed by a single access list address per EIP-7981.

70
ACCESS_LIST_ADDRESS_FLOOR_TOKENS = Uint(80)

ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS

Floor data tokens contributed by a single access list storage key per EIP-7981.

78
ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS = Uint(128)

LegacyTransaction

Atomic operation performed on the block chain. This represents the original transaction format used before EIP-1559, EIP-2930, EIP-4844, and EIP-7702.

87
@final
88
@slotted_freezable
89
@dataclass
class LegacyTransaction:

nonce

A scalar value equal to the number of transactions sent by the sender.

102
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

107
    gas_price: Uint

gas

The maximum amount of gas that can be used by this transaction.

112
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

117
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

123
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

128
    data: Bytes

v

The recovery id of the signature.

134
    v: U256

r

The first part of the signature.

139
    r: U256

s

The second part of the signature.

144
    s: U256

Access

A mapping from account address to storage slots that are pre-warmed as part of a transaction.

150
@final
151
@slotted_freezable
152
@dataclass
class Access:

account

The address of the account that is accessed.

159
    account: Address

slots

A tuple of storage slots that are accessed in the account.

164
    slots: Tuple[Bytes32, ...]

AccessListTransaction

The transaction type added in EIP-2930 to support access lists.

This transaction type extends the legacy transaction with an access list and chain ID. The access list specifies which addresses and storage slots the transaction will access.

170
@final
171
@slotted_freezable
172
@dataclass
class AccessListTransaction:

chain_id

The ID of the chain on which this transaction is executed.

184
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

189
    nonce: U256

gas_price

The price of gas for this transaction.

194
    gas_price: Uint

gas

The maximum amount of gas that can be used by this transaction.

199
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

204
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

210
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

215
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

221
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

227
    y_parity: U256

r

The first part of the signature.

232
    r: U256

s

The second part of the signature.

237
    s: U256

FeeMarketTransaction

The transaction type added in EIP-1559.

This transaction type introduces a new fee market mechanism with two gas price parameters: max_priority_fee_per_gas and max_fee_per_gas.

243
@final
244
@slotted_freezable
245
@dataclass
class FeeMarketTransaction:

chain_id

The ID of the chain on which this transaction is executed.

256
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

261
    nonce: U256

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

266
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

271
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

277
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

282
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

288
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

293
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

299
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

305
    y_parity: U256

r

The first part of the signature.

310
    r: U256

s

The second part of the signature.

315
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

This transaction type extends the fee market transaction to support blob-carrying transactions.

321
@final
322
@slotted_freezable
323
@dataclass
class BlobTransaction:

chain_id

The ID of the chain on which this transaction is executed.

334
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

339
    nonce: U256

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

344
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

349
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

355
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

360
    to: Address

value

The amount of ether (in wei) to send with this transaction.

366
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

371
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

377
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

The maximum fee per blob gas that the sender is willing to pay.

383
    max_fee_per_blob_gas: U256

blob_versioned_hashes

A tuple of objects that represent the versioned hashes of the blobs included in the transaction.

388
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

394
    y_parity: U256

r

The first part of the signature.

399
    r: U256

s

The second part of the signature.

404
    s: U256

SetCodeTransaction

The transaction type added in EIP-7702.

This transaction type allows Ethereum Externally Owned Accounts (EOAs) to set code on their account, enabling them to act as smart contracts.

410
@final
411
@slotted_freezable
412
@dataclass
class SetCodeTransaction:

chain_id

The ID of the chain on which this transaction is executed.

423
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

428
    nonce: U64

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

433
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

438
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

444
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

449
    to: Address

value

The amount of ether (in wei) to send with this transaction.

455
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

460
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

466
    access_list: Tuple[Access, ...]

authorizations

A tuple of Authorization objects that specify what code the signer desires to execute in the context of their EOA.

472
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

478
    y_parity: U256

r

The first part of the signature.

483
    r: U256

s

The second part of the signature.

488
    s: U256

Transaction

Union type representing any valid transaction type.

494
Transaction = (
495
    LegacyTransaction
496
    | AccessListTransaction
497
    | FeeMarketTransaction
498
    | BlobTransaction
499
    | SetCodeTransaction
500
    | FrameTransaction
501
)

AccessListCapableTransaction

Transaction types that include an EIP-2930-style access list.

See has_access_list and Access for more details.

507
AccessListCapableTransaction = (
508
    AccessListTransaction
509
    | FeeMarketTransaction
510
    | BlobTransaction
511
    | SetCodeTransaction
512
)

FeeMarketCapableTransaction

Transaction types that include the EIP-1559-style fee structure.

Frame transactions carry the fee parameters nested in their fees list rather than as flat fields.

See FeeMarketTransaction for more details.

524
FeeMarketCapableTransaction = (
525
    FeeMarketTransaction
526
    | BlobTransaction
527
    | SetCodeTransaction
528
    | FrameTransaction
529
)

BlobCapableTransaction

Transaction types that can carry EIP-4844 blobs.

See BlobTransaction and FrameTransaction for more details.

544
BlobCapableTransaction = BlobTransaction | FrameTransaction

encode_transaction

Encode a transaction into its RLP or typed transaction format. Needed because non-legacy transactions aren't RLP.

Legacy transactions are returned as-is, while other transaction types are prefixed with their type identifier and RLP encoded.

def encode_transaction(​tx: Transaction​) -> LegacyTransaction | Bytes:
558
    <snip>
565
    if isinstance(tx, LegacyTransaction):
566
        return tx
567
    elif isinstance(tx, AccessListTransaction):
568
        return b"\x01" + rlp.encode(tx)
569
    elif isinstance(tx, FeeMarketTransaction):
570
        return b"\x02" + rlp.encode(tx)
571
    elif isinstance(tx, BlobTransaction):
572
        return b"\x03" + rlp.encode(tx)
573
    elif isinstance(tx, SetCodeTransaction):
574
        return b"\x04" + rlp.encode(tx)
575
    elif isinstance(tx, FrameTransaction):
576
        return b"\x06" + rlp.encode(tx)
577
    else:
578
        raise Exception(f"Unable to encode transaction of type {type(tx)}")

decode_transaction

Decode a transaction from its RLP or typed transaction format. Needed because non-legacy transactions aren't RLP.

Accept a LegacyTransaction object (returned as-is) or raw bytes.

EIP-2718 states that the first byte distinguishes the format: [0x00, 0x7f] is a typed transaction, [0xc0, 0xfe] is a legacy transaction (RLP list prefix).

def decode_transaction(​tx: LegacyTransaction | Bytes​) -> Transaction:
582
    <snip>
593
    if isinstance(tx, Bytes):
594
        if len(tx) == 0:
595
            raise InvalidBlock("empty transaction")
596
        if tx[0] == 1:
597
            return rlp.decode_to(AccessListTransaction, tx[1:])
598
        elif tx[0] == 2:
599
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
600
        elif tx[0] == 3:
601
            return rlp.decode_to(BlobTransaction, tx[1:])
602
        elif tx[0] == 4:
603
            return rlp.decode_to(SetCodeTransaction, tx[1:])
604
        elif tx[0] == 6:
605
            return rlp.decode_to(FrameTransaction, tx[1:])
606
        elif 0xC0 <= tx[0] <= 0xFE:
607
            return rlp.decode_to(LegacyTransaction, tx)
608
        else:
609
            raise TransactionTypeError(tx[0])
610
    else:
611
        return tx

validate_transaction

Verifies a transaction.

The gas in a transaction gets used to pay for the intrinsic cost of operations, therefore if there is insufficient gas then it would not be possible to execute a transaction and it will be declared invalid.

Additionally, the nonce of a transaction must not equal or exceed the limit defined in EIP-2681. In practice, defining the limit as 2**64-1 has no impact because sending 2**64-1 transactions is improbable. It's not strictly impossible though, 2**64-1 transactions is the entire capacity of the Ethereum blockchain at 2022 gas limits for a little over 22 years.

Also, the code size of a contract creation transaction must be within limits of the protocol.

The gas limit of a transaction may not exceed TX_MAX_TOTAL_GAS_LIMIT (EIP-8037). TX_MAX_GAS_LIMIT (EIP-7825) bounds only the execution-gas a transaction can spend, so it is checked against the larger of the intrinsic execution cost and the EIP-7623 calldata floor rather than against the gas limit.

This function takes a transaction and gas_limit as parameters and returns the intrinsic gas costs for the transaction after validation. It throws an InsufficientTransactionGasError exception if the transaction does not provide enough gas to cover the intrinsic cost, and a NonceOverflowError exception if the nonce overflows. It also raises an InitCodeTooLargeError if the code size of a contract creation transaction exceeds the maximum allowed size, a TransactionGasLimitExceededError if the gas limit exceeds TX_MAX_TOTAL_GAS_LIMIT, and a PriorityFeeGreaterThanMaxFeeError if the maximum priority fee per gas of a fee market transaction exceeds its maximum fee per gas.

def validate_transaction(​tx: Transaction, ​​sender: Address​) -> IntrinsicGasCost:
615
    <snip>  # noqa: E501
657
    from ..vm.gas import GasCosts
658
    from ..vm.interpreter import MAX_INIT_CODE_SIZE
659
660
    assert not isinstance(tx, FrameTransaction)
661
662
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
663
        raise NonceOverflowError("Nonce too high")
664
665
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
666
        raise InitCodeTooLargeError("Code size too large")
667
668
    if tx.gas > GasCosts.TX_MAX_TOTAL_GAS_LIMIT:
669
        raise TransactionGasLimitExceededError("Gas limit too high")
670
671
    if isinstance(tx, FeeMarketCapableTransaction):
672
        if tx.max_fee_per_gas < tx.max_priority_fee_per_gas:
673
            raise PriorityFeeGreaterThanMaxFeeError(
674
                "priority fee greater than max fee"
675
            )
676
677
    if isinstance(tx, BlobTransaction):
678
        blob_count = len(tx.blob_versioned_hashes)
679
        if blob_count == 0:
680
            raise NoBlobDataError("no blob data in transaction")
681
        if blob_count > BLOB_COUNT_LIMIT:
682
            raise BlobCountExceededError(
683
                f"Tx has {blob_count} blobs. Max allowed: {BLOB_COUNT_LIMIT}"
684
            )
685
        for blob_versioned_hash in tx.blob_versioned_hashes:
686
            if blob_versioned_hash[0:1] != VERSIONED_HASH_VERSION_KZG:
687
                raise InvalidBlobVersionedHashError(
688
                    "invalid blob versioned hash"
689
                )
690
691
    if isinstance(tx, (BlobTransaction, SetCodeTransaction)):
692
        if not isinstance(tx.to, Address):
693
            raise TransactionTypeContractCreationError(tx)
694
695
    if isinstance(tx, SetCodeTransaction):
696
        if not any(tx.authorizations):
697
            raise EmptyAuthorizationListError("empty authorization list")
698
699
    intrinsic = calculate_intrinsic_cost(tx, sender)
700
    intrinsic_gas = Uint(intrinsic.execution)
701
    if intrinsic_gas > tx.gas:
702
        raise InsufficientTransactionGasError("Insufficient intrinsic gas")
703
    if intrinsic.calldata_floor > tx.gas:
704
        raise InsufficientTransactionGasError("Insufficient calldata floor")
705
    if intrinsic.execution > GasCosts.TX_MAX_GAS_LIMIT:
706
        raise InsufficientTransactionGasError(
707
            "Intrinsic execution gas exceeds TX_MAX_GAS_LIMIT"
708
        )
709
    if intrinsic.calldata_floor > GasCosts.TX_MAX_GAS_LIMIT:
710
        raise InsufficientTransactionGasError(
711
            "Intrinsic calldata floor exceeds TX_MAX_GAS_LIMIT"
712
        )
713
714
    return intrinsic

calculate_intrinsic_cost

Calculate the gas charged before execution starts and the data floor.

The intrinsic cost of the transaction is charged before execution has begun. Functions/operations in the EVM cost money to execute so this intrinsic cost is for the operations that need to be paid for as part of the transaction. Data transfer, for example, is part of this intrinsic cost. It costs ether to send data over the wire and that ether is accounted for in the intrinsic cost calculated in this function. This intrinsic cost must be calculated and paid for before execution in order for all operations to be implemented.

The intrinsic cost includes:

  1. Sender cost (TX_BASE).

  2. Recipient cost (COLD_ACCOUNT_ACCESS for a non-self-transfer call, or CREATE_ACCESS for a contract creation). The created account's NEW_ACCOUNT state gas is state-dependent and is charged at the top frame, not here.

  3. Value cost (TX_VALUE_COST for a non-self-transfer call) when tx.value > 0.

  4. Calldata cost (zero and non-zero bytes).

  5. Access list entry charges and the data surcharge (if applicable).

  6. Authorizations (if applicable): only the state-independent base cost (EXECUTION_PER_AUTH_BASE_COST) per tuple. The state-dependent account-creation and delegation-write costs are charged at the top frame by set_delegation.

Self-transfers (sender == tx.to) skip the recipient and value charges.

This function takes a transaction and its sender as parameters and returns the intrinsic execution gas cost and the minimum (floor) gas cost based on the calldata size and access list data surcharge. The surcharge is added to both costs, so it is charged regardless of which side determines the gas used. The floor is anchored on the execution-gas portion of items 1 to 3 above rather than TX_BASE alone, so it never undercuts the transaction's own intrinsic base.

def calculate_intrinsic_cost(​tx: Transaction, ​​sender: Address​) -> IntrinsicGasCost:
720
    <snip>
758
    from ..vm.gas import GasCosts, init_code_cost
759
760
    # Frame transactions never reach this function; their intrinsic cost
761
    # is calculated by `calculate_frame_transaction_intrinsic_cost`.
762
    assert not isinstance(tx, FrameTransaction)
763
764
    tokens_in_calldata = count_tokens_in_data(tx.data)
765
766
    data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD
767
768
    is_create = tx.to == Bytes0(b"")
769
    is_self_transfer = tx.to == sender
770
771
    recipient_execution_gas = Uint(0)
772
    init_code_gas = Uint(0)
773
    if is_create:
774
        recipient_execution_gas = GasCosts.CREATE_ACCESS
775
        init_code_gas = init_code_cost(ulen(tx.data))
776
    elif not is_self_transfer:
777
        recipient_execution_gas = GasCosts.COLD_ACCOUNT_ACCESS
778
        if tx.value > U256(0):
779
            recipient_execution_gas += GasCosts.TX_VALUE_COST
780
781
    access_list_cost = Uint(0)
782
    tokens_in_access_list = Uint(0)
783
    if has_access_list(tx):
784
        for access in tx.access_list:
785
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
786
            access_list_cost += (
787
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
788
            )
789
            tokens_in_access_list += ACCESS_LIST_ADDRESS_FLOOR_TOKENS
790
            tokens_in_access_list += (
791
                ulen(access.slots) * ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS
792
            )
793
794
    # Charge the access list data surcharge on both sides of the gas-used
795
    # maximum, independently of the existing per-entry access charges.
796
    access_list_data_cost = (
797
        tokens_in_access_list * GasCosts.TX_DATA_TOKEN_FLOOR
798
    )
799
800
    auth_cost = Uint(0)
801
    if isinstance(tx, SetCodeTransaction):
802
        auth_cost = GasCosts.EXECUTION_PER_AUTH_BASE_COST * ulen(
803
            tx.authorizations
804
        )
805
806
    # EIP-7976 floor tokens: all calldata bytes count uniformly.
807
    floor_tokens_in_calldata = ulen(tx.data) * Uint(4)
808
809
    # Decomposed execution-gas intrinsic base (EIP-2780), which also
810
    # anchors the calldata floor.
811
    base_execution_gas = GasCosts.TX_BASE + recipient_execution_gas
812
813
    # Floor gas cost (EIP-7623: minimum gas for data-heavy transactions).
814
    data_floor_gas_cost = (
815
        base_execution_gas
816
        + floor_tokens_in_calldata * GasCosts.TX_DATA_TOKEN_FLOOR
817
        + access_list_data_cost
818
    )
819
820
    return IntrinsicGasCost(
821
        execution=ExecutionGas(
822
            base_execution_gas
823
            + init_code_gas
824
            + data_cost
825
            + access_list_cost
826
            + access_list_data_cost
827
            + auth_cost
828
        ),
829
        calldata_floor=ExecutionGas(data_floor_gas_cost),
830
    )

count_tokens_in_data

Count the data tokens in arbitrary input bytes.

Zero bytes count as 1 token; non-zero bytes count as 4 tokens.

def count_tokens_in_data(​data: bytes​) -> Uint:
834
    <snip>
839
    num_zeros = Uint(data.count(0))
840
    num_non_zeros = ulen(data) - num_zeros
841
842
    return num_zeros + num_non_zeros * Uint(4)

calculate_effective_gas_price

Calculate the price per unit of gas the transaction actually pays.

A fee-market transaction pays the base fee plus a priority fee capped by both of its fee caps; its maximum fee must cover the base fee, or an InsufficientMaxFeePerGasError is raised. A transaction priced with a plain gas price pays that price outright, which must likewise cover the base fee, or the same error is raised.

Frame transactions never reach this function: they cap their total fee rather than their price per gas, and their price is derived from that budget in the frame flow.

def calculate_effective_gas_price(​tx: Transaction, ​​base_fee_per_gas: Uint​) -> Uint:
848
    <snip>
861
    assert not isinstance(tx, FrameTransaction)
862
863
    if isinstance(tx, FeeMarketCapableTransaction):
864
        max_fee_per_gas = tx.max_fee_per_gas
865
        max_priority_fee_per_gas = tx.max_priority_fee_per_gas
866
867
        if max_fee_per_gas < base_fee_per_gas:
868
            raise InsufficientMaxFeePerGasError(
869
                max_fee_per_gas, base_fee_per_gas
870
            )
871
872
        priority_fee_per_gas = min(
873
            max_priority_fee_per_gas,
874
            max_fee_per_gas - base_fee_per_gas,
875
        )
876
        return priority_fee_per_gas + base_fee_per_gas
877
878
    if tx.gas_price < base_fee_per_gas:
879
        raise InsufficientMaxFeePerGasError(tx.gas_price, base_fee_per_gas)
880
    return tx.gas_price

calculate_max_gas_fee

Calculate the largest execution-gas fee the transaction can incur: gas_limit priced at the transaction's fee cap.

Frame transactions never reach this function: their maximum cost is anchored on the derived max_gas and computed in the frame flow.

def calculate_max_gas_fee(​tx: Transaction, ​​gas_limit: Uint​) -> Uint:
884
    <snip>
891
    assert not isinstance(tx, FrameTransaction)
892
893
    if isinstance(tx, FeeMarketCapableTransaction):
894
        return gas_limit * tx.max_fee_per_gas
895
    return gas_limit * tx.gas_price

check_nonce

Check that the transaction's nonce equals the sender's next nonce.

def check_nonce(​tx: Transaction, ​​sender_nonce: Uint​) -> None:
899
    <snip>
902
    if sender_nonce > Uint(tx.nonce):
903
        raise NonceMismatchError("nonce too low")
904
    elif sender_nonce < Uint(tx.nonce):
905
        raise NonceMismatchError("nonce too high")

chain_id

Extract the chain identifier from a transaction. See EIP-155.

def chain_id(​tx: Transaction​) -> None | U64:
909
    <snip>
914
    if isinstance(tx, LegacyTransaction):
915
        if tx.v == 27 or tx.v == 28:
916
            return None
917
918
        if tx.v < U256(35):
919
            raise InvalidSignatureError("bad v")
920
921
        tx_chain_id = (tx.v - U256(35)) >> U256(1)
922
        if tx_chain_id > U256(U64.MAX_VALUE):
923
            raise InvalidSignatureError("bad v")
924
        return U64(tx_chain_id)
925
    else:
926
        return tx.chain_id

recover_sender

Extracts the sender address from a transaction.

The v, r, and s values are the three parts that make up the signature of a transaction. In order to recover the sender of a transaction the two components needed are the signature (v, r, and s) and the signing hash of the transaction. The sender's public key can be obtained with these two values and therefore the sender address can be retrieved.

This function takes chain_id and a transaction as parameters and returns the address of the sender of the transaction. It raises an InvalidSignatureError if the signature values (r, s, v) are invalid.

def recover_sender(​tx: Transaction​) -> Address:
930
    <snip>
943
    assert not isinstance(tx, FrameTransaction)
944
945
    r, s = tx.r, tx.s
946
    if U256(0) >= r or r >= SECP256K1N:
947
        raise InvalidSignatureError("bad r")
948
    if U256(0) >= s or s > SECP256K1N // U256(2):
949
        raise InvalidSignatureError("bad s")
950
951
    if isinstance(tx, LegacyTransaction):
952
        v = tx.v
953
        if v == 27 or v == 28:
954
            public_key = secp256k1_recover(
955
                r, s, v - U256(27), signing_hash_pre155(tx)
956
            )
957
        else:
958
            assert v >= U256(35), "call chain_id before recover_sender"
959
            tx_chain_id = U64((v - U256(35)) >> U256(1))
960
            v = (v - U256(35)) & U256(1)
961
            public_key = secp256k1_recover(
962
                r,
963
                s,
964
                v,
965
                signing_hash_155(tx, tx_chain_id),
966
            )
967
    elif isinstance(tx, AccessListTransaction):
968
        if tx.y_parity not in (U256(0), U256(1)):
969
            raise InvalidSignatureError("bad y_parity")
970
        public_key = secp256k1_recover(
971
            r, s, tx.y_parity, signing_hash_2930(tx)
972
        )
973
    elif isinstance(tx, FeeMarketTransaction):
974
        if tx.y_parity not in (U256(0), U256(1)):
975
            raise InvalidSignatureError("bad y_parity")
976
        public_key = secp256k1_recover(
977
            r, s, tx.y_parity, signing_hash_1559(tx)
978
        )
979
    elif isinstance(tx, BlobTransaction):
980
        if tx.y_parity not in (U256(0), U256(1)):
981
            raise InvalidSignatureError("bad y_parity")
982
        public_key = secp256k1_recover(
983
            r, s, tx.y_parity, signing_hash_4844(tx)
984
        )
985
    elif isinstance(tx, SetCodeTransaction):
986
        if tx.y_parity not in (U256(0), U256(1)):
987
            raise InvalidSignatureError("bad y_parity")
988
        public_key = secp256k1_recover(
989
            r, s, tx.y_parity, signing_hash_7702(tx)
990
        )
991
992
    return Address(keccak256(public_key)[12:32])

signing_hash_pre155

Compute the hash of a transaction used in a legacy (pre EIP-155) signature.

This function takes a legacy transaction as a parameter and returns the signing hash of the transaction.

def signing_hash_pre155(​tx: LegacyTransaction​) -> Hash32:
996
    <snip>
1005
    return keccak256(
1006
        rlp.encode(
1007
            (
1008
                tx.nonce,
1009
                tx.gas_price,
1010
                tx.gas,
1011
                tx.to,
1012
                tx.value,
1013
                tx.data,
1014
            )
1015
        )
1016
    )

signing_hash_155

Compute the hash of a transaction used in a EIP-155 signature.

This function takes a legacy transaction and a chain ID as parameters and returns the hash of the transaction used in an EIP-155 signature.

def signing_hash_155(​tx: LegacyTransaction, ​​chain_id: U64​) -> Hash32:
1020
    <snip>
1028
    return keccak256(
1029
        rlp.encode(
1030
            (
1031
                tx.nonce,
1032
                tx.gas_price,
1033
                tx.gas,
1034
                tx.to,
1035
                tx.value,
1036
                tx.data,
1037
                chain_id,
1038
                Uint(0),
1039
                Uint(0),
1040
            )
1041
        )
1042
    )

signing_hash_2930

Compute the hash of a transaction used in a EIP-2930 signature.

This function takes an access list transaction as a parameter and returns the hash of the transaction used in an EIP-2930 signature.

def signing_hash_2930(​tx: AccessListTransaction​) -> Hash32:
1046
    <snip>
1054
    return keccak256(
1055
        b"\x01"
1056
        + rlp.encode(
1057
            (
1058
                tx.chain_id,
1059
                tx.nonce,
1060
                tx.gas_price,
1061
                tx.gas,
1062
                tx.to,
1063
                tx.value,
1064
                tx.data,
1065
                tx.access_list,
1066
            )
1067
        )
1068
    )

signing_hash_1559

Compute the hash of a transaction used in an EIP-1559 signature.

This function takes a fee market transaction as a parameter and returns the hash of the transaction used in an EIP-1559 signature.

def signing_hash_1559(​tx: FeeMarketTransaction​) -> Hash32:
1072
    <snip>
1080
    return keccak256(
1081
        b"\x02"
1082
        + rlp.encode(
1083
            (
1084
                tx.chain_id,
1085
                tx.nonce,
1086
                tx.max_priority_fee_per_gas,
1087
                tx.max_fee_per_gas,
1088
                tx.gas,
1089
                tx.to,
1090
                tx.value,
1091
                tx.data,
1092
                tx.access_list,
1093
            )
1094
        )
1095
    )

signing_hash_4844

Compute the hash of a transaction used in an EIP-4844 signature.

This function takes a transaction as a parameter and returns the signing hash of the transaction used in an EIP-4844 signature.

def signing_hash_4844(​tx: BlobTransaction​) -> Hash32:
1099
    <snip>
1107
    return keccak256(
1108
        b"\x03"
1109
        + rlp.encode(
1110
            (
1111
                tx.chain_id,
1112
                tx.nonce,
1113
                tx.max_priority_fee_per_gas,
1114
                tx.max_fee_per_gas,
1115
                tx.gas,
1116
                tx.to,
1117
                tx.value,
1118
                tx.data,
1119
                tx.access_list,
1120
                tx.max_fee_per_blob_gas,
1121
                tx.blob_versioned_hashes,
1122
            )
1123
        )
1124
    )

signing_hash_7702

Compute the hash of a transaction used in a EIP-7702 signature.

This function takes a transaction as a parameter and returns the signing hash of the transaction used in a EIP-7702 signature.

def signing_hash_7702(​tx: SetCodeTransaction​) -> Hash32:
1128
    <snip>
1136
    return keccak256(
1137
        b"\x04"
1138
        + rlp.encode(
1139
            (
1140
                tx.chain_id,
1141
                tx.nonce,
1142
                tx.max_priority_fee_per_gas,
1143
                tx.max_fee_per_gas,
1144
                tx.gas,
1145
                tx.to,
1146
                tx.value,
1147
                tx.data,
1148
                tx.access_list,
1149
                tx.authorizations,
1150
            )
1151
        )
1152
    )

get_transaction_hash

Compute the hash of a transaction.

This function takes a transaction as a parameter and returns the keccak256 hash of the transaction. It can handle both legacy transactions and typed transactions (AccessListTransaction, FeeMarketTransaction, etc.).

def get_transaction_hash(​tx: Bytes | LegacyTransaction​) -> Hash32:
1156
    <snip>
1164
    assert isinstance(tx, (LegacyTransaction, Bytes))
1165
    if isinstance(tx, LegacyTransaction):
1166
        return keccak256(rlp.encode(tx))
1167
    else:
1168
        return keccak256(tx)

has_access_list

Return whether the transaction has an EIP-2930-style access list.

def has_access_list(​tx: Transaction​) -> TypeGuard[AccessListCapableTransaction]:
1174
    <snip>
1179
    return isinstance(
1180
        tx,
1181
        AccessListCapableTransaction,
1182
    )