ethereum.forks.muir_glacier.transactionsethereum.forks.berlin.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-2930.

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

nonce

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

39
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

44
    gas_price: Uint

gas

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

49
    gas: Uint

to

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

54
    to: Bytes0 | Address

value

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

60
    value: U256

data

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

65
    data: Bytes

v

The recovery id of the signature.

71
    v: U256

r

The first part of the signature.

76
    r: U256

s

The second part of the signature.

81
    s: U256

Access

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

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

account

The address of the account that is accessed.

96
    account: Address

slots

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

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

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

chain_id

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

121
    chain_id: U64

nonce

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

126
    nonce: U256

gas_price

The price of gas for this transaction.

131
    gas_price: Uint

gas

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

136
    gas: Uint

to

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

141
    to: Bytes0 | Address

value

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

147
    value: U256

data

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

152
    data: Bytes

access_list

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

158
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

164
    y_parity: U256

r

The first part of the signature.

169
    r: U256

s

The second part of the signature.

174
    s: U256

Transaction

Atomic operation performed on the block chain.

25
@final
26
@slotted_freezable
27
@dataclass
class Transaction:

nonce

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

33
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

38
    gas_price: Uint

gas

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

43
    gas: Uint

to

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

48
    to: Bytes0 | Address

value

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

54
    value: U256

data

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

59
    data: Bytes

v

The recovery id of the signature.

65
    v: U256

r

The first part of the signature.

70
    r: U256

s

The second part of the signature.

75
    s: U256

Transaction

Union type representing any valid transaction type.

180
Transaction = LegacyTransaction | AccessListTransaction

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:
187
    <snip>
194
    if isinstance(tx, LegacyTransaction):
195
        return tx
196
    elif isinstance(tx, AccessListTransaction):
197
        return b"\x01" + rlp.encode(tx)
198
    else:
199
        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:
203
    <snip>
210
    if isinstance(tx, Bytes):
211
        if len(tx) == 0:
212
            raise InvalidBlock("empty transaction")
213
        if tx[0] != 1:
214
            raise TransactionTypeError(tx[0])
215
        return rlp.decode_to(AccessListTransaction, tx[1:])
216
    else:
217
        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.

def validate_transaction(​tx: Transaction​) -> Uint:
221
    <snip>
243
    intrinsic_gas = calculate_intrinsic_cost(tx)
244
    if intrinsic_gas > tx.gas:
245
        raise InsufficientTransactionGasError("Insufficient gas")
246
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
247
        raise NonceOverflowError("Nonce too high")
248
    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:
252
    <snip>
273
    from .vm.gas import GasCosts
274
275
    num_zeros = Uint(tx.data.count(0))
276
    num_non_zeros = ulen(tx.data) - num_zeros
277
    data_cost = (
278
        num_zeros * GasCosts.TX_DATA_PER_ZERO
279
        + num_non_zeros * GasCosts.TX_DATA_PER_NON_ZERO
280
    )
281
282
    if tx.to == Bytes0(b""):
283
        create_cost = GasCosts.TX_CREATE
284
    else:
285
        create_cost = Uint(0)
286
147
    return GasCosts.TX_BASE + data_cost + create_cost
287
    access_list_cost = Uint(0)
288
    if isinstance(tx, AccessListTransaction):
289
        for access in tx.access_list:
290
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
291
            access_list_cost += (
292
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
293
            )
294
295
    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:
299
    <snip>
156
    if tx.v == 27 or tx.v == 28:
157
        return None
158
    if tx.v < U256(35):
159
        raise InvalidSignatureError("bad v")
160
    tx_chain_id = (tx.v - U256(35)) >> U256(1)
161
    if tx_chain_id > U256(U64.MAX_VALUE):
162
        raise InvalidSignatureError("bad v")
163
    return U64(tx_chain_id)
304
    if isinstance(tx, LegacyTransaction):
305
        if tx.v == 27 or tx.v == 28:
306
            return None
