ethereum.berlin.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
 33from ..utils.ensure import ensure
 34
 35Address = Bytes20
 36Root = Hash32
 37
 38Bloom = Bytes256
 39
 40TX_BASE_COST = 21000
 41TX_DATA_COST_PER_NON_ZERO = 16
 42TX_DATA_COST_PER_ZERO = 4
 43TX_CREATE_COST = 32000
 44TX_ACCESS_LIST_ADDRESS_COST = 2400
 45TX_ACCESS_LIST_STORAGE_KEY_COST = 1900
 46
 47
 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
 64
 65
 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
 84
 85
 86Transaction = Union[LegacyTransaction, AccessListTransaction]
 87
 88
 89def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
 90    """
 91    Encode a transaction. Needed because non-legacy transactions aren't RLP.
 92    """
 93    if isinstance(tx, LegacyTransaction):
 94        return tx
 95    elif isinstance(tx, AccessListTransaction):
 96        return b"\x01" + rlp.encode(tx)
 97    else:
 98        raise Exception(f"Unable to encode transaction of type {type(tx)}")
 99
100
101def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
102    """
103    Decode a transaction. Needed because non-legacy transactions aren't RLP.
104    """
105    if isinstance(tx, Bytes):
106        ensure(tx[0] == 1, InvalidBlock)
107        return rlp.decode_to(AccessListTransaction, tx[1:])
108    else:
109        return tx
110
111
112@slotted_freezable
113@dataclass
114class Account:
115    """
116    State associated with an address.
117    """
118
119    nonce: Uint
120    balance: U256
121    code: bytes
122
123
124EMPTY_ACCOUNT = Account(
125    nonce=Uint(0),
126    balance=U256(0),
127    code=bytearray(),
128)
129
130
131def encode_account(raw_account_data: Account, storage_root: Bytes) -> Bytes:
132    """
133    Encode `Account` dataclass.
134
135    Storage is not stored in the `Account` dataclass, so `Accounts` cannot be
136    encoded with providing a storage root.
137    """
138    return rlp.encode(
139        (
140            raw_account_data.nonce,
141            raw_account_data.balance,
142            storage_root,
143            keccak256(raw_account_data.code),
144        )
145    )
146
147
148@slotted_freezable
149@dataclass
150class Header:
151    """
152    Header portion of a block on the chain.
153    """
154
155    parent_hash: Hash32
156    ommers_hash: Hash32
157    coinbase: Address
158    state_root: Root
159    transactions_root: Root
160    receipt_root: Root
161    bloom: Bloom
162    difficulty: Uint
163    number: Uint
164    gas_limit: Uint
165    gas_used: Uint
166    timestamp: U256
167    extra_data: Bytes
168    mix_digest: Bytes32
169    nonce: Bytes8
170
171
172@slotted_freezable
173@dataclass
174class Block:
175    """
176    A complete block.
177    """
178
179    header: Header
180    transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
181    ommers: Tuple[Header, ...]
182
183
184@slotted_freezable
185@dataclass
186class Log:
187    """
188    Data record produced during the execution of a transaction.
189    """
190
191    address: Address
192    topics: Tuple[Hash32, ...]
193    data: bytes
194
195
196@slotted_freezable
197@dataclass
198class Receipt:
199    """
200    Result of a transaction.
201    """
202
203    succeeded: bool
204    cumulative_gas_used: Uint
205    bloom: Bloom
206    logs: Tuple[Log, ...]
@slotted_freezable
@dataclass
class LegacyTransaction:
49@slotted_freezable
50@dataclass
51class LegacyTransaction:
52    """
53    Atomic operation performed on the block chain.
54    """
55
56    nonce: U256
57    gas_price: Uint
58    gas: Uint
59    to: Union[Bytes0, Address]
60    value: U256
61    data: Bytes
62    v: U256
63    r: U256
64    s: U256

Atomic operation performed on the block chain.

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

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

def encode_transaction( tx: Union[LegacyTransaction, AccessListTransaction]) -> Union[LegacyTransaction, bytes]:
90def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
91    """
92    Encode a transaction. Needed because non-legacy transactions aren't RLP.
93    """
94    if isinstance(tx, LegacyTransaction):
95        return tx
96    elif isinstance(tx, AccessListTransaction):
97        return b"\x01" + rlp.encode(tx)
98    else:
99        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]:
102def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
103    """
104    Decode a transaction. Needed because non-legacy transactions aren't RLP.
105    """
106    if isinstance(tx, Bytes):
107        ensure(tx[0] == 1, InvalidBlock)
108        return rlp.decode_to(AccessListTransaction, tx[1:])
109    else:
110        return tx

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

@slotted_freezable
@dataclass
class Account:
113@slotted_freezable
114@dataclass
115class Account:
116    """
117    State associated with an address.
118    """
119
120    nonce: Uint
121    balance: U256
122    code: bytes

State associated with an address.

def encode_account( raw_account_data: Account, storage_root: bytes) -> bytes:
132def encode_account(raw_account_data: Account, storage_root: Bytes) -> Bytes:
133    """
134    Encode `Account` dataclass.
135
136    Storage is not stored in the `Account` dataclass, so `Accounts` cannot be
137    encoded with providing a storage root.
138    """
139    return rlp.encode(
140        (
141            raw_account_data.nonce,
142            raw_account_data.balance,
143            storage_root,
144            keccak256(raw_account_data.code),
145        )
146    )

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:
173@slotted_freezable
174@dataclass
175class Block:
176    """
177    A complete block.
178    """
179
180    header: Header
181    transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
182    ommers: Tuple[Header, ...]

A complete block.

@slotted_freezable
@dataclass
class Log:
185@slotted_freezable
186@dataclass
187class Log:
188    """
189    Data record produced during the execution of a transaction.
190    """
191
192    address: Address
193    topics: Tuple[Hash32, ...]
194    data: bytes

Data record produced during the execution of a transaction.

@slotted_freezable
@dataclass
class Receipt:
197@slotted_freezable
198@dataclass
199class Receipt:
200    """
201    Result of a transaction.
202    """
203
204    succeeded: bool
205    cumulative_gas_used: Uint
206    bloom: Bloom
207    logs: Tuple[Log, ...]

Result of a transaction.