ethereum.forks.homestead.trieethereum.forks.dao_fork.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

58
EMPTY_TRIE_ROOT = Root(
59
    hex_to_bytes(
60
        "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
61
    )
62
)

Node

64
Node = Account | Bytes | Transaction | Receipt | Uint | U256

K

65
K = TypeVar("K", bound=Bytes)

V

66
V = TypeVar(
67
    "V",
68
    Optional[Account],
69
    Optional[Bytes],
70
    Bytes,
71
    Optional[Transaction],
72
    Optional[Receipt],
73
    Uint,
74
    U256,
75
)

LeafNode

Leaf node in the Merkle Trie.

78
@slotted_freezable
79
@dataclass
class LeafNode:

rest_of_key

83
    rest_of_key: Bytes

value

84
    value: Extended

ExtensionNode

Extension node in the Merkle Trie.

87
@slotted_freezable
88
@dataclass
class ExtensionNode:

key_segment

92
    key_segment: Bytes

subnode

93
    subnode: Extended

BranchSubnodes

96
BranchSubnodes = Tuple[
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
    Extended,
113
]

BranchNode

Branch node in the Merkle Trie.

116
@slotted_freezable
117
@dataclass
class BranchNode:

subnodes

121
    subnodes: BranchSubnodes

value

122
    value: Extended

InternalNode

125
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 than 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:
129
    """
130
    Encodes a Merkle Trie node into its RLP form. The RLP will then be
131
    serialized into a `Bytes` and hashed unless it is less than 32 bytes
132
    when serialized.
133
134
    This function also accepts `None`, representing the absence of a node,
135
    which is encoded to `b""`.
136
137
    Parameters
138
    ----------
139
    node : Optional[InternalNode]
140
        The node to encode.
141
142
    Returns
143
    -------
144
    encoded : `rlp.RLP`
145
        The node encoded as RLP.
146
147
    """
148
    unencoded: 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.

def encode_node(node: Node, ​​storage_root: Optional[Bytes]) -> Bytes:
174
    """
175
    Encode a Node for storage in the Merkle Trie.
176
    """
177
    if isinstance(node, Account):
178
        assert storage_root is not None
179
        return encode_account(node, storage_root)
180
    elif isinstance(node, (Transaction, Receipt, U256, Uint)):
181
        return rlp.encode(node)
182
    elif isinstance(node, Bytes):
183
        return node
184
    else:
185
        return previous_trie.encode_node(node, storage_root)
185
        return previous_trie.encode_node(node, storage_root)

Trie

The Merkle Trie.

188
@dataclass
class Trie:

secured

194
    secured: bool

default

195
    default: V

_data

196
    _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]:
200
    """
201
    Create a copy of `trie`. Since only frozen objects may be stored in tries,
202
    the contents are reused.
203
204
    Parameters
205
    ----------
206
    trie: `Trie`
207
        Trie to copy.
208
209
    Returns
210
    -------
211
    new_trie : `Trie[K, V]`
212
        A copy of the trie.
213
214
    """
215
    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:
219
    """
220
    Stores an item in a Merkle Trie.
221
222
    This method deletes the key if `value == trie.default`, because the Merkle
223
    Trie represents the default value by omitting it from the trie.
224
225
    Parameters
226
    ----------
227
    trie: `Trie`
228
        Trie to store in.
229
    key : `Bytes`
230
        Key to lookup.
231
    value : `V`
232
        Node to insert at `key`.
233
234
    """
235
    if value == trie.default:
236
        if key in trie._data:
237
            del trie._data[key]
238
    else:
239
        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:
243
    """
244
    Gets an item from the Merkle Trie.
245
246
    This method returns `trie.default` if the key is missing.
247
248
    Parameters
249
    ----------
250
    trie:
251
        Trie to lookup in.
252
    key :
253
        Key to lookup.
254
255
    Returns
256
    -------
257
    node : `V`
258
        Node at `key` in the trie.
259
260
    """
261
    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:
265
    """
266
    Find the longest common prefix of two sequences.
267
    """
268
    for i in range(len(a)):
269
        if i >= len(b) or a[i] != b[i]:
270
            return i
271
    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:
275
    """
276
    Compresses nibble-list into a standard byte array with a flag.
277
278
    A nibble-list is a list of byte values no greater than `15`. The flag is
279
    encoded in high nibble of the highest byte. The flag nibble can be broken
280
    down into two two-bit flags.
281
282
    Highest nibble::
283
284
        +---+---+----------+--------+
285
        | _ | _ | is_leaf | parity |
286
        +---+---+----------+--------+
287
          3   2      1         0
288
289
290
    The lowest bit of the nibble encodes the parity of the length of the
291
    remaining nibbles -- `0` when even and `1` when odd. The second lowest bit
292
    is used to distinguish leaf and extension nodes. The other two bits are not
293
    used.
294
295
    Parameters
296
    ----------
297
    x :
298
        Array of nibbles.
299
    is_leaf :
300
        True if this is part of a leaf node, or false if it is an extension
301
        node.
302
303
    Returns
304
    -------
305
    compressed : `bytearray`
306
        Compact byte array.
307
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
    """
