-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_types.py
154 lines (123 loc) · 2.49 KB
/
eth_types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Ethereum Types
^^^^^^^^^^^^^^
.. contents:: Table of Contents
:backlinks: none
:local:
Introduction
------------
Types re-used throughout the specification, which are specific to Ethereum.
"""
from dataclasses import dataclass
from typing import Tuple, Union
from .. import rlp
from ..base_types import (
U256,
Bytes,
Bytes0,
Bytes8,
Bytes20,
Bytes32,
Bytes256,
Uint,
slotted_freezable,
)
from ..crypto import Hash32, keccak256
Address = Bytes20
Root = Hash32
Bloom = Bytes256
TX_BASE_COST = 21000
TX_DATA_COST_PER_NON_ZERO = 68
TX_DATA_COST_PER_ZERO = 4
@slotted_freezable
@dataclass
class Transaction:
"""
Atomic operation performed on the block chain.
"""
nonce: U256
gas_price: U256
gas: U256
to: Union[Bytes0, Address]
value: U256
data: Bytes
v: U256
r: U256
s: U256
@slotted_freezable
@dataclass
class Account:
"""
State associated with an address.
"""
nonce: Uint
balance: U256
code: bytes
EMPTY_ACCOUNT = Account(
nonce=Uint(0),
balance=U256(0),
code=bytearray(),
)
def encode_account(raw_account_data: Account, storage_root: Bytes) -> Bytes:
"""
Encode `Account` dataclass.
Storage is not stored in the `Account` dataclass, so `Accounts` cannot be
encoded with providing a storage root.
"""
return rlp.encode(
(
raw_account_data.nonce,
raw_account_data.balance,
storage_root,
keccak256(raw_account_data.code),
)
)
@slotted_freezable
@dataclass
class Header:
"""
Header portion of a block on the chain.
"""
parent_hash: Hash32
ommers_hash: Hash32
coinbase: Address
state_root: Root
transactions_root: Root
receipt_root: Root
bloom: Bloom
difficulty: Uint
number: Uint
gas_limit: Uint
gas_used: Uint
timestamp: U256
extra_data: Bytes
mix_digest: Bytes32
nonce: Bytes8
@slotted_freezable
@dataclass
class Block:
"""
A complete block.
"""
header: Header
transactions: Tuple[Transaction, ...]
ommers: Tuple[Header, ...]
@slotted_freezable
@dataclass
class Log:
"""
Data record produced during the execution of a transaction.
"""
address: Address
topics: Tuple[Hash32, ...]
data: bytes
@slotted_freezable
@dataclass
class Receipt:
"""
Result of a transaction.
"""
post_state: Root
cumulative_gas_used: Uint
bloom: Bloom
logs: Tuple[Log, ...]