ethereum.london.fork_types

Ethereum Types ^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Types re-used throughout the specification, which are specific to Ethereum.

  1"""
  2Ethereum Types
  3^^^^^^^^^^^^^^
  4
  5.. contents:: Table of Contents
  6    :backlinks: none
  7    :local:
  8
  9Introduction
 10------------
 11
 12Types re-used throughout the specification, which are specific to Ethereum.
 13"""
 14
 15from dataclasses import dataclass
 16from typing import Tuple, Union
 17
 18from .. import rlp
 19from ..base_types import (
 20    U64,
 21    U256,
 22    Bytes,
 23    Bytes0,
 24    Bytes8,
 25    Bytes20,
 26    Bytes32,
 27    Bytes256,
 28    Uint,
 29    slotted_freezable,
 30)
 31from ..crypto.hash import Hash32, keccak256
 32from ..exceptions import InvalidBlock
 33
 34Address = Bytes20
 35Root = Hash32
 36
 37Bloom = Bytes256
 38
 39TX_BASE_COST = 21000
 40TX_DATA_COST_PER_NON_ZERO = 16
 41TX_DATA_COST_PER_ZERO = 4
 42TX_CREATE_COST = 32000
 43TX_ACCESS_LIST_ADDRESS_COST = 2400
 44TX_ACCESS_LIST_STORAGE_KEY_COST = 1900
 45
 46
 47@slotted_freezable
 48@dataclass
 49class LegacyTransaction:
 50    """
 51    Atomic operation performed on the block chain.
 52    """
 53
 54    nonce: U256
 55    gas_price: Uint
 56    gas: Uint
 57    to: Union[Bytes0, Address]
 58    value: U256
 59    data: Bytes
 60    v: U256
 61    r: U256
 62    s: U256
 63
 64
 65@slotted_freezable
 66@dataclass
 67class AccessListTransaction:
 68    """
 69    The transaction type added in EIP-2930 to support access lists.
 70    """
 71
 72    chain_id: U64
 73    nonce: U256
 74    gas_price: Uint
 75    gas: Uint
 76    to: Union[Bytes0, Address]
 77    value: U256
 78    data: Bytes
 79    access_list: Tuple[Tuple[Address, Tuple[Bytes32, ...]], ...]
 80    y_parity: U256
 81    r: U256
 82    s: U256
 83
 84
 85@slotted_freezable
 86@dataclass
 87class FeeMarketTransaction:
 88    """
 89    The transaction type added in EIP-1559.
 90    """
 91
 92    chain_id: U64
 93    nonce: U256
 94    max_priority_fee_per_gas: Uint
 95    max_fee_per_gas: Uint
 96    gas: Uint
 97    to: Union[Bytes0, Address]
 98    value: U256
 99    data: Bytes
100    access_list: Tuple[Tuple[Address, Tuple[Bytes32, ...]], ...]
101    y_parity: U256
102    r: U256
103    s: U256
104
105
106Transaction = Union[
107    LegacyTransaction, AccessListTransaction, FeeMarketTransaction
108]
109
110
111def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
112    """
113    Encode a transaction. Needed because non-legacy transactions aren't RLP.
114    """
115    if isinstance(tx, LegacyTransaction):
116        return tx
117    elif isinstance(tx, AccessListTransaction):
118        return b"\x01" + rlp.encode(tx)
119    elif isinstance(tx, FeeMarketTransaction):
120        return b"\x02" + rlp.encode(tx)
121    else:
122        raise Exception(f"Unable to encode transaction of type {type(tx)}")
123
124
125def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
126    """
127    Decode a transaction. Needed because non-legacy transactions aren't RLP.
128    """
129    if isinstance(tx, Bytes):
130        if tx[0] == 1:
131            return rlp.decode_to(AccessListTransaction, tx[1:])
132        elif tx[0] == 2:
133            return rlp.decode_to(FeeMarketTransaction, tx[1:])
134        else:
135            raise InvalidBlock
136    else:
137        return tx
138
139
140@slotted_freezable
141@dataclass
142class Account:
143    """
144    State associated with an address.
145    """
146
147    nonce: Uint
148    balance: U256
149    code: bytes
150
151
152EMPTY_ACCOUNT = Account(
153    nonce=Uint(0),
154    balance=U256(0),
155    code=bytearray(),
156)
157
158
159def encode_account(raw_account_data: Account, storage_root: Bytes) -> Bytes:
160    """
161    Encode `Account` dataclass.
162
163    Storage is not stored in the `Account` dataclass, so `Accounts` cannot be
164    encoded with providing a storage root.
165    """
166    return rlp.encode(
167        (
168            raw_account_data.nonce,
169            raw_account_data.balance,
170            storage_root,
171            keccak256(raw_account_data.code),
172        )
173    )
174
175
176@slotted_freezable
177@dataclass
178class Header:
179    """
180    Header portion of a block on the chain.
181    """
182
183    parent_hash: Hash32
184    ommers_hash: Hash32
185    coinbase: Address
186    state_root: Root
187    transactions_root: Root
188    receipt_root: Root
189    bloom: Bloom
190    difficulty: Uint
191    number: Uint
192    gas_limit: Uint
193    gas_used: Uint
194    timestamp: U256
195    extra_data: Bytes
196    mix_digest: Bytes32
197    nonce: Bytes8
198    base_fee_per_gas: Uint
199
200
201@slotted_freezable
202@dataclass
203class Block:
204    """
205    A complete block.
206    """
207
208    header: Header
209    transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
210    ommers: Tuple[Header, ...]
211
212
213@slotted_freezable
214@dataclass
215class Log:
216    """
217    Data record produced during the execution of a transaction.
218    """
219
220    address: Address
221    topics: Tuple[Hash32, ...]
222    data: bytes
223
224
225@slotted_freezable
226@dataclass
227class Receipt:
228    """
229    Result of a transaction.
230    """
231
232    succeeded: bool
233    cumulative_gas_used: Uint
234    bloom: Bloom
235    logs: Tuple[Log, ...]
@slotted_freezable
@dataclass
class LegacyTransaction:
48@slotted_freezable
49@dataclass
50class LegacyTransaction:
51    """
52    Atomic operation performed on the block chain.
53    """
54
55    nonce: U256
56    gas_price: Uint
57    gas: Uint
58    to: Union[Bytes0, Address]
59    value: U256
60    data: Bytes
61    v: U256
62    r: U256
63    s: U256