338
    nibble_list = bytearray(2 * len(bytes_))
339
    for byte_index, byte in enumerate(bytes_):
340
        nibble_list[byte_index * 2] = (byte & 0xF0) >> 4
341
        nibble_list[byte_index * 2 + 1] = byte & 0x0F
342
    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]:
349
    """
350
    Prepares the trie for root calculation. Removes values that are empty,
351
    hashes the keys (if `secured == True`) and encodes all the nodes.
352
353
    Parameters
354
    ----------
355
    trie :
356
        The `Trie` to prepare.
357
    get_storage_root :
358
        Function to get the storage root of an account. Needed to encode
359
        `Account` objects.
360
361
    Returns
362
    -------
363
    out : `Mapping[ethereum.base_types.Bytes, Node]`
364
        Object with keys mapped to nibble-byte form.
365
366
    """
367
    mapped: MutableMapping[Bytes, Bytes] = {}
368
369
    for preimage, value in trie._data.items():
370
        if isinstance(value, Account):
371
            assert get_storage_root is not None
372
            address = Address(preimage)
373
            encoded_value = encode_node(value, get_storage_root(address))
374
        elif value is None:
375
            raise AssertionError("cannot encode `None`")
376
        else:
377
            encoded_value = encode_node(value)
378
        if encoded_value == b"":
379
            raise AssertionError
380
        key: Bytes
381
        if trie.secured:
382
            # "secure" tries hash keys once before construction
383
            key = keccak256(preimage)
384
        else:
385
            key = preimage
386
        mapped[bytes_to_nibble_list(key)] = encoded_value
387
388
    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:
395
    """
396
    Computes the root of a modified merkle patricia trie (MPT).
397
398
    Parameters
399
    ----------
400
    trie :
401
        `Trie` to get the root of.
402
    get_storage_root :
403
        Function to get the storage root of an account. Needed to encode
404
        `Account` objects.
405
406
407
    Returns
408
    -------
409
    root : `.fork_types.Root`
410
        MPT root of the underlying key-value pairs.
411
412
    """
413
    obj = _prepare_trie(trie, get_storage_root)
414
415
    root_node = encode_internal_node(patricialize(obj, Uint(0)))
416
    if len(rlp.encode(root_node)) < 32:
417
        return keccak256(rlp.encode(root_node))
418
    else:
419
        assert isinstance(root_node, Bytes)
420
        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]:
426
    """
427
    Structural composition function.
428
429
    Used to recursively patricialize and merkleize a dictionary. Includes
430
    memoization of the tree structure and hashes.
431
432
    Parameters
433
    ----------
434
    obj :
435
        Underlying trie key-value pairs, with keys in nibble-list format.
436
    level :
437
        Current trie level.
438
439
    Returns
440
    -------
441
    node : `ethereum.base_types.Bytes`
442
        Root node of `obj`.
443
444
    """
445
    if len(obj) == 0:
446
        return None
447
448
    arbitrary_key = next(iter(obj))
449
450
    # if leaf node
451
    if len(obj) == 1:
452
        leaf = LeafNode(arbitrary_key[level:], obj[arbitrary_key])
453
        return leaf
454
455
    # prepare for extension node check by finding max j such that all keys in
456
    # obj have the same key[i:j]
457
    substring = arbitrary_key[level:]
458
    prefix_length = len(substring)
459
    for key in obj:
460
        prefix_length = min(
461
            prefix_length, common_prefix_length(substring, key[level:])
462
        )
463
464
        # finished searching, found another key at the current level
465
        if prefix_length == 0:
466
            break
467
468
    # if extension node
469
    if prefix_length > 0:
470
        prefix = arbitrary_key[int(level) : int(level) + prefix_length]
471
        return ExtensionNode(
472
            prefix,
473
            encode_internal_node(
474
                patricialize(obj, level + Uint(prefix_length))
475
            ),
476
        )
477
478
    branches: List[MutableMapping[Bytes, Bytes]] = []
479
    for _ in range(16):
480
        branches.append({})
481
    value = b""
482
    for key in obj:
483
        if len(key) == level:
484
            # shouldn't ever have an account or receipt in an internal node
485
            if isinstance(obj[key], (Account, Receipt, Uint)):
486
                raise AssertionError
487
            value = obj[key]
488
        else:
489
            branches[key[level]][key] = obj[key]
490
491
    subnodes = tuple(
492
        encode_internal_node(patricialize(branches[k], level + Uint(1)))
493
        for k in range(16)
494
    )
495
    return BranchNode(
496
        cast(BranchSubnodes, assert_type(subnodes, Tuple[Extended, ...])),
497
        value,
498
    )