ethereum.forks.prague.transactionsethereum.forks.osaka.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.

32
@final
33
@dataclass
class IntrinsicGasCost:

regular

Regular execution gas (calldata, base cost, access list, etc.).

37
    regular: Uint

calldata_floor

Minimum gas cost based on calldata size per EIP-7623.

40
    calldata_floor: Uint

TX_MAX_GAS_LIMIT

48
TX_MAX_GAS_LIMIT = Uint(16_777_216)

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.

51
@final
52
@slotted_freezable
53
@dataclass
class LegacyTransaction:

nonce

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

66
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

71
    gas_price: Uint

gas

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

76
    gas: Uint

to

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

81
    to: Bytes0 | Address

value

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

87
    value: U256

data

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

92
    data: Bytes

v

The recovery id of the signature.

98
    v: U256

r

The first part of the signature.

103
    r: U256

s

The second part of the signature.

108
    s: U256

Access

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

114
@final
115
@slotted_freezable
116
@dataclass
class Access:

account

The address of the account that is accessed.

123
    account: Address

slots

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

128
    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.

134
@final
135
@slotted_freezable
136
@dataclass
class AccessListTransaction:

chain_id

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

148
    chain_id: U64

nonce

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

153
    nonce: U256

gas_price

The price of gas for this transaction.

158
    gas_price: Uint

gas

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

163
    gas: Uint

to

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

168
    to: Bytes0 | Address

value

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

174
    value: U256

data

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

179
    data: Bytes

access_list

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

185
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

191
    y_parity: U256

r

The first part of the signature.

196
    r: U256

s

The second part of the signature.

201
    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.

207
@final
208
@slotted_freezable
209
@dataclass
class FeeMarketTransaction:

chain_id

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

220
    chain_id: U64

nonce

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

225
    nonce: U256

max_priority_fee_per_gas

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

230
    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.

235
    max_fee_per_gas: Uint

gas

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

241
    gas: Uint

to

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

246
    to: Bytes0 | Address

value

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

252
    value: U256

data

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

257
    data: Bytes

access_list

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

263
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

269
    y_parity: U256

r

The first part of the signature.

274
    r: U256

s

The second part of the signature.

279
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

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

285
@final
286
@slotted_freezable
287
@dataclass
class BlobTransaction:

chain_id

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

298
    chain_id: U64

nonce

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

303
    nonce: U256

max_priority_fee_per_gas

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

308
    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.

313
    max_fee_per_gas: Uint

gas

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

319
    gas: Uint

to

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

324
    to: Address

value

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

330
    value: U256

data

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

335
    data: Bytes

access_list

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

341
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

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

347
    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.

352
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

358
    y_parity: U256

r

The first part of the signature.

363
    r: U256

s

The second part of the signature.

368
    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.

374
@final
375
@slotted_freezable
376
@dataclass
class SetCodeTransaction:

chain_id

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

387
    chain_id: U64

nonce

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

392
    nonce: U64

max_priority_fee_per_gas

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

397
    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.

402
    max_fee_per_gas: Uint

gas

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

408
    gas: Uint

to

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

413
    to: Address

value

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

419
    value: U256

data

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

424
    data: Bytes

access_list

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

430
    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.

436
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

442
    y_parity: U256

r

The first part of the signature.

447
    r: U256

s

The second part of the signature.

452
    s: U256

Transaction

Union type representing any valid transaction type.

458
Transaction = (
459
    LegacyTransaction
460
    | AccessListTransaction
461
    | FeeMarketTransaction
462
    | BlobTransaction
463
    | SetCodeTransaction
464
)

AccessListCapableTransaction

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

See has_access_list and Access for more details.

470
AccessListCapableTransaction = (
471
    AccessListTransaction
472
    | FeeMarketTransaction
473
    | BlobTransaction
474
    | SetCodeTransaction
475
)

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:
488
    <snip>
495
    if isinstance(tx, LegacyTransaction):
496
        return tx
497
    elif isinstance(tx, AccessListTransaction):
498
        return b"\x01" + rlp.encode(tx)
499
    elif isinstance(tx, FeeMarketTransaction):
500
        return b"\x02" + rlp.encode(tx)
501
    elif isinstance(tx, BlobTransaction):
502
        return b"\x03" + rlp.encode(tx)
503
    elif isinstance(tx, SetCodeTransaction):
