ethereum.paris.transactionsethereum.shanghai.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.
TX_BASE_COST
Base cost of a transaction in gas units. This is the minimum amount of gas required to execute a transaction.
21 | TX_BASE_COST = Uint(21000) |
---|
TX_DATA_COST_PER_NON_ZERO
Gas cost per non-zero byte in the transaction data.
27 | TX_DATA_COST_PER_NON_ZERO = Uint(16) |
---|
TX_DATA_COST_PER_ZERO
Gas cost per zero byte in the transaction data.
32 | TX_DATA_COST_PER_ZERO = Uint(4) |
---|
TX_CREATE_COST
Additional gas cost for creating a new contract.
37 | TX_CREATE_COST = Uint(32000) |
---|
TX_ACCESS_LIST_ADDRESS_COST
Gas cost for including an address in the access list of a transaction.
42 | TX_ACCESS_LIST_ADDRESS_COST = Uint(2400) |
---|
TX_ACCESS_LIST_STORAGE_KEY_COST
Gas cost for including a storage key in the access list of a transaction.
47 | TX_ACCESS_LIST_STORAGE_KEY_COST = Uint(1900) |
---|
LegacyTransaction
53 | @slotted_freezable |
---|
54 | @dataclass |
---|
class LegacyTransaction:
nonce
64 | nonce: U256 |
---|
gas_price
69 | gas_price: Uint |
---|
gas
74 | gas: Uint |
---|
to
79 | to: Union[Bytes0, Address] |
---|
value
85 | value: U256 |
---|
data
90 | data: Bytes |
---|
v
96 | v: U256 |
---|
r
101 | r: U256 |
---|
s
106 | s: U256 |
---|
Access
A mapping from account address to storage slots that are pre-warmed as part of a transaction.
112 | @slotted_freezable |
---|
113 | @dataclass |
---|
class Access:
account
120 | account: Address |
---|
slots
125 | 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.
131 | @slotted_freezable |
---|
132 | @dataclass |
---|
class AccessListTransaction:
chain_id
144 | chain_id: U64 |
---|
nonce
149 | nonce: U256 |
---|
gas_price
154 | gas_price: Uint |
---|
gas
159 | gas: Uint |
---|
to
164 | to: Union[Bytes0, Address] |
---|
value
170 | value: U256 |
---|
data
175 | data: Bytes |
---|
access_list
181 | access_list: Tuple[Access, ...] |
---|
y_parity
187 | y_parity: U256 |
---|
r
192 | r: U256 |
---|
s
197 | 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.
203 | @slotted_freezable |
---|
204 | @dataclass |
---|
class FeeMarketTransaction:
chain_id
215 | chain_id: U64 |
---|
nonce
220 | nonce: U256 |
---|
max_priority_fee_per_gas
225 | max_priority_fee_per_gas: Uint |
---|
max_fee_per_gas
230 | max_fee_per_gas: Uint |
---|
gas
236 | gas: Uint |
---|
to
241 | to: Union[Bytes0, Address] |
---|
value
247 | value: U256 |
---|
data
252 | data: Bytes |
---|
access_list
258 | access_list: Tuple[Access, ...] |
---|
y_parity
264 | y_parity: U256 |
---|
r
269 | r: U256 |
---|
s
274 | s: U256 |
---|
Transaction
Union type representing any valid transaction type.
280 | Transaction = Union[ |
---|---|
281 | LegacyTransaction, AccessListTransaction, FeeMarketTransaction |
282 | ] |
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) -> Union[LegacyTransaction, Bytes]:
289 | """ |
---|---|
290 | Encode a transaction into its RLP or typed transaction format. |
291 | Needed because non-legacy transactions aren't RLP. |
292 |
|
293 | Legacy transactions are returned as-is, while other transaction types |
294 | are prefixed with their type identifier and RLP encoded. |
295 | """ |
296 | if isinstance(tx, LegacyTransaction): |
297 | return tx |
298 | elif isinstance(tx, AccessListTransaction): |
299 | return b"\x01" + rlp.encode(tx) |
300 | elif isinstance(tx, FeeMarketTransaction): |
301 | return b"\x02" + rlp.encode(tx) |
302 | else: |
303 | 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: Union[LegacyTransaction, Bytes]) -> Transaction:
307 | """ |
---|---|
308 | Decode a transaction from its RLP or typed transaction format. |
309 | Needed because non-legacy transactions aren't RLP. |
310 |
|
311 | Legacy transactions are returned as-is, while other transaction types |
312 | are decoded based on their type identifier prefix. |
313 | """ |
314 | if isinstance(tx, Bytes): |
315 | if tx[0] == 1: |
316 | return rlp.decode_to(AccessListTransaction, tx[1:]) |
317 | elif tx[0] == 2: |
318 | return rlp.decode_to(FeeMarketTransaction, tx[1:]) |
319 | else: |
320 | raise TransactionTypeError(tx[0]) |
321 | else: |
322 | 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 of the transaction after validation. It throws an
InvalidTransaction
exception if the transaction is invalid.
def validate_transaction(tx: Transaction) -> Uint:
326 | """ |
---|---|
327 | Verifies a transaction. |
328 |
|
329 | The gas in a transaction gets used to pay for the intrinsic cost of |
330 | operations, therefore if there is insufficient gas then it would not |
331 | be possible to execute a transaction and it will be declared invalid. |
332 |
|
333 | Additionally, the nonce of a transaction must not equal or exceed the |
334 | limit defined in [EIP-2681]. |
335 | In practice, defining the limit as ``2**64-1`` has no impact because |
336 | sending ``2**64-1`` transactions is improbable. It's not strictly |
337 | impossible though, ``2**64-1`` transactions is the entire capacity of the |
338 | Ethereum blockchain at 2022 gas limits for a little over 22 years. |
339 |
|
340 | Also, the code size of a contract creation transaction must be within |
341 | limits of the protocol. |
342 |
|
343 | This function takes a transaction as a parameter and returns the intrinsic |
344 | gas cost of the transaction after validation. It throws an |
345 | `InvalidTransaction` exception if the transaction is invalid. |
346 |
|
347 | [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681 |
348 | """ |
349 | from .vm.interpreter import MAX_CODE_SIZE |
350 | |
351 | intrinsic_gas = calculate_intrinsic_cost(tx) |
352 | if intrinsic_gas > tx.gas: |
353 | raise InvalidTransaction("Insufficient gas") |
354 | if U256(tx.nonce) >= U256(U64.MAX_VALUE): |
355 | raise InvalidTransaction("Nonce too high") |
356 | if tx.to == Bytes0(b"") and len(tx.data) > 2 * MAX_CODE_SIZE: |
357 | raise InvalidTransaction("Code size too large") |
358 | |
359 | 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:
Base cost (
TX_BASE_COST
)Cost for data (zero and non-zero bytes)
Cost for contract creation (if applicable)
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:
363 | """ |
---|---|
364 | Calculates the gas that is charged before execution is started. |
365 |
|
366 | The intrinsic cost of the transaction is charged before execution has |
367 | begun. Functions/operations in the EVM cost money to execute so this |
368 | intrinsic cost is for the operations that need to be paid for as part of |
369 | the transaction. Data transfer, for example, is part of this intrinsic |
370 | cost. It costs ether to send data over the wire and that ether is |
371 | accounted for in the intrinsic cost calculated in this function. This |
372 | intrinsic cost must be calculated and paid for before execution in order |
373 | for all operations to be implemented. |
374 |
|
375 | The intrinsic cost includes: |
376 | 1. Base cost (`TX_BASE_COST`) |
377 | 2. Cost for data (zero and non-zero bytes) |
378 | 3. Cost for contract creation (if applicable) |
379 | 4. Cost for access list entries (if applicable) |
380 |
|
381 | This function takes a transaction as a parameter and returns the intrinsic |
382 | gas cost of the transaction. |
383 | """ |
384 | from .vm.gas import init_code_cost |
385 | |
386 | data_cost = Uint(0) |
387 | |
388 | for byte in tx.data: |
389 | if byte == 0: |
390 | data_cost += TX_DATA_COST_PER_ZERO |
391 | else: |
392 | data_cost += TX_DATA_COST_PER_NON_ZERO |
393 | |
394 | if tx.to == Bytes0(b""): |
385 | create_cost = TX_CREATE_COST |
395 | create_cost = TX_CREATE_COST + init_code_cost(ulen(tx.data)) |
396 | else: |
397 | create_cost = Uint(0) |
398 | |
399 | access_list_cost = Uint(0) |
400 | if isinstance(tx, (AccessListTransaction, FeeMarketTransaction)): |
401 | for access in tx.access_list: |
402 | access_list_cost += TX_ACCESS_LIST_ADDRESS_COST |
403 | access_list_cost += ( |
404 | ulen(access.slots) * TX_ACCESS_LIST_STORAGE_KEY_COST |
405 | ) |
406 | |
407 | return TX_BASE_COST + data_cost + create_cost + access_list_cost |
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:
411 | """ |
---|---|
412 | Extracts the sender address from a transaction. |
413 |
|
414 | The v, r, and s values are the three parts that make up the signature |
415 | of a transaction. In order to recover the sender of a transaction the two |
416 | components needed are the signature (``v``, ``r``, and ``s``) and the |
417 | signing hash of the transaction. The sender's public key can be obtained |
418 | with these two values and therefore the sender address can be retrieved. |
419 |
|
420 | This function takes chain_id and a transaction as parameters and returns |
421 | the address of the sender of the transaction. It raises an |
422 | `InvalidSignatureError` if the signature values (r, s, v) are invalid. |
423 | """ |
424 | r, s = tx.r, tx.s |
425 | if U256(0) >= r or r >= SECP256K1N: |
426 | raise InvalidSignatureError("bad r") |
427 | if U256(0) >= s or s > SECP256K1N // U256(2): |
428 | raise InvalidSignatureError("bad s") |
429 | |
430 | if isinstance(tx, LegacyTransaction): |
431 | v = tx.v |
432 | if v == 27 or v == 28: |
433 | public_key = secp256k1_recover( |
434 | r, s, v - U256(27), signing_hash_pre155(tx) |
435 | ) |
436 | else: |
437 | chain_id_x2 = U256(chain_id) * U256(2) |
438 | if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2: |
439 | raise InvalidSignatureError("bad v") |
440 | public_key = secp256k1_recover( |
441 | r, |
442 | s, |
443 | v - U256(35) - chain_id_x2, |
444 | signing_hash_155(tx, chain_id), |
445 | ) |
446 | elif isinstance(tx, AccessListTransaction): |
447 | if tx.y_parity not in (U256(0), U256(1)): |
448 | raise InvalidSignatureError("bad y_parity") |
449 | public_key = secp256k1_recover( |
450 | r, s, tx.y_parity, signing_hash_2930(tx) |
451 | ) |
452 | elif isinstance(tx, FeeMarketTransaction): |
453 | if tx.y_parity not in (U256(0), U256(1)): |
454 | raise InvalidSignatureError("bad y_parity") |
455 | public_key = secp256k1_recover( |
456 | r, s, tx.y_parity, signing_hash_1559(tx) |
457 | ) |
458 | |
459 | 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:
463 | """ |
---|---|
464 | Compute the hash of a transaction used in a legacy (pre [EIP-155]) |
465 | signature. |
466 |
|
467 | This function takes a legacy transaction as a parameter and returns the |
468 | signing hash of the transaction. |
469 |
|
470 | [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 |
471 | """ |
472 | return keccak256( |
473 | rlp.encode( |
474 | ( |
475 | tx.nonce, |
476 | tx.gas_price, |
477 | tx.gas, |
478 | tx.to, |
479 | tx.value, |
480 | tx.data, |
481 | ) |
482 | ) |
483 | ) |
signing_hash_155
def signing_hash_155(tx: LegacyTransaction, chain_id: U64) -> Hash32:
487 | """ |
---|---|
488 | Compute the hash of a transaction used in a [EIP-155] signature. |
489 |
|
490 | This function takes a legacy transaction and a chain ID as parameters |
491 | and returns the hash of the transaction used in an [EIP-155] signature. |
492 |
|
493 | [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 |
494 | """ |
495 | return keccak256( |
496 | rlp.encode( |
497 | ( |
498 | tx.nonce, |
499 | tx.gas_price, |
500 | tx.gas, |
501 | tx.to, |
502 | tx.value, |
503 | tx.data, |
504 | chain_id, |
505 | Uint(0), |
506 | Uint(0), |
507 | ) |
508 | ) |
509 | ) |
signing_hash_2930
def signing_hash_2930(tx: AccessListTransaction) -> Hash32:
513 | """ |
---|---|
514 | Compute the hash of a transaction used in a [EIP-2930] signature. |
515 |
|
516 | This function takes an access list transaction as a parameter |
517 | and returns the hash of the transaction used in an [EIP-2930] signature. |
518 |
|
519 | [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 |
520 | """ |
521 | return keccak256( |
522 | b"\x01" |
523 | + rlp.encode( |
524 | ( |
525 | tx.chain_id, |
526 | tx.nonce, |
527 | tx.gas_price, |
528 | tx.gas, |
529 | tx.to, |
530 | tx.value, |
531 | tx.data, |
532 | tx.access_list, |
533 | ) |
534 | ) |
535 | ) |
signing_hash_1559
def signing_hash_1559(tx: FeeMarketTransaction) -> Hash32:
539 | """ |
---|---|
540 | Compute the hash of a transaction used in an [EIP-1559] signature. |
541 |
|
542 | This function takes a fee market transaction as a parameter |
543 | and returns the hash of the transaction used in an [EIP-1559] signature. |
544 |
|
545 | [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559 |
546 | """ |
547 | return keccak256( |
548 | b"\x02" |
549 | + rlp.encode( |
550 | ( |
551 | tx.chain_id, |
552 | tx.nonce, |
553 | tx.max_priority_fee_per_gas, |
554 | tx.max_fee_per_gas, |
555 | tx.gas, |
556 | tx.to, |
557 | tx.value, |
558 | tx.data, |
559 | tx.access_list, |
560 | ) |
561 | ) |
562 | ) |
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: Union[Bytes, LegacyTransaction]) -> Hash32:
566 | """ |
---|---|
567 | Compute the hash of a transaction. |
568 |
|
569 | This function takes a transaction as a parameter and returns the |
570 | keccak256 hash of the transaction. It can handle both legacy transactions |
571 | and typed transactions (`AccessListTransaction`, `FeeMarketTransaction`, |
572 | etc.). |
573 | """ |
574 | assert isinstance(tx, (LegacyTransaction, Bytes)) |
575 | if isinstance(tx, LegacyTransaction): |
576 | return keccak256(rlp.encode(tx)) |
577 | else: |
578 | return keccak256(tx) |