ethereum.forks.amsterdam.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.
| 40 | @final |
|---|
| 41 | @dataclass |
|---|
class IntrinsicGasCost:
execution¶
Execution gas (calldata, base cost, access list, etc.).
| 45 | execution: ExecutionGas |
|---|
calldata_floor¶
Minimum gas cost based on calldata size per EIP-7623.
| 48 | calldata_floor: ExecutionGas |
|---|
TX_MAX_GAS_LIMIT¶
| 56 | TX_MAX_GAS_LIMIT = Uint(16_777_216) |
|---|
BLOB_COUNT_LIMIT¶
Maximum number of blobs a single transaction may carry.
| 58 | BLOB_COUNT_LIMIT = 6 |
|---|
VERSIONED_HASH_VERSION_KZG¶
Version byte that every blob versioned hash must start with.
| 63 | VERSIONED_HASH_VERSION_KZG = b"\x01" |
|---|
ACCESS_LIST_ADDRESS_FLOOR_TOKENS¶
Floor data tokens contributed by a single access list address per EIP-7981.
| 68 | 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.
| 76 | ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS = Uint(128) |
|---|
LegacyTransaction ¶
| 85 | @final |
|---|
| 86 | @slotted_freezable |
|---|
| 87 | @dataclass |
|---|
class LegacyTransaction:
nonce¶
A scalar value equal to the number of transactions sent by the sender.
| 100 | nonce: U256 |
|---|
gas_price¶
The price of gas for this transaction, in wei.
| 105 | gas_price: Uint |
|---|
gas¶
The maximum amount of gas that can be used by this transaction.
| 110 | gas: Uint |
|---|
to¶
The address of the recipient. If empty, the transaction is a contract creation.
| 115 | to: Bytes0 | Address |
|---|
value¶
The amount of ether (in wei) to send with this transaction.
| 121 | value: U256 |
|---|
data¶
The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.
| 126 | data: Bytes |
|---|
v¶
The recovery id of the signature.
| 132 | v: U256 |
|---|
r¶
The first part of the signature.
| 137 | r: U256 |
|---|
s¶
The second part of the signature.
| 142 | s: U256 |
|---|
Access ¶
A mapping from account address to storage slots that are pre-warmed as part of a transaction.
| 148 | @final |
|---|
| 149 | @slotted_freezable |
|---|
| 150 | @dataclass |
|---|
class Access:
account¶
The address of the account that is accessed.
| 157 | account: Address |
|---|
slots¶
A tuple of storage slots that are accessed in the account.
| 162 | 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.
| 168 | @final |
|---|
| 169 | @slotted_freezable |
|---|
| 170 | @dataclass |
|---|
class AccessListTransaction:
chain_id¶
The ID of the chain on which this transaction is executed.
| 182 | chain_id: U64 |
|---|
nonce¶
A scalar value equal to the number of transactions sent by the sender.
| 187 | nonce: U256 |
|---|
gas_price¶
The price of gas for this transaction.
| 192 | gas_price: Uint |
|---|
gas¶
The maximum amount of gas that can be used by this transaction.
| 197 | gas: Uint |
|---|
to¶
The address of the recipient. If empty, the transaction is a contract creation.
| 202 | to: Bytes0 | Address |
|---|
value¶
The amount of ether (in wei) to send with this transaction.
| 208 | value: U256 |
|---|
data¶
The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.
| 213 | data: Bytes |
|---|
access_list¶
A tuple of Access objects that specify which addresses and storage slots
are accessed in the transaction.
| 219 | access_list: Tuple[Access, ...] |
|---|
y_parity¶
The recovery id of the signature.
| 225 | y_parity: U256 |
|---|
r¶
The first part of the signature.
| 230 | r: U256 |
|---|
s¶
The second part of the signature.
| 235 | 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.
| 241 | @final |
|---|
| 242 | @slotted_freezable |
|---|
| 243 | @dataclass |
|---|
class FeeMarketTransaction:
chain_id¶
The ID of the chain on which this transaction is executed.
| 254 | chain_id: U64 |
|---|
nonce¶
A scalar value equal to the number of transactions sent by the sender.
| 259 | nonce: U256 |
|---|
max_priority_fee_per_gas¶
The maximum priority fee per gas that the sender is willing to pay.
| 264 | 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.
| 269 | max_fee_per_gas: Uint |
|---|
gas¶
The maximum amount of gas that can be used by this transaction.
| 275 | gas: Uint |
|---|
to¶
The address of the recipient. If empty, the transaction is a contract creation.
| 280 | to: Bytes0 | Address |
|---|
value¶
The amount of ether (in wei) to send with this transaction.
| 286 | value: U256 |
|---|
data¶
The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.
| 291 | data: Bytes |
|---|
access_list¶
A tuple of Access objects that specify which addresses and storage slots
are accessed in the transaction.
| 297 | access_list: Tuple[Access, ...] |
|---|
y_parity¶
The recovery id of the signature.
| 303 | y_parity: U256 |
|---|
r¶
The first part of the signature.
| 308 | r: U256 |
|---|
s¶
The second part of the signature.
| 313 | s: U256 |
|---|
BlobTransaction ¶
The transaction type added in EIP-4844.
This transaction type extends the fee market transaction to support blob-carrying transactions.
| 319 | @final |
|---|
| 320 | @slotted_freezable |
|---|
| 321 | @dataclass |
|---|
class BlobTransaction:
chain_id¶
The ID of the chain on which this transaction is executed.
| 332 | chain_id: U64 |
|---|
nonce¶
A scalar value equal to the number of transactions sent by the sender.
| 337 | nonce: U256 |
|---|
max_priority_fee_per_gas¶
The maximum priority fee per gas that the sender is willing to pay.
| 342 | 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.
| 347 | max_fee_per_gas: Uint |
|---|
gas¶
The maximum amount of gas that can be used by this transaction.
| 353 | gas: Uint |
|---|
to¶
The address of the recipient. If empty, the transaction is a contract creation.
| 358 | to: Address |
|---|
value¶
The amount of ether (in wei) to send with this transaction.
| 364 | value: U256 |
|---|
data¶
The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.
| 369 | data: Bytes |
|---|
access_list¶
A tuple of Access objects that specify which addresses and storage slots
are accessed in the transaction.
| 375 | access_list: Tuple[Access, ...] |
|---|
max_fee_per_blob_gas¶
The maximum fee per blob gas that the sender is willing to pay.
| 381 | 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.
| 386 | blob_versioned_hashes: Tuple[VersionedHash, ...] |
|---|
y_parity¶
The recovery id of the signature.
| 392 | y_parity: U256 |
|---|
r¶
The first part of the signature.
| 397 | r: U256 |
|---|
s¶
The second part of the signature.
| 402 | 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.
| 408 | @final |
|---|
| 409 | @slotted_freezable |
|---|
| 410 | @dataclass |
|---|
class SetCodeTransaction:
chain_id¶
The ID of the chain on which this transaction is executed.
| 421 | chain_id: U64 |
|---|
nonce¶
A scalar value equal to the number of transactions sent by the sender.
| 426 | nonce: U64 |
|---|
max_priority_fee_per_gas¶
The maximum priority fee per gas that the sender is willing to pay.
| 431 | 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.
| 436 | max_fee_per_gas: Uint |
|---|
gas¶
The maximum amount of gas that can be used by this transaction.
| 442 | gas: Uint |
|---|
to¶
The address of the recipient. If empty, the transaction is a contract creation.
| 447 | to: Address |
|---|
value¶
The amount of ether (in wei) to send with this transaction.
| 453 | value: U256 |
|---|
data¶
The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.
| 458 | data: Bytes |
|---|
access_list¶
A tuple of Access objects that specify which addresses and storage slots
are accessed in the transaction.
| 464 | access_list: Tuple[Access, ...] |
|---|
y_parity¶
The recovery id of the signature.
| 476 | y_parity: U256 |
|---|
r¶
The first part of the signature.
| 481 | r: U256 |
|---|
s¶
The second part of the signature.
| 486 | s: U256 |
|---|
Transaction¶
Union type representing any valid transaction type.
| 492 | Transaction = ( |
|---|---|
| 493 | LegacyTransaction |
| 494 | | AccessListTransaction |
| 495 | | FeeMarketTransaction |
| 496 | | BlobTransaction |
| 497 | | SetCodeTransaction |
| 498 | ) |
AccessListCapableTransaction¶
Transaction types that include an EIP-2930-style access list.
See has_access_list and Access for more details.
| 504 | AccessListCapableTransaction = ( |
|---|---|
| 505 | AccessListTransaction |
| 506 | | FeeMarketTransaction |
| 507 | | BlobTransaction |
| 508 | | SetCodeTransaction |
| 509 | ) |
FeeMarketCapableTransaction¶
Transaction types that include the EIP-1559-style fee structure.
See FeeMarketTransaction for more details.
| 521 | FeeMarketCapableTransaction = ( |
|---|---|
| 522 | FeeMarketTransaction | BlobTransaction | SetCodeTransaction |
| 523 | ) |
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:
| 535 | <snip> |
|---|---|
| 542 | if isinstance(tx, LegacyTransaction): |
| 543 | return tx |
| 544 | elif isinstance(tx, AccessListTransaction): |
| 545 | return b"\x01" + rlp.encode(tx) |
| 546 | elif isinstance(tx, FeeMarketTransaction): |
| 547 | return b"\x02" + rlp.encode(tx) |
| 548 | elif isinstance(tx, BlobTransaction): |
| 549 | return b"\x03" + rlp.encode(tx) |
| 550 | elif isinstance(tx, SetCodeTransaction): |
| 551 | return b"\x04" + rlp.encode(tx) |
| 552 | else: |
| 553 | 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:
| 557 | <snip> |
|---|---|
| 568 | if isinstance(tx, Bytes): |
| 569 | if tx[0] == 1: |
| 570 | return rlp.decode_to(AccessListTransaction, tx[1:]) |
| 571 | elif tx[0] == 2: |
| 572 | return rlp.decode_to(FeeMarketTransaction, tx[1:]) |
| 573 | elif tx[0] == 3: |
| 574 | return rlp.decode_to(BlobTransaction, tx[1:]) |
| 575 | elif tx[0] == 4: |
| 576 | return rlp.decode_to(SetCodeTransaction, tx[1:]) |
| 577 | elif tx[0] >= 0xC0: |
| 578 | assert tx[0] <= 0xFE |
| 579 | return rlp.decode_to(LegacyTransaction, tx) |
| 580 | else: |
| 581 | raise TransactionTypeError(tx[0]) |
| 582 | else: |
| 583 | 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 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, 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:
| 587 | <snip> |
|---|---|
| 618 | from .vm.interpreter import MAX_INIT_CODE_SIZE |
| 619 | |
| 620 | if U256(tx.nonce) >= U256(U64.MAX_VALUE): |
| 621 | raise NonceOverflowError("Nonce too high") |
| 622 | |
| 623 | if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE: |
| 624 | raise InitCodeTooLargeError("Code size too large") |
| 625 | |
| 626 | if isinstance(tx, FeeMarketCapableTransaction): |
| 627 | if tx.max_fee_per_gas < tx.max_priority_fee_per_gas: |
| 628 | raise PriorityFeeGreaterThanMaxFeeError( |
| 629 | "priority fee greater than max fee" |
| 630 | ) |
| 631 | |
| 632 | if isinstance(tx, BlobTransaction): |
| 633 | blob_count = len(tx.blob_versioned_hashes) |
| 634 | if blob_count == 0: |
| 635 | raise NoBlobDataError("no blob data in transaction") |
| 636 | if blob_count > BLOB_COUNT_LIMIT: |
| 637 | raise BlobCountExceededError( |
| 638 | f"Tx has {blob_count} blobs. Max allowed: {BLOB_COUNT_LIMIT}" |
| 639 | ) |
| 640 | for blob_versioned_hash in tx.blob_versioned_hashes: |
| 641 | if blob_versioned_hash[0:1] != VERSIONED_HASH_VERSION_KZG: |
| 642 | raise InvalidBlobVersionedHashError( |
| 643 | "invalid blob versioned hash" |
| 644 | ) |
| 645 | |
| 646 | if isinstance(tx, (BlobTransaction, SetCodeTransaction)): |
| 647 | if not isinstance(tx.to, Address): |
| 648 | raise TransactionTypeContractCreationError(tx) |
| 649 | |
| 650 | if isinstance(tx, SetCodeTransaction): |
| 651 | if not any(tx.authorizations): |
| 652 | raise EmptyAuthorizationListError("empty authorization list") |
| 653 | |
| 654 | intrinsic = calculate_intrinsic_cost(tx, sender) |
| 655 | intrinsic_gas = Uint(intrinsic.execution) |
| 656 | if intrinsic_gas > tx.gas: |
| 657 | raise InsufficientTransactionGasError("Insufficient intrinsic gas") |
| 658 | if intrinsic.calldata_floor > tx.gas: |
| 659 | raise InsufficientTransactionGasError("Insufficient calldata floor") |
| 660 | if intrinsic.execution > TX_MAX_GAS_LIMIT: |
| 661 | raise InsufficientTransactionGasError( |
| 662 | "Intrinsic execution gas exceeds TX_MAX_GAS_LIMIT" |
| 663 | ) |
| 664 | if intrinsic.calldata_floor > TX_MAX_GAS_LIMIT: |
| 665 | raise InsufficientTransactionGasError( |
| 666 | "Intrinsic calldata floor exceeds TX_MAX_GAS_LIMIT" |
| 667 | ) |
| 668 | |
| 669 | 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:
Sender cost (
TX_BASE).Recipient cost (
COLD_ACCOUNT_ACCESSfor a non-self-transfer call, orCREATE_ACCESSfor a contract creation). The created account'sNEW_ACCOUNTstate gas is state-dependent and is charged at the top frame, not here.Value cost (
TX_VALUE_COSTfor a non-self-transfer call) whentx.value > 0.Calldata cost (zero and non-zero bytes).
Access list entries (if applicable).
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 byset_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. 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:
| 675 | <snip> |
|---|---|
| 711 | from .vm.gas import GasCosts, init_code_cost |
| 712 | |
| 713 | tokens_in_calldata = count_tokens_in_data(tx.data) |
| 714 | |
| 715 | data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD |
| 716 | |
| 717 | is_create = tx.to == Bytes0(b"") |
| 718 | is_self_transfer = tx.to == sender |
| 719 | |
| 720 | recipient_execution_gas = Uint(0) |
| 721 | init_code_gas = Uint(0) |
| 722 | if is_create: |
| 723 | recipient_execution_gas = GasCosts.CREATE_ACCESS |
| 724 | init_code_gas = init_code_cost(ulen(tx.data)) |
| 725 | elif not is_self_transfer: |
| 726 | recipient_execution_gas = GasCosts.COLD_ACCOUNT_ACCESS |
| 727 | if tx.value > U256(0): |
| 728 | recipient_execution_gas += GasCosts.TX_VALUE_COST |
| 729 | |
| 730 | access_list_cost = Uint(0) |
| 731 | tokens_in_access_list = Uint(0) |
| 732 | if has_access_list(tx): |
| 733 | for access in tx.access_list: |
| 734 | access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS |
| 735 | access_list_cost += ( |
| 736 | ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY |
| 737 | ) |
| 738 | tokens_in_access_list += ACCESS_LIST_ADDRESS_FLOOR_TOKENS |
| 739 | tokens_in_access_list += ( |
| 740 | ulen(access.slots) * ACCESS_LIST_STORAGE_KEY_FLOOR_TOKENS |
| 741 | ) |
| 742 | |
| 743 | # Data token floor cost for access list bytes. |
| 744 | access_list_cost += tokens_in_access_list * GasCosts.TX_DATA_TOKEN_FLOOR |
| 745 | |
| 746 | auth_cost = Uint(0) |
| 747 | if isinstance(tx, SetCodeTransaction): |
| 748 | auth_cost = GasCosts.EXECUTION_PER_AUTH_BASE_COST * ulen( |
| 749 | tx.authorizations |
| 750 | ) |
| 751 | |
| 752 | # EIP-7976 floor tokens: all calldata bytes count uniformly. |
| 753 | floor_tokens_in_calldata = ulen(tx.data) * GasCosts.TX_DATA_TOKEN_STANDARD |
| 754 | |
| 755 | # Total floor tokens. |
| 756 | total_floor_tokens = floor_tokens_in_calldata + tokens_in_access_list |
| 757 | |
| 758 | # Decomposed execution-gas intrinsic base (EIP-2780), which also |
| 759 | # anchors the calldata floor. |
| 760 | base_execution_gas = GasCosts.TX_BASE + recipient_execution_gas |
| 761 | |
| 762 | # Floor gas cost (EIP-7623: minimum gas for data-heavy transactions). |
| 763 | data_floor_gas_cost = ( |
| 764 | total_floor_tokens * GasCosts.TX_DATA_TOKEN_FLOOR + base_execution_gas |
| 765 | ) |
| 766 | |
| 767 | return IntrinsicGasCost( |
| 768 | execution=ExecutionGas( |
| 769 | base_execution_gas |
| 770 | + init_code_gas |
| 771 | + data_cost |
| 772 | + access_list_cost |
| 773 | + auth_cost |
| 774 | ), |
| 775 | calldata_floor=ExecutionGas(data_floor_gas_cost), |
| 776 | ) |
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.
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.
def calculate_effective_gas_price(tx: Transaction, base_fee_per_gas: Uint) -> Uint:
| 794 | <snip> |
|---|---|
| 803 | if isinstance(tx, FeeMarketCapableTransaction): |
| 804 | if tx.max_fee_per_gas < base_fee_per_gas: |
| 805 | raise InsufficientMaxFeePerGasError( |
| 806 | tx.max_fee_per_gas, base_fee_per_gas |
| 807 | ) |
| 808 | |
| 809 | priority_fee_per_gas = min( |
| 810 | tx.max_priority_fee_per_gas, |
| 811 | tx.max_fee_per_gas - base_fee_per_gas, |
| 812 | ) |
| 813 | return priority_fee_per_gas + base_fee_per_gas |
| 814 | |
| 815 | if tx.gas_price < base_fee_per_gas: |
| 816 | raise InvalidBlock |
| 817 | 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.
def calculate_max_gas_fee(tx: Transaction, gas_limit: Uint) -> Uint:
| 821 | <snip> |
|---|---|
| 825 | if isinstance(tx, FeeMarketCapableTransaction): |
| 826 | return gas_limit * tx.max_fee_per_gas |
| 827 | 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:
| 831 | <snip> |
|---|---|
| 834 | if sender_nonce > Uint(tx.nonce): |
| 835 | raise NonceMismatchError("nonce too low") |
| 836 | elif sender_nonce < Uint(tx.nonce): |
| 837 | raise NonceMismatchError("nonce too high") |
chain_id ¶
Extract the chain identifier from a transaction. See EIP-155.
def chain_id(tx: Transaction) -> None | U64:
| 841 | <snip> |
|---|---|
| 846 | if isinstance(tx, LegacyTransaction): |
| 847 | if tx.v == 27 or tx.v == 28: |
| 848 | return None |
| 849 | |
| 850 | if tx.v < U256(35): |
| 851 | raise InvalidSignatureError("bad v") |
| 852 | |
| 853 | return U64((tx.v - U256(35)) >> U256(1)) |
| 854 | else: |
| 855 | 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:
| 859 | <snip> |
|---|---|
| 872 | r, s = tx.r, tx.s |
| 873 | if U256(0) >= r or r >= SECP256K1N: |
| 874 | raise InvalidSignatureError("bad r") |
| 875 | if U256(0) >= s or s > SECP256K1N // U256(2): |
| 876 | raise InvalidSignatureError("bad s") |
| 877 | |
| 878 | if isinstance(tx, LegacyTransaction): |
| 879 | v = tx.v |
| 880 | if v == 27 or v == 28: |
| 881 | public_key = secp256k1_recover( |
| 882 | r, s, v - U256(27), signing_hash_pre155(tx) |
| 883 | ) |
| 884 | else: |
| 885 | assert v >= U256(35), "call chain_id before recover_sender" |
| 886 | tx_chain_id = U64((v - U256(35)) >> U256(1)) |
| 887 | v = (v - U256(35)) & U256(1) |
| 888 | public_key = secp256k1_recover( |
| 889 | r, |
| 890 | s, |
| 891 | v, |
| 892 | signing_hash_155(tx, tx_chain_id), |
| 893 | ) |
| 894 | elif isinstance(tx, AccessListTransaction): |
| 895 | if tx.y_parity not in (U256(0), U256(1)): |
| 896 | raise InvalidSignatureError("bad y_parity") |
| 897 | public_key = secp256k1_recover( |
| 898 | r, s, tx.y_parity, signing_hash_2930(tx) |
| 899 | ) |
| 900 | elif isinstance(tx, FeeMarketTransaction): |
| 901 | if tx.y_parity not in (U256(0), U256(1)): |
| 902 | raise InvalidSignatureError("bad y_parity") |
| 903 | public_key = secp256k1_recover( |
| 904 | r, s, tx.y_parity, signing_hash_1559(tx) |
| 905 | ) |
| 906 | elif isinstance(tx, BlobTransaction): |
| 907 | if tx.y_parity not in (U256(0), U256(1)): |
| 908 | raise InvalidSignatureError("bad y_parity") |
| 909 | public_key = secp256k1_recover( |
| 910 | r, s, tx.y_parity, signing_hash_4844(tx) |
| 911 | ) |
| 912 | elif isinstance(tx, SetCodeTransaction): |
| 913 | if tx.y_parity not in (U256(0), U256(1)): |
| 914 | raise InvalidSignatureError("bad y_parity") |
| 915 | public_key = secp256k1_recover( |
| 916 | r, s, tx.y_parity, signing_hash_7702(tx) |
| 917 | ) |
| 918 | |
| 919 | 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.
signing_hash_155 ¶
signing_hash_2930 ¶
signing_hash_1559 ¶
def signing_hash_1559(tx: FeeMarketTransaction) -> Hash32:
| 999 | <snip> |
|---|---|
| 1007 | return keccak256( |
| 1008 | b"\x02" |
| 1009 | + rlp.encode( |
| 1010 | ( |
| 1011 | tx.chain_id, |
| 1012 | tx.nonce, |
| 1013 | tx.max_priority_fee_per_gas, |
| 1014 | tx.max_fee_per_gas, |
| 1015 | tx.gas, |
| 1016 | tx.to, |
| 1017 | tx.value, |
| 1018 | tx.data, |
| 1019 | tx.access_list, |
| 1020 | ) |
| 1021 | ) |
| 1022 | ) |
signing_hash_4844 ¶
def signing_hash_4844(tx: BlobTransaction) -> Hash32:
| 1026 | <snip> |
|---|---|
| 1034 | return keccak256( |
| 1035 | b"\x03" |
| 1036 | + rlp.encode( |
| 1037 | ( |
| 1038 | tx.chain_id, |
| 1039 | tx.nonce, |
| 1040 | tx.max_priority_fee_per_gas, |
| 1041 | tx.max_fee_per_gas, |
| 1042 | tx.gas, |
| 1043 | tx.to, |
| 1044 | tx.value, |
| 1045 | tx.data, |
| 1046 | tx.access_list, |
| 1047 | tx.max_fee_per_blob_gas, |
| 1048 | tx.blob_versioned_hashes, |
| 1049 | ) |
| 1050 | ) |
| 1051 | ) |
signing_hash_7702 ¶
def signing_hash_7702(tx: SetCodeTransaction) -> Hash32:
| 1055 | <snip> |
|---|---|
| 1063 | return keccak256( |
| 1064 | b"\x04" |
| 1065 | + rlp.encode( |
| 1066 | ( |
| 1067 | tx.chain_id, |
| 1068 | tx.nonce, |
| 1069 | tx.max_priority_fee_per_gas, |
| 1070 | tx.max_fee_per_gas, |
| 1071 | tx.gas, |
| 1072 | tx.to, |
| 1073 | tx.value, |
| 1074 | tx.data, |
| 1075 | tx.access_list, |
| 1076 | tx.authorizations, |
| 1077 | ) |
| 1078 | ) |
| 1079 | ) |
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:
| 1083 | <snip> |
|---|---|
| 1091 | assert isinstance(tx, (LegacyTransaction, Bytes)) |
| 1092 | if isinstance(tx, LegacyTransaction): |
| 1093 | return keccak256(rlp.encode(tx)) |
| 1094 | else: |
| 1095 | 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]:
| 1101 | <snip> |
|---|---|
| 1106 | return isinstance( |
| 1107 | tx, |
| 1108 | AccessListCapableTransaction, |
| 1109 | ) |