ethereum.forks.london.transactionsethereum.forks.arrow_glacier.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.

LegacyTransaction

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

28
@final
29
@slotted_freezable
30
@dataclass
class LegacyTransaction:

nonce

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

40
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

45
    gas_price: Uint

gas

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

50
    gas: Uint

to

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

55
    to: Bytes0 | Address

value

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

61
    value: U256

data

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

66
    data: Bytes

v

The recovery id of the signature.

72
    v: U256

r

The first part of the signature.

77
    r: U256

s

The second part of the signature.

82
    s: U256

Access

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

88
@final
89
@slotted_freezable
90
@dataclass
class Access:

account

The address of the account that is accessed.

97
    account: Address

slots

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

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

108
@final
109
@slotted_freezable
110
@dataclass
class AccessListTransaction:

chain_id

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

122
    chain_id: U64

nonce

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

127
    nonce: U256

gas_price

The price of gas for this transaction.

132
    gas_price: Uint

gas

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

137
    gas: Uint

to

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

142
    to: Bytes0 | Address

value

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

148
    value: U256

data

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

153
    data: Bytes

access_list

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

159
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

165
    y_parity: U256

r

The first part of the signature.

170
    r: U256

s

The second part of the signature.

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

181
@final
182
@slotted_freezable
183
@dataclass
class FeeMarketTransaction:

chain_id

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

194
    chain_id: U64

nonce

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

199
    nonce: U256

max_priority_fee_per_gas

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

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

209
    max_fee_per_gas: Uint

gas

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

215
    gas: Uint

to

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

220
    to: Bytes0 | Address

value

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

226
    value: U256

data

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

231
    data: Bytes

access_list

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

237
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

243
    y_parity: U256

r

The first part of the signature.

248
    r: U256

s

The second part of the signature.

253
    s: U256

Transaction

Union type representing any valid transaction type.

259
Transaction = LegacyTransaction | AccessListTransaction | FeeMarketTransaction

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:
266
    <snip>
273
    if isinstance(tx, LegacyTransaction):
274
        return tx
275
    elif isinstance(tx, AccessListTransaction):
276
        return b"\x01" + rlp.encode(tx)
277
    elif isinstance(tx, FeeMarketTransaction):
278
        return b"\x02" + rlp.encode(tx)
279
    else:
280
        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:
284
    <snip>
291
    if isinstance(tx, Bytes):
292
        if len(tx) == 0:
293
            raise InvalidBlock("empty transaction")
294
        if tx[0] == 1:
295
            return rlp.decode_to(AccessListTransaction, tx[1:])
296
        elif tx[0] == 2:
297
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
298
        else:
299
            raise TransactionTypeError(tx[0])
300
    else:
301
        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.

This function takes a transaction as a parameter and returns the intrinsic gas cost of 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 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​) -> Uint:
305
    <snip>
329
    intrinsic_gas = calculate_intrinsic_cost(tx)
330
    if intrinsic_gas > tx.gas:
331
        raise InsufficientTransactionGasError("Insufficient gas")
332
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
333
        raise NonceOverflowError("Nonce too high")
334
    if isinstance(tx, FeeMarketTransaction):
335
        if tx.max_fee_per_gas < tx.max_priority_fee_per_gas:
336
            raise PriorityFeeGreaterThanMaxFeeError(
337
                "priority fee greater than max fee"
338
            )
339
340
    return intrinsic_gas

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)

This function takes a transaction as a parameter and returns the intrinsic gas cost of the transaction.

def calculate_intrinsic_cost(​tx: Transaction​) -> Uint:
344
    <snip>
365
    from .vm.gas import GasCosts
366
367
    num_zeros = Uint(tx.data.count(0))
368
    num_non_zeros = ulen(tx.data) - num_zeros
369
    data_cost = (
370
        num_zeros * GasCosts.TX_DATA_PER_ZERO
371
        + num_non_zeros * GasCosts.TX_DATA_PER_NON_ZERO
372
    )
373
374
    if tx.to == Bytes0(b""):
375
        create_cost = GasCosts.TX_CREATE
376
    else:
377
        create_cost = Uint(0)
