ethereum.shanghai.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 Withdrawal: 179 """ 180 Withdrawals that have been validated on the consensus layer. 181 """ 182 183 index: U64 184 validator_index: U64 185 address: Address 186 amount: U256 187 188 189@slotted_freezable 190@dataclass 191class Header: 192 """ 193 Header portion of a block on the chain. 194 """ 195 196 parent_hash: Hash32 197 ommers_hash: Hash32 198 coinbase: Address 199 state_root: Root 200 transactions_root: Root 201 receipt_root: Root 202 bloom: Bloom 203 difficulty: Uint 204 number: Uint 205 gas_limit: Uint 206 gas_used: Uint 207 timestamp: U256 208 extra_data: Bytes 209 prev_randao: Bytes32 210 nonce: Bytes8 211 base_fee_per_gas: Uint 212 withdrawals_root: Root 213 214 215@slotted_freezable 216@dataclass 217class Block: 218 """ 219 A complete block. 220 """ 221 222 header: Header 223 transactions: Tuple[Union[Bytes, LegacyTransaction], ...] 224 ommers: Tuple[Header, ...] 225 withdrawals: Tuple[Withdrawal, ...] 226 227 228@slotted_freezable 229@dataclass 230class Log: 231 """ 232 Data record produced during the execution of a transaction. 233 """ 234 235 address: Address 236 topics: Tuple[Hash32, ...] 237 data: bytes 238 239 240@slotted_freezable 241@dataclass 242class Receipt: 243 """ 244 Result of a transaction. 245 """ 246 247 succeeded: bool 248 cumulative_gas_used: Uint 249 bloom: Bloom 250 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.
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 )
@slotted_freezable
@dataclass
class
Withdrawal:
177@slotted_freezable 178@dataclass 179class Withdrawal: 180 """ 181 Withdrawals that have been validated on the consensus layer. 182 """ 183 184 index: U64 185 validator_index: U64 186 address: Address 187 amount: U256
Withdrawals that have been validated on the consensus layer.
@slotted_freezable
@dataclass
class
Header:
190@slotted_freezable 191@dataclass 192class Header: 193 """ 194 Header portion of a block on the chain. 195 """ 196 197 parent_hash: Hash32 198 ommers_hash: Hash32 199 coinbase: Address 200 state_root: Root 201 transactions_root: Root 202 receipt_root: Root 203 bloom: Bloom 204 difficulty: Uint 205 number: Uint 206 gas_limit: Uint 207 gas_used: Uint 208 timestamp: U256 209 extra_data: Bytes 210 prev_randao: Bytes32 211 nonce: Bytes8 212 base_fee_per_gas: Uint 213 withdrawals_root: Root
Header portion of a block on the chain.
@slotted_freezable
@dataclass
class
Block:
216@slotted_freezable 217@dataclass 218class Block: 219 """ 220 A complete block. 221 """ 222 223 header: Header 224 transactions: Tuple[Union[Bytes, LegacyTransaction], ...] 225 ommers: Tuple[Header, ...] 226 withdrawals: Tuple[Withdrawal, ...]
A complete block.
@slotted_freezable
@dataclass
class
Log:
229@slotted_freezable 230@dataclass 231class Log: 232 """ 233 Data record produced during the execution of a transaction. 234 """ 235 236 address: Address 237 topics: Tuple[Hash32, ...] 238 data: bytes
Data record produced during the execution of a transaction.
@slotted_freezable
@dataclass
class
Receipt:
241@slotted_freezable 242@dataclass 243class Receipt: 244 """ 245 Result of a transaction. 246 """ 247 248 succeeded: bool 249 cumulative_gas_used: Uint 250 bloom: Bloom 251 logs: Tuple[Log, ...]
Result of a transaction.