ethereum.frontier.trie
State Trie ^^^^^^^^^^
.. contents:: Table of Contents :backlinks: none :local:
Introduction
The state trie is the structure responsible for storing
.fork_types.Account
objects.
EMPTY_TRIE_ROOT
59 | EMPTY_TRIE_ROOT = Root( |
---|---|
60 | hex_to_bytes( |
61 | "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" |
62 | ) |
63 | ) |
Node
65 | Node = Union[Account, Bytes, Transaction, Receipt, Uint, U256, None] |
---|
K
66 | K = TypeVar("K", bound=Bytes) |
---|
V
67 | V = TypeVar( |
---|---|
68 | "V", |
69 | Optional[Account], |
70 | Optional[Bytes], |
71 | Bytes, |
72 | Optional[Transaction], |
73 | Optional[Receipt], |
74 | Uint, |
75 | U256, |
76 | ) |
LeafNode
Leaf node in the Merkle Trie
79 | @slotted_freezable |
---|
80 | @dataclass |
---|
class LeafNode:
rest_of_key
84 | rest_of_key: Bytes |
---|
value
85 | value: rlp.Extended |
---|
ExtensionNode
Extension node in the Merkle Trie
88 | @slotted_freezable |
---|
89 | @dataclass |
---|
class ExtensionNode:
key_segment
93 | key_segment: Bytes |
---|
subnode
94 | subnode: rlp.Extended |
---|
BranchSubnodes
97 | BranchSubnodes = Tuple[ |
---|---|
98 | rlp.Extended, |
99 | rlp.Extended, |
100 | rlp.Extended, |
101 | rlp.Extended, |
102 | rlp.Extended, |
103 | rlp.Extended, |
104 | rlp.Extended, |
105 | rlp.Extended, |
106 | rlp.Extended, |
107 | rlp.Extended, |
108 | rlp.Extended, |
109 | rlp.Extended, |
110 | rlp.Extended, |
111 | rlp.Extended, |
112 | rlp.Extended, |
113 | rlp.Extended, |
114 | ] |
BranchNode
Branch node in the Merkle Trie
117 | @slotted_freezable |
---|
118 | @dataclass |
---|
class BranchNode:
subnodes
122 | subnodes: BranchSubnodes |
---|
value
123 | value: rlp.Extended |
---|
InternalNode
126 | InternalNode = Union[LeafNode, ExtensionNode, BranchNode] |
---|
encode_internal_node
Encodes a Merkle Trie node into its RLP form. The RLP will then be
serialized into a Bytes
and hashed unless it is less that 32 bytes
when serialized.
This function also accepts None
, representing the absence of a node,
which is encoded to b""
.
Parameters
node : Optional[InternalNode] The node to encode.
Returns
encoded : rlp.RLP
The node encoded as RLP.
def encode_internal_node(node: Optional[InternalNode]) -> ethereum_rlp.rlp.Extended:
130 | """ |
---|---|
131 | Encodes a Merkle Trie node into its RLP form. The RLP will then be |
132 | serialized into a `Bytes` and hashed unless it is less that 32 bytes |
133 | when serialized. |
134 |
|
135 | This function also accepts `None`, representing the absence of a node, |
136 | which is encoded to `b""`. |
137 |
|
138 | Parameters |
139 | ---------- |
140 | node : Optional[InternalNode] |
141 | The node to encode. |
142 |
|
143 | Returns |
144 | ------- |
145 | encoded : `rlp.RLP` |
146 | The node encoded as RLP. |
147 | """ |
148 | unencoded: rlp.Extended |
149 | if node is None: |
150 | unencoded = b"" |
151 | elif isinstance(node, LeafNode): |
152 | unencoded = ( |
153 | nibble_list_to_compact(node.rest_of_key, True), |
154 | node.value, |
155 | ) |
156 | elif isinstance(node, ExtensionNode): |
157 | unencoded = ( |
158 | nibble_list_to_compact(node.key_segment, False), |
159 | node.subnode, |
160 | ) |
161 | elif isinstance(node, BranchNode): |
162 | unencoded = list(node.subnodes) + [node.value] |
163 | else: |
164 | raise AssertionError(f"Invalid internal node type {type(node)}!") |
165 | |
166 | encoded = rlp.encode(unencoded) |
167 | if len(encoded) < 32: |
168 | return unencoded |
169 | else: |
170 | return keccak256(encoded) |
encode_node
Encode a Node for storage in the Merkle Trie.
Currently mostly an unimplemented stub.
def encode_node(node: Node, storage_root: Optional[Bytes]) -> Bytes:
174 | """ |
---|---|
175 | Encode a Node for storage in the Merkle Trie. |
176 |
|
177 | Currently mostly an unimplemented stub. |
178 | """ |
179 | if isinstance(node, Account): |
180 | assert storage_root is not None |
181 | return encode_account(node, storage_root) |
182 | elif isinstance(node, (Transaction, Receipt, U256)): |
183 | return rlp.encode(node) |
184 | elif isinstance(node, Bytes): |
185 | return node |
186 | else: |
187 | raise AssertionError( |
188 | f"encoding for {type(node)} is not currently implemented" |
189 | ) |
Trie
The Merkle Trie.
192 | @dataclass |
---|
class Trie:
secured
198 | secured: bool |
---|
default
199 | default: V |
---|
_data
200 | _data: Dict[K, V] = field(default_factory=dict) |
---|
copy_trie
Create a copy of trie
. Since only frozen objects may be stored in tries,
the contents are reused.
Parameters
trie: Trie
Trie to copy.
Returns
new_trie : Trie[K, V]
A copy of the trie.
def copy_trie(trie: Trie[K, V]) -> Trie[K, V]:
204 | """ |
---|---|
205 | Create a copy of `trie`. Since only frozen objects may be stored in tries, |
206 | the contents are reused. |
207 |
|
208 | Parameters |
209 | ---------- |
210 | trie: `Trie` |
211 | Trie to copy. |
212 |
|
213 | Returns |
214 | ------- |
215 | new_trie : `Trie[K, V]` |
216 | A copy of the trie. |
217 | """ |
218 | return Trie(trie.secured, trie.default, copy.copy(trie._data)) |
trie_set
Stores an item in a Merkle Trie.
This method deletes the key if value == trie.default
, because the Merkle
Trie represents the default value by omitting it from the trie.
Parameters
trie: Trie
Trie to store in.
key : Bytes
Key to lookup.
value : V
Node to insert at key
.
def trie_set(trie: Trie[K, V], key: K, value: V) -> None:
222 | """ |
---|---|
223 | Stores an item in a Merkle Trie. |
224 |
|
225 | This method deletes the key if `value == trie.default`, because the Merkle |
226 | Trie represents the default value by omitting it from the trie. |
227 |
|
228 | Parameters |
229 | ---------- |
230 | trie: `Trie` |
231 | Trie to store in. |
232 | key : `Bytes` |
233 | Key to lookup. |
234 | value : `V` |
235 | Node to insert at `key`. |
236 | """ |
237 | if value == trie.default: |
238 | if key in trie._data: |
239 | del trie._data[key] |
240 | else: |
241 | trie._data[key] = value |
trie_get
Gets an item from the Merkle Trie.
This method returns trie.default
if the key is missing.
Parameters
trie: Trie to lookup in. key : Key to lookup.
Returns
node : V
Node at key
in the trie.
def trie_get(trie: Trie[K, V], key: K) -> V:
245 | """ |
---|---|
246 | Gets an item from the Merkle Trie. |
247 |
|
248 | This method returns `trie.default` if the key is missing. |
249 |
|
250 | Parameters |
251 | ---------- |
252 | trie: |
253 | Trie to lookup in. |
254 | key : |
255 | Key to lookup. |
256 |
|
257 | Returns |
258 | ------- |
259 | node : `V` |
260 | Node at `key` in the trie. |
261 | """ |
262 | return trie._data.get(key, trie.default) |
common_prefix_length
Find the longest common prefix of two sequences.
def common_prefix_length(a: Sequence, b: Sequence) -> int:
266 | """ |
---|---|
267 | Find the longest common prefix of two sequences. |
268 | """ |
269 | for i in range(len(a)): |
270 | if i >= len(b) or a[i] != b[i]: |
271 | return i |
272 | return len(a) |
nibble_list_to_compact
Compresses nibble-list into a standard byte array with a flag.
A nibble-list is a list of byte values no greater than 15
. The flag is
encoded in high nibble of the highest byte. The flag nibble can be broken
down into two two-bit flags.
Highest nibble::
+---+---+----------+--------+
| _ | _ | is_leaf | parity |
+---+---+----------+--------+
3 2 1 0
The lowest bit of the nibble encodes the parity of the length of the
remaining nibbles -- 0
when even and 1
when odd. The second lowest bit
is used to distinguish leaf and extension nodes. The other two bits are not
used.
Parameters
x : Array of nibbles. is_leaf : True if this is part of a leaf node, or false if it is an extension node.
Returns
compressed : bytearray
Compact byte array.
def nibble_list_to_compact(x: Bytes, is_leaf: bool) -> Bytes:
276 | """ |
---|---|
277 | Compresses nibble-list into a standard byte array with a flag. |
278 |
|
279 | A nibble-list is a list of byte values no greater than `15`. The flag is |
280 | encoded in high nibble of the highest byte. The flag nibble can be broken |
281 | down into two two-bit flags. |
282 |
|
283 | Highest nibble:: |
284 |
|
285 | +---+---+----------+--------+ |
286 | | _ | _ | is_leaf | parity | |
287 | +---+---+----------+--------+ |
288 | 3 2 1 0 |
289 |
|
290 |
|
291 | The lowest bit of the nibble encodes the parity of the length of the |
292 | remaining nibbles -- `0` when even and `1` when odd. The second lowest bit |
293 | is used to distinguish leaf and extension nodes. The other two bits are not |
294 | used. |
295 |
|
296 | Parameters |
297 | ---------- |
298 | x : |
299 | Array of nibbles. |
300 | is_leaf : |
301 | True if this is part of a leaf node, or false if it is an extension |
302 | node. |
303 |
|
304 | Returns |
305 | ------- |
306 | compressed : `bytearray` |
307 | Compact byte array. |
308 | """ |
309 | compact = bytearray() |
310 | |
311 | if len(x) % 2 == 0: # ie even length |
312 | compact.append(16 * (2 * is_leaf)) |
313 | for i in range(0, len(x), 2): |
314 | compact.append(16 * x[i] + x[i + 1]) |
315 | else: |
316 | compact.append(16 * ((2 * is_leaf) + 1) + x[0]) |
317 | for i in range(1, len(x), 2): |
318 | compact.append(16 * x[i] + x[i + 1]) |
319 | |
320 | return Bytes(compact) |
bytes_to_nibble_list
Converts a Bytes
into to a sequence of nibbles (bytes with value < 16).
Parameters
bytes_:
The Bytes
to convert.
Returns
nibble_list : Bytes
The Bytes
in nibble-list format.
def bytes_to_nibble_list(bytes_: Bytes) -> Bytes:
324 | """ |
---|---|
325 | Converts a `Bytes` into to a sequence of nibbles (bytes with value < 16). |
326 |
|
327 | Parameters |
328 | ---------- |
329 | bytes_: |
330 | The `Bytes` to convert. |
331 |
|
332 | Returns |
333 | ------- |
334 | nibble_list : `Bytes` |
335 | The `Bytes` in nibble-list format. |
336 | """ |
337 | nibble_list = bytearray(2 * len(bytes_)) |
338 | for byte_index, byte in enumerate(bytes_): |
339 | nibble_list[byte_index * 2] = (byte & 0xF0) >> 4 |
340 | nibble_list[byte_index * 2 + 1] = byte & 0x0F |
341 | return Bytes(nibble_list) |
_prepare_trie
Prepares the trie for root calculation. Removes values that are empty,
hashes the keys (if secured == True
) and encodes all the nodes.
Parameters
trie :
The Trie
to prepare.
get_storage_root :
Function to get the storage root of an account. Needed to encode
Account
objects.
Returns
out : Mapping[ethereum.base_types.Bytes, Node]
Object with keys mapped to nibble-byte form.
def _prepare_trie(trie: Trie[K, V], get_storage_root: Optional[Callable[[Address], Root]]) -> Mapping[Bytes, Bytes]:
348 | """ |
---|---|
349 | Prepares the trie for root calculation. Removes values that are empty, |
350 | hashes the keys (if `secured == True`) and encodes all the nodes. |
351 |
|
352 | Parameters |
353 | ---------- |
354 | trie : |
355 | The `Trie` to prepare. |
356 | get_storage_root : |
357 | Function to get the storage root of an account. Needed to encode |
358 | `Account` objects. |
359 |
|
360 | Returns |
361 | ------- |
362 | out : `Mapping[ethereum.base_types.Bytes, Node]` |
363 | Object with keys mapped to nibble-byte form. |
364 | """ |
365 | mapped: MutableMapping[Bytes, Bytes] = {} |
366 | |
367 | for preimage, value in trie._data.items(): |
368 | if isinstance(value, Account): |
369 | assert get_storage_root is not None |
370 | address = Address(preimage) |
371 | encoded_value = encode_node(value, get_storage_root(address)) |
372 | else: |
373 | encoded_value = encode_node(value) |
374 | if encoded_value == b"": |
375 | raise AssertionError |
376 | key: Bytes |
377 | if trie.secured: |
378 | # "secure" tries hash keys once before construction |
379 | key = keccak256(preimage) |
380 | else: |
381 | key = preimage |
382 | mapped[bytes_to_nibble_list(key)] = encoded_value |
383 | |
384 | return mapped |
root
Computes the root of a modified merkle patricia trie (MPT).
Parameters
trie :
Trie
to get the root of.
get_storage_root :
Function to get the storage root of an account. Needed to encode
Account
objects.
Returns
root : .fork_types.Root
MPT root of the underlying key-value pairs.
def root(trie: Trie[K, V], get_storage_root: Optional[Callable[[Address], Root]]) -> Root:
391 | """ |
---|---|
392 | Computes the root of a modified merkle patricia trie (MPT). |
393 |
|
394 | Parameters |
395 | ---------- |
396 | trie : |
397 | `Trie` to get the root of. |
398 | get_storage_root : |
399 | Function to get the storage root of an account. Needed to encode |
400 | `Account` objects. |
401 |
|
402 |
|
403 | Returns |
404 | ------- |
405 | root : `.fork_types.Root` |
406 | MPT root of the underlying key-value pairs. |
407 | """ |
408 | obj = _prepare_trie(trie, get_storage_root) |
409 | |
410 | root_node = encode_internal_node(patricialize(obj, Uint(0))) |
411 | if len(rlp.encode(root_node)) < 32: |
412 | return keccak256(rlp.encode(root_node)) |
413 | else: |
414 | assert isinstance(root_node, Bytes) |
415 | return Root(root_node) |
patricialize
Structural composition function.
Used to recursively patricialize and merkleize a dictionary. Includes memoization of the tree structure and hashes.
Parameters
obj : Underlying trie key-value pairs, with keys in nibble-list format. level : Current trie level.
Returns
node : ethereum.base_types.Bytes
Root node of obj
.
def patricialize(obj: Mapping[Bytes, Bytes], level: Uint) -> Optional[InternalNode]:
421 | """ |
---|---|
422 | Structural composition function. |
423 |
|
424 | Used to recursively patricialize and merkleize a dictionary. Includes |
425 | memoization of the tree structure and hashes. |
426 |
|
427 | Parameters |
428 | ---------- |
429 | obj : |
430 | Underlying trie key-value pairs, with keys in nibble-list format. |
431 | level : |
432 | Current trie level. |
433 |
|
434 | Returns |
435 | ------- |
436 | node : `ethereum.base_types.Bytes` |
437 | Root node of `obj`. |
438 | """ |
439 | if len(obj) == 0: |
440 | return None |
441 | |
442 | arbitrary_key = next(iter(obj)) |
443 | |
444 | # if leaf node |
445 | if len(obj) == 1: |
446 | leaf = LeafNode(arbitrary_key[level:], obj[arbitrary_key]) |
447 | return leaf |
448 | |
449 | # prepare for extension node check by finding max j such that all keys in |
450 | # obj have the same key[i:j] |
451 | substring = arbitrary_key[level:] |
452 | prefix_length = len(substring) |
453 | for key in obj: |
454 | prefix_length = min( |
455 | prefix_length, common_prefix_length(substring, key[level:]) |
456 | ) |
457 |
|
458 | # finished searching, found another key at the current level |
459 | if prefix_length == 0: |
460 | break |
461 | |
462 | # if extension node |
463 | if prefix_length > 0: |
464 | prefix = arbitrary_key[int(level) : int(level) + prefix_length] |
465 | return ExtensionNode( |
466 | prefix, |
467 | encode_internal_node( |
468 | patricialize(obj, level + Uint(prefix_length)) |
469 | ), |
470 | ) |
471 | |
472 | branches: List[MutableMapping[Bytes, Bytes]] = [] |
473 | for _ in range(16): |
474 | branches.append({}) |
475 | value = b"" |
476 | for key in obj: |
477 | if len(key) == level: |
478 | # shouldn't ever have an account or receipt in an internal node |
479 | if isinstance(obj[key], (Account, Receipt, Uint)): |
480 | raise AssertionError |
481 | value = obj[key] |
482 | else: |
483 | branches[key[level]][key] = obj[key] |
484 | |
485 | subnodes = tuple( |
486 | encode_internal_node(patricialize(branches[k], level + Uint(1))) |
487 | for k in range(16) |
488 | ) |
489 | return BranchNode( |
490 | cast(BranchSubnodes, assert_type(subnodes, Tuple[rlp.Extended, ...])), |
491 | value, |
492 | ) |