378
379
    access_list_cost = Uint(0)
380
    if isinstance(tx, (AccessListTransaction, FeeMarketTransaction)):
381
        for access in tx.access_list:
382
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
383
            access_list_cost += (
384
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
385
            )
386
387
    return GasCosts.TX_BASE + data_cost + create_cost + access_list_cost

chain_id

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

def chain_id(​tx: Transaction​) -> None | U64:
391
    <snip>
396
    if isinstance(tx, LegacyTransaction):
397
        if tx.v == 27 or tx.v == 28:
398
            return None
399
400
        if tx.v < U256(35):
401
            raise InvalidSignatureError("bad v")
402
403
        tx_chain_id = (tx.v - U256(35)) >> U256(1)
404
        if tx_chain_id > U256(U64.MAX_VALUE):
405
            raise InvalidSignatureError("bad v")
406
        return U64(tx_chain_id)
407
    else:
408
        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:
412
    <snip>
425
    r, s = tx.r, tx.s
426
    if U256(0) >= r or r >= SECP256K1N:
427
        raise InvalidSignatureError("bad r")
428
    if U256(0) >= s or s > SECP256K1N // U256(2):
429
        raise InvalidSignatureError("bad s")
430
431
    if isinstance(tx, LegacyTransaction):
432
        v = tx.v
433
        if v == 27 or v == 28:
434
            public_key = secp256k1_recover(
435
                r, s, v - U256(27), signing_hash_pre155(tx)
436
            )
437
        else:
438
            assert v >= U256(35), "call chain_id before recover_sender"
439
            tx_chain_id = U64((v - U256(35)) >> U256(1))
440
            v = (v - U256(35)) & U256(1)
441
            public_key = secp256k1_recover(
442
                r,
443
                s,
444
                v,
445
                signing_hash_155(tx, tx_chain_id),
446
            )
447
    elif isinstance(tx, AccessListTransaction):
448
        if tx.y_parity not in (U256(0), U256(1)):
449
            raise InvalidSignatureError("bad y_parity")
450
        public_key = secp256k1_recover(
451
            r, s, tx.y_parity, signing_hash_2930(tx)
452
        )
453
    elif isinstance(tx, FeeMarketTransaction):
454
        if tx.y_parity not in (U256(0), U256(1)):
455
            raise InvalidSignatureError("bad y_parity")
456
        public_key = secp256k1_recover(
457
            r, s, tx.y_parity, signing_hash_1559(tx)
458
        )
459
460
    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:
464
    <snip>
473
    return keccak256(
474
        rlp.encode(
475
            (
476
                tx.nonce,
477
                tx.gas_price,
478
                tx.gas,
479
                tx.to,
480
                tx.value,
481
                tx.data,
482
            )
483
        )
484
    )

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:
488
    <snip>
496
    return keccak256(
497
        rlp.encode(
498
            (
499
                tx.nonce,
500
                tx.gas_price,
501
                tx.gas,
502
                tx.to,
503
                tx.value,
504
                tx.data,
505
                chain_id,
506
                Uint(0),
507
                Uint(0),
508
            )
509
        )
510
    )

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:
514
    <snip>
522
    return keccak256(
523
        b"\x01"
524
        + rlp.encode(
525
            (
526
                tx.chain_id,
527
                tx.nonce,
528
                tx.gas_price,
529
                tx.gas,
530
                tx.to,
531
                tx.value,
532
                tx.data,
533
                tx.access_list,
534
            )
535
        )
536
    )

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:
540
    <snip>
548
    return keccak256(
549
        b"\x02"
550
        + rlp.encode(
551
            (
552
                tx.chain_id,
553
                tx.nonce,
554
                tx.max_priority_fee_per_gas,
555
                tx.max_fee_per_gas,
556
                tx.gas,
557
                tx.to,
558
                tx.value,
559
                tx.data,
560
                tx.access_list,
561
            )
562
        )
563
    )

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:
567
    <snip>
575
    assert isinstance(tx, (LegacyTransaction, Bytes))
576
    if isinstance(tx, LegacyTransaction):
577
        return keccak256(rlp.encode(tx))
578
    else:
579
        return keccak256(tx)