307
308
        if tx.v < U256(35):
309
            raise InvalidSignatureError("bad v")
310
311
        tx_chain_id = (tx.v - U256(35)) >> U256(1)
312
        if tx_chain_id > U256(U64.MAX_VALUE):
313
            raise InvalidSignatureError("bad v")
314
        return U64(tx_chain_id)
315
    else:
316
        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:
320
    <snip>
180
    v, r, s = tx.v, tx.r, tx.s
333
    r, s = tx.r, tx.s
334
    if U256(0) >= r or r >= SECP256K1N:
335
        raise InvalidSignatureError("bad r")
336
    if U256(0) >= s or s > SECP256K1N // U256(2):
337
        raise InvalidSignatureError("bad s")
338
186
    if v == 27 or v == 28:
187
        public_key = secp256k1_recover(
188
            r, s, v - U256(27), signing_hash_pre155(tx)
189
        )
190
    else:
191
        assert v >= U256(35), "call chain_id before recover_sender"
192
        tx_chain_id = U64((v - U256(35)) >> U256(1))
193
        v = (v - U256(35)) & U256(1)
194
        public_key = secp256k1_recover(
195
            r, s, v, signing_hash_155(tx, tx_chain_id)
196
        )
339
    if isinstance(tx, LegacyTransaction):
340
        v = tx.v
341
        if v == 27 or v == 28:
342
            public_key = secp256k1_recover(
343
                r, s, v - U256(27), signing_hash_pre155(tx)
344
            )
345
        else:
346
            assert v >= U256(35), "call chain_id before recover_sender"
347
            tx_chain_id = U64((v - U256(35)) >> U256(1))
348
            v = (v - U256(35)) & U256(1)
349
            public_key = secp256k1_recover(
350
                r,
351
                s,
352
                v,
353
                signing_hash_155(tx, tx_chain_id),
354
            )
355
    elif isinstance(tx, AccessListTransaction):
356
        if tx.y_parity not in (U256(0), U256(1)):
357
            raise InvalidSignatureError("bad y_parity")
358
        public_key = secp256k1_recover(
359
            r, s, tx.y_parity, signing_hash_2930(tx)
360
        )
361
362
    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 transaction as a parameter and returns the signing hash of the transaction.hash of the transaction used in a legacy signature.

def signing_hash_pre155(​tx: Transaction​) -> Hash32:
366
    <snip>
375
    return keccak256(
376
        rlp.encode(
377
            (
378
                tx.nonce,
379
                tx.gas_price,
380
                tx.gas,
381
                tx.to,
382
                tx.value,
383
                tx.data,
384
            )
385
        )
386
    )

signing_hash_155

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

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

def signing_hash_155(​tx: Transaction, ​​chain_id: U64​) -> Hash32:
390
    <snip>
398
    return keccak256(
399
        rlp.encode(
400
            (
401
                tx.nonce,
402
                tx.gas_price,
403
                tx.gas,
404
                tx.to,
405
                tx.value,
406
                tx.data,
407
                chain_id,
408
                Uint(0),
409
                Uint(0),
410
            )
411
        )
412
    )

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:
416
    <snip>
424
    return keccak256(
425
        b"\x01"
426
        + rlp.encode(
427
            (
428
                tx.chain_id,
429
                tx.nonce,
430
                tx.gas_price,
431
                tx.gas,
432
                tx.to,
433
                tx.value,
434
                tx.data,
435
                tx.access_list,
436
            )
437
        )
438
    )

get_transaction_hash

Compute the hash of a transaction.

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

def get_transaction_hash(​tx: TransactionBytes | LegacyTransaction​) -> Hash32:
442
    <snip>
258
    return keccak256(rlp.encode(tx))
449
    assert isinstance(tx, (LegacyTransaction, Bytes))
450
    if isinstance(tx, LegacyTransaction):
451
        return keccak256(rlp.encode(tx))
452
    else:
453
        return keccak256(tx)