Atomic operation performed on the block chain.

@slotted_freezable
@dataclass
class AccessListTransaction:
66@slotted_freezable
67@dataclass
68class AccessListTransaction:
69    """
70    The transaction type added in EIP-2930 to support access lists.
71    """
72
73    chain_id: U64
74    nonce: U256
75    gas_price: Uint
76    gas: Uint
77    to: Union[Bytes0, Address]
78    value: U256
79    data: Bytes
80    access_list: Tuple[Tuple[Address, Tuple[Bytes32, ...]], ...]
81    y_parity: U256
82    r: U256
83    s: U256

The transaction type added in EIP-2930 to support access lists.

@slotted_freezable
@dataclass
class FeeMarketTransaction:
 86@slotted_freezable
 87@dataclass
 88class FeeMarketTransaction:
 89    """
 90    The transaction type added in EIP-1559.
 91    """
 92
 93    chain_id: U64
 94    nonce: U256
 95    max_priority_fee_per_gas: Uint
 96    max_fee_per_gas: Uint
 97    gas: Uint
 98    to: Union[Bytes0, Address]
 99    value: U256
100    data: Bytes
101    access_list: Tuple[Tuple[Address, Tuple[Bytes32, ...]], ...]
102    y_parity: U256
103    r: U256
104    s: U256

The transaction type added in EIP-1559.

def encode_transaction( tx: Union[LegacyTransaction, AccessListTransaction, FeeMarketTransaction]) -> Union[LegacyTransaction, bytes]:
112def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
113    """
114    Encode a transaction. Needed because non-legacy transactions aren't RLP.
115    """
116    if isinstance(tx, LegacyTransaction):
117        return tx
118    elif isinstance(tx, AccessListTransaction):
119        return b"\x01" + rlp.encode(tx)
120    elif isinstance(tx, FeeMarketTransaction):
121        return b"\x02" + rlp.encode(tx)
122    else:
123        raise Exception(f"Unable to encode transaction of type {type(tx)}")

Encode a transaction. Needed because non-legacy transactions aren't RLP.

def decode_transaction( tx: Union[LegacyTransaction, bytes]) -> Union[LegacyTransaction, AccessListTransaction, FeeMarketTransaction]:
126def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
127    """
128    Decode a transaction. Needed because non-legacy transactions aren't RLP.
129    """
130    if isinstance(tx, Bytes):
131        if tx[0] == 1:
132            return rlp.decode_to(AccessListTransaction, tx[1:])
133        elif tx[0] == 2:
134            return rlp.decode_to(FeeMarketTransaction, tx[1:])
135        else:
136            raise InvalidBlock
137    else:
138        return tx

Decode a transaction. Needed because non-legacy transactions aren't RLP.

@slotted_freezable
@dataclass
class Account:
141@slotted_freezable
142@dataclass
143class Account:
144    """
145    State associated with an address.
146    """
147
148    nonce: Uint
149    balance: U256
150    code: bytes

State associated with an address.

def encode_account( raw_account_data: Account, storage_root: bytes) -> bytes:
160def encode_account(raw_account_data: Account, storage_root: Bytes) -> Bytes:
161    """
162    Encode `Account` dataclass.
163
164    Storage is not stored in the `Account` dataclass, so `Accounts` cannot be
165    encoded with providing a storage root.
166    """
167    return rlp.encode(
168        (
169            raw_account_data.nonce,
170            raw_account_data.balance,
171            storage_root,
172            keccak256(raw_account_data.code),
173        )
174    )

Encode Account dataclass.

Storage is not stored in the Account dataclass, so Accounts cannot be encoded with providing a storage root.

@slotted_freezable
@dataclass
class Block:
202@slotted_freezable
203@dataclass
204class Block:
205    """
206    A complete block.
207    """
208
209    header: Header
210    transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
211    ommers: Tuple[Header, ...]

A complete block.

@slotted_freezable
@dataclass
class Log:
214@slotted_freezable
215@dataclass
216class Log:
217    """
218    Data record produced during the execution of a transaction.
219    """
220
221    address: Address
222    topics: Tuple[Hash32, ...]
223    data: bytes

Data record produced during the execution of a transaction.

@slotted_freezable
@dataclass
class Receipt:
226@slotted_freezable
227@dataclass
228class Receipt:
229    """
230    Result of a transaction.
231    """
232
233    succeeded: bool
234    cumulative_gas_used: Uint
235    bloom: Bloom
236    logs: Tuple[Log, ...]

Result of a transaction.