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.
GAS_TX_BASE¶
Base cost of a transaction in gas units. This is the minimum amount of gas required to execute a transaction.
| 26 | GAS_TX_BASE = Uint(21000) |
|---|
GAS_TX_DATA_PER_NON_ZERO¶
Gas cost per non-zero byte in the transaction data.
| 32 | GAS_TX_DATA_PER_NON_ZERO = Uint(16) |
|---|
GAS_TX_DATA_PER_ZERO¶
Gas cost per zero byte in the transaction data.
| 37 | GAS_TX_DATA_PER_ZERO = Uint(4) |
|---|
GAS_TX_CREATE¶
Additional gas cost for creating a new contract.
| 42 | GAS_TX_CREATE = Uint(32000) |
|---|
GAS_TX_ACCESS_LIST_ADDRESS¶
Gas cost for including an address in the access list of a transaction.
| 47 | GAS_TX_ACCESS_LIST_ADDRESS = Uint(2400) |
|---|
GAS_TX_ACCESS_LIST_STORAGE_KEY¶
Gas cost for including a storage key in the access list of a transaction.
| 52 | GAS_TX_ACCESS_LIST_STORAGE_KEY = Uint(1900) |
|---|
LegacyTransaction ¶
Atomic operation performed on the block chain. This represents the original transaction format used before EIP-2930.
| 58 | @slotted_freezable |
|---|
| 59 | @dataclass |
|---|
class LegacyTransaction:
nonce¶
A scalar value equal to the number of transactions sent by the sender.
| 68 | nonce: U256 |
|---|
gas_price¶
The price of gas for this transaction, in wei.
| 73 | gas_price: Uint |
|---|
gas¶
The maximum amount of gas that can be used by this transaction.
| 78 | gas: Uint |
|---|
to¶
The address of the recipient. If empty, the transaction is a contract creation.
| 83 | to: Bytes0 | Address |
|---|
value¶
The amount of ether (in wei) to send with this transaction.
| 89 | value: U256 |
|---|
data¶
The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.
| 94 | data: Bytes |
|---|
v¶
The recovery id of the signature.
| 100 | v: U256 |
|---|
r¶
The first part of the signature.
| 105 | r: U256 |
|---|
s¶
The second part of the signature.
| 110 | s: U256 |
|---|
Access ¶
A mapping from account address to storage slots that are pre-warmed as part of a transaction.
| 116 | @slotted_freezable |
|---|
| 117 | @dataclass |
|---|
class Access:
account¶
The address of the account that is accessed.
| 124 | account: Address |
|---|
slots¶
A tuple of storage slots that are accessed in the account.
| 129 | 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.
| 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 |
|---|
Transaction ¶
Atomic operation performed on the block chain.
| 46 | @slotted_freezable |
|---|
| 47 | @dataclass |
|---|
class Transaction:
nonce¶
A scalar value equal to the number of transactions sent by the sender.
| 53 | nonce: U256 |
|---|
gas_price¶
The price of gas for this transaction, in wei.
| 58 | gas_price: Uint |
|---|
gas¶
The maximum amount of gas that can be used by this transaction.
| 63 | gas: Uint |
|---|
to¶
The address of the recipient. If empty, the transaction is a contract creation.
| 68 | to: Bytes0 | Address |
|---|
value¶
The amount of ether (in wei) to send with this transaction.
| 74 | value: U256 |
|---|
data¶
The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.
| 79 | data: Bytes |
|---|
v¶
The recovery id of the signature.
| 85 | v: U256 |
|---|
r¶
The first part of the signature.
| 90 | r: U256 |
|---|
s¶
The second part of the signature.
| 95 | s: U256 |
|---|
Transaction¶
Union type representing any valid transaction type.
| 207 | 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:
| 214 | """ |
|---|---|
| 215 | Encode a transaction into its RLP or typed transaction format. |
| 216 | Needed because non-legacy transactions aren't RLP. |
| 217 | |
| 218 | Legacy transactions are returned as-is, while other transaction types |
| 219 | are prefixed with their type identifier and RLP encoded. |
| 220 | """ |
| 221 | if isinstance(tx, LegacyTransaction): |
| 222 | return tx |
| 223 | elif isinstance(tx, AccessListTransaction): |
| 224 | return b"\x01" + rlp.encode(tx) |
| 225 | else: |
| 226 | 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:
| 230 | """ |
|---|---|
| 231 | Decode a transaction from its RLP or typed transaction format. |
| 232 | Needed because non-legacy transactions aren't RLP. |
| 233 | |
| 234 | Legacy transactions are returned as-is, while other transaction types |
| 235 | are decoded based on their type identifier prefix. |
| 236 | """ |
| 237 | if isinstance(tx, Bytes): |
| 238 | if tx[0] != 1: |
| 239 | raise TransactionTypeError(tx[0]) |
| 240 | return rlp.decode_to(AccessListTransaction, tx[1:]) |
| 241 | else: |
| 242 | 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:
| 246 | """ |
|---|---|
| 247 | Verifies a transaction. |
| 248 | |
| 249 | The gas in a transaction gets used to pay for the intrinsic cost of |
| 250 | operations, therefore if there is insufficient gas then it would not |
| 251 | be possible to execute a transaction and it will be declared invalid. |
| 252 | |
| 253 | Additionally, the nonce of a transaction must not equal or exceed the |
| 254 | limit defined in [EIP-2681]. |
| 255 | In practice, defining the limit as ``2**64-1`` has no impact because |
| 256 | sending ``2**64-1`` transactions is improbable. It's not strictly |
| 257 | impossible though, ``2**64-1`` transactions is the entire capacity of the |
| 258 | Ethereum blockchain at 2022 gas limits for a little over 22 years. |
| 259 | |
| 260 | This function takes a transaction as a parameter and returns the intrinsic |
| 261 | gas cost of the transaction after validation. It throws an |
| 262 | `InsufficientTransactionGasError` exception if the transaction does not |
| 263 | provide enough gas to cover the intrinsic cost, and a `NonceOverflowError` |
| 264 | exception if the nonce is greater than `2**64 - 2`. |
| 265 | |
| 266 | [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681 |
| 267 | """ |
| 268 | intrinsic_gas = calculate_intrinsic_cost(tx) |
| 269 | if intrinsic_gas > tx.gas: |
| 270 | raise InsufficientTransactionGasError("Insufficient gas") |
| 271 | if U256(tx.nonce) >= U256(U64.MAX_VALUE): |
| 272 | raise NonceOverflowError("Nonce too high") |
| 273 | 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 (
GAS_TX_BASE)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:
| 277 | """ |
|---|---|
| 278 | Calculates the gas that is charged before execution is started. |
| 279 | |
| 280 | The intrinsic cost of the transaction is charged before execution has |
| 281 | begun. Functions/operations in the EVM cost money to execute so this |
| 282 | intrinsic cost is for the operations that need to be paid for as part of |
| 283 | the transaction. Data transfer, for example, is part of this intrinsic |
| 284 | cost. It costs ether to send data over the wire and that ether is |
| 285 | accounted for in the intrinsic cost calculated in this function. This |
| 286 | intrinsic cost must be calculated and paid for before execution in order |
| 287 | for all operations to be implemented. |
| 288 | |
| 289 | The intrinsic cost includes: |
| 290 | 1. Base cost (`GAS_TX_BASE`) |
| 291 | 2. Cost for data (zero and non-zero bytes) |
| 292 | 3. Cost for contract creation (if applicable) |
| 293 | 4. Cost for access list entries (if applicable) |
| 294 | |
| 295 | This function takes a transaction as a parameter and returns the intrinsic |
| 296 | gas cost of the transaction. |
| 297 | """ |
| 298 | data_cost = Uint(0) |
| 299 | |
| 300 | for byte in tx.data: |
| 301 | if byte == 0: |
| 302 | data_cost += GAS_TX_DATA_PER_ZERO |
| 303 | else: |
| 304 | data_cost += GAS_TX_DATA_PER_NON_ZERO |
| 305 | |
| 306 | if tx.to == Bytes0(b""): |
| 307 | create_cost = GAS_TX_CREATE |
| 308 | else: |
| 309 | create_cost = Uint(0) |
| 310 | |
| 166 | return GAS_TX_BASE + data_cost + create_cost |
| 311 | access_list_cost = Uint(0) |
| 312 | if isinstance(tx, AccessListTransaction): |
| 313 | for access in tx.access_list: |
| 314 | access_list_cost += GAS_TX_ACCESS_LIST_ADDRESS |
| 315 | access_list_cost += ( |
| 316 | ulen(access.slots) * GAS_TX_ACCESS_LIST_STORAGE_KEY |
| 317 | ) |
| 318 | |
| 319 | return GAS_TX_BASE + 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:
| 323 | """ |
|---|---|
| 324 | Extracts the sender address from a transaction. |
| 325 | |
| 326 | The v, r, and s values are the three parts that make up the signature |
| 327 | of a transaction. In order to recover the sender of a transaction the two |
| 328 | components needed are the signature (``v``, ``r``, and ``s``) and the |
| 329 | signing hash of the transaction. The sender's public key can be obtained |
| 330 | with these two values and therefore the sender address can be retrieved. |
| 331 | |
| 332 | This function takes chain_id and a transaction as parameters and returns |
| 333 | the address of the sender of the transaction. It raises an |
| 334 | `InvalidSignatureError` if the signature values (r, s, v) are invalid. |
| 335 | """ |
| 183 | v, r, s = tx.v, tx.r, tx.s |
| 336 | r, s = tx.r, tx.s |
| 337 | if U256(0) >= r or r >= SECP256K1N: |
| 338 | raise InvalidSignatureError("bad r") |
| 339 | if U256(0) >= s or s > SECP256K1N // U256(2): |
| 340 | raise InvalidSignatureError("bad s") |
| 341 | |
| 189 | if v == 27 or v == 28: |
| 190 | public_key = secp256k1_recover( |
| 191 | r, s, v - U256(27), signing_hash_pre155(tx) |
| 192 | ) |
| 193 | else: |
| 194 | chain_id_x2 = U256(chain_id) * U256(2) |
| 195 | if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2: |
| 196 | raise InvalidSignatureError("bad v") |
| 197 | public_key = secp256k1_recover( |
| 198 | r, s, v - U256(35) - chain_id_x2, signing_hash_155(tx, chain_id) |
| 199 | ) |
| 342 | if isinstance(tx, LegacyTransaction): |
| 343 | v = tx.v |
| 344 | if v == 27 or v == 28: |
| 345 | public_key = secp256k1_recover( |
| 346 | r, s, v - U256(27), signing_hash_pre155(tx) |
| 347 | ) |
| 348 | else: |
| 349 | chain_id_x2 = U256(chain_id) * U256(2) |
| 350 | if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2: |
| 351 | raise InvalidSignatureError("bad v") |
| 352 | public_key = secp256k1_recover( |
| 353 | r, |
| 354 | s, |
| 355 | v - U256(35) - chain_id_x2, |
| 356 | signing_hash_155(tx, chain_id), |
| 357 | ) |
| 358 | elif isinstance(tx, AccessListTransaction): |
| 359 | if tx.y_parity not in (U256(0), U256(1)): |
| 360 | raise InvalidSignatureError("bad y_parity") |
| 361 | public_key = secp256k1_recover( |
| 362 | r, s, tx.y_parity, signing_hash_2930(tx) |
| 363 | ) |
| 364 | |
| 365 | 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:
| 369 | """ |
|---|---|
| 370 | Compute the hash of a transaction used in a legacy (pre [EIP-155]) |
| 371 | signature. |
| 372 | |
| 373 | This function takes a transaction as a parameter and returns the |
| 210 | signing hash of the transaction. |
| 374 | hash of the transaction used in a legacy signature. |
| 375 | |
| 376 | [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 |
| 377 | """ |
| 378 | return keccak256( |
| 379 | rlp.encode( |
| 380 | ( |
| 381 | tx.nonce, |
| 382 | tx.gas_price, |
| 383 | tx.gas, |
| 384 | tx.to, |
| 385 | tx.value, |
| 386 | tx.data, |
| 387 | ) |
| 388 | ) |
| 389 | ) |
signing_hash_155 ¶
def signing_hash_155(tx: Transaction, chain_id: U64) -> Hash32:
| 393 | """ |
|---|---|
| 394 | Compute the hash of a transaction used in a [EIP-155] signature. |
| 395 | |
| 396 | This function takes a transaction and chain ID as parameters and returns |
| 397 | the hash of the transaction used in a [EIP-155] signature. |
| 398 | |
| 399 | [EIP-155]: https://eips.ethereum.org/EIPS/eip-155 |
| 400 | """ |
| 401 | return keccak256( |
| 402 | rlp.encode( |
| 403 | ( |
| 404 | tx.nonce, |
| 405 | tx.gas_price, |
| 406 | tx.gas, |
| 407 | tx.to, |
| 408 | tx.value, |
| 409 | tx.data, |
| 410 | chain_id, |
| 411 | Uint(0), |
| 412 | Uint(0), |
| 413 | ) |
| 414 | ) |
| 415 | ) |
signing_hash_2930 ¶
def signing_hash_2930(tx: AccessListTransaction) -> Hash32:
| 419 | """ |
|---|---|
| 420 | Compute the hash of a transaction used in a [EIP-2930] signature. |
| 421 | |
| 422 | This function takes an access list transaction as a parameter |
| 423 | and returns the hash of the transaction used in an [EIP-2930] signature. |
| 424 | |
| 425 | [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 |
| 426 | """ |
| 427 | return keccak256( |
| 428 | b"\x01" |
| 429 | + rlp.encode( |
| 430 | ( |
| 431 | tx.chain_id, |
| 432 | tx.nonce, |
| 433 | tx.gas_price, |
| 434 | tx.gas, |
| 435 | tx.to, |
| 436 | tx.value, |
| 437 | tx.data, |
| 438 | tx.access_list, |
| 439 | ) |
| 440 | ) |
| 441 | ) |
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:
| 445 | """ |
|---|---|
| 446 | Compute the hash of a transaction. |
| 447 | |
| 448 | This function takes a transaction as a parameter and returns the |
| 259 | hash of the transaction. |
| 449 | keccak256 hash of the transaction. It can handle both legacy transactions |
| 450 | and typed transactions (eg. `AccessListTransaction`). |
| 451 | """ |
| 261 | return keccak256(rlp.encode(tx)) |
| 452 | assert isinstance(tx, (LegacyTransaction, Bytes)) |
| 453 | if isinstance(tx, LegacyTransaction): |
| 454 | return keccak256(rlp.encode(tx)) |
| 455 | else: |
| 456 | return keccak256(tx) |