504
        return b"\x04" + rlp.encode(tx)
505
    else:
506
        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.

Legacy transactions are returned as-is, while other transaction types are decoded based on their type identifier prefix.

def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
510
    <snip>
517
    if isinstance(tx, Bytes):
518
        if tx[0] == 1:
519
            return rlp.decode_to(AccessListTransaction, tx[1:])
520
        elif tx[0] == 2:
521
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
522
        elif tx[0] == 3:
523
            return rlp.decode_to(BlobTransaction, tx[1:])
524
        elif tx[0] == 4:
525
            return rlp.decode_to(SetCodeTransaction, tx[1:])
526
        else:
527
            raise TransactionTypeError(tx[0])
528
    else:
529
        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.

This function takes a transaction as a parameter and returns the intrinsic gas cost and the minimum calldata gas cost 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 is greater than 2**64 - 2. It also raises an InitCodeTooLargeError if the code size of a contract creation transaction exceeds the maximum allowed size.

def validate_transaction(tx: Transaction) -> IntrinsicGasCost:
533
    <snip>
561
    from .vm.interpreter import MAX_INIT_CODE_SIZE
562
563
    intrinsic = calculate_intrinsic_cost(tx)
564
    if max(intrinsic.regular, intrinsic.calldata_floor) > tx.gas:
565
        raise InsufficientTransactionGasError("Insufficient gas")
566
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
567
        raise NonceOverflowError("Nonce too high")
568
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
569
        raise InitCodeTooLargeError("Code size too large")
570
    if tx.gas > TX_MAX_GAS_LIMIT:
571
        raise TransactionGasLimitExceededError("Gas limit too high")
572
573
    return intrinsic

calculate_intrinsic_cost

Calculates the gas that is charged before execution is started.

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. Base cost (TX_BASE)

  2. Cost for data (zero and non-zero bytes)

  3. Cost for contract creation (if applicable)

  4. Cost for access list entries (if applicable)

  5. Cost for authorizations (if applicable)

This function takes a transaction as a parameter and returns the intrinsic gas cost of the transaction and the minimum gas cost used by the transaction based on the calldata size.

def calculate_intrinsic_cost(tx: Transaction) -> IntrinsicGasCost:
577
    <snip>
601
    from .vm.gas import GasCosts, init_code_cost
602
603
    num_zeros = Uint(tx.data.count(0))
604
    num_non_zeros = ulen(tx.data) - num_zeros
605
606
    tokens_in_calldata = num_zeros + num_non_zeros * Uint(4)
607
    # EIP-7623 floor price (note: no EVM costs)
608
    calldata_floor_gas_cost = (
609
        tokens_in_calldata * GasCosts.TX_DATA_TOKEN_FLOOR + GasCosts.TX_BASE
610
    )
611
612
    data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD
613
614
    if tx.to == Bytes0(b""):
615
        create_cost = GasCosts.TX_CREATE + init_code_cost(ulen(tx.data))
616
    else:
617
        create_cost = Uint(0)
618
619
    access_list_cost = Uint(0)
620
    if has_access_list(tx):
621
        for access in tx.access_list:
622
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
623
            access_list_cost += (
624
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
625
            )
626
627
    auth_cost = Uint(0)
628
    if isinstance(tx, SetCodeTransaction):
629
        auth_cost += Uint(
630
            GasCosts.AUTH_PER_EMPTY_ACCOUNT * len(tx.authorizations)
631
        )
632
633
    return IntrinsicGasCost(
634
        regular=Uint(
635
            GasCosts.TX_BASE
636
            + data_cost
637
            + create_cost
638
            + access_list_cost
639
            + auth_cost
640
        ),
641
        calldata_floor=calldata_floor_gas_cost,
642
    )

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(chain_id: U64, ​​tx: Transaction) -> Address:
646
    <snip>
659
    r, s = tx.r, tx.s
660
    if U256(0) >= r or r >= SECP256K1N:
661
        raise InvalidSignatureError("bad r")
662
    if U256(0) >= s or s > SECP256K1N // U256(2):
663
        raise InvalidSignatureError("bad s")
664
665
    if isinstance(tx, LegacyTransaction):
666
        v = tx.v
667
        if v == 27 or v == 28:
668
            public_key = secp256k1_recover(
669
                r, s, v - U256(27), signing_hash_pre155(tx)
670
            )
671
        else:
672
            chain_id_x2 = U256(chain_id) * U256(2)
673
            if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2:
674
                raise InvalidSignatureError("bad v")
675
            public_key = secp256k1_recover(
676
                r,
677
                s,
678
                v - U256(35) - chain_id_x2,
679
                signing_hash_155(tx, chain_id),
680
            )
681
    elif isinstance(tx, AccessListTransaction):
682
        if tx.y_parity not in (U256(0), U256(1)):
683
            raise InvalidSignatureError("bad y_parity")
684
        public_key = secp256k1_recover(
685
            r, s, tx.y_parity, signing_hash_2930(tx)
686
        )
687
    elif isinstance(tx, FeeMarketTransaction):
688
        if tx.y_parity not in (U256(0), U256(1)):
689
            raise InvalidSignatureError("bad y_parity")
690
        public_key = secp256k1_recover(
691
            r, s, tx.y_parity, signing_hash_1559(tx)
692
        )
693
    elif isinstance(tx, BlobTransaction):
694
        if tx.y_parity not in (U256(0), U256(1)):
695
            raise InvalidSignatureError("bad y_parity")
696
        public_key = secp256k1_recover(
697
            r, s, tx.y_parity, signing_hash_4844(tx)
698
        )
699
    elif isinstance(tx, SetCodeTransaction):
700
        if tx.y_parity not in (U256(0), U256(1)):
701
            raise InvalidSignatureError("bad y_parity")
702
        public_key = secp256k1_recover(
703
            r, s, tx.y_parity, signing_hash_7702(tx)
704
        )
705
706
    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:
710
    <snip>
719
    return keccak256(
720
        rlp.encode(
721
            (
722
                tx.nonce,
723
                tx.gas_price,
724
                tx.gas,
725
                tx.to,
726
                tx.value,
727
                tx.data,
728
            )
729
        )
730
    )

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:
734
    <snip>
742
    return keccak256(
743
        rlp.encode(
744
            (
745
                tx.nonce,
746
                tx.gas_price,
747
                tx.gas,
748
                tx.to,
749
                tx.value,
750
                tx.data,
751
                chain_id,
752
                Uint(0),
753
                Uint(0),
754
            )
755
        )
756
    )

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:
760
    <snip>
768
    return keccak256(
769
        b"\x01"
770
        + rlp.encode(
771
            (
772
                tx.chain_id,
773
                tx.nonce,
774
                tx.gas_price,
775
                tx.gas,
776
                tx.to,
777
                tx.value,
778
                tx.data,
779
                tx.access_list,
780
            )
781
        )
782
    )

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:
786
    <snip>
794
    return keccak256(
795
        b"\x02"
796
        + rlp.encode(
797
            (
798
                tx.chain_id,
799
                tx.nonce,
800
                tx.max_priority_fee_per_gas,
801
                tx.max_fee_per_gas,
802
                tx.gas,
803
                tx.to,
804
                tx.value,
805
                tx.data,
806
                tx.access_list,
807
            )
808
        )
809
    )

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:
813
    <snip>
821
    return keccak256(
822
        b"\x03"
823
        + rlp.encode(
824
            (
825
                tx.chain_id,
826
                tx.nonce,
827
                tx.max_priority_fee_per_gas,
828
                tx.max_fee_per_gas,
829
                tx.gas,
830
                tx.to,
831
                tx.value,
832
                tx.data,
833
                tx.access_list,
834
                tx.max_fee_per_blob_gas,
835
                tx.blob_versioned_hashes,
836
            )
837
        )
838
    )

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:
842
    <snip>
850
    return keccak256(
851
        b"\x04"
852
        + rlp.encode(
853
            (
854
                tx.chain_id,
855
                tx.nonce,
856
                tx.max_priority_fee_per_gas,
857
                tx.max_fee_per_gas,
858
                tx.gas,
859
                tx.to,
860
                tx.value,
861
                tx.data,
862
                tx.access_list,
863
                tx.authorizations,
864
            )
865
        )
866
    )

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:
870
    <snip>
878
    assert isinstance(tx, (LegacyTransaction, Bytes))
879
    if isinstance(tx, LegacyTransaction):
880
        return keccak256(rlp.encode(tx))
881
    else:
882
        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]:
888
    <snip>
893
    return isinstance(
894
        tx,
895
        AccessListCapableTransaction,
896
    )