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