Skip to content

Commit aa7d914

Browse files
committed
change type of branch node subnodes (#1066) (#1095)
1 parent e2136e9 commit aa7d914

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/ethereum/prague/trie.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
MutableMapping,
2525
Optional,
2626
Sequence,
27+
Tuple,
2728
TypeVar,
2829
Union,
30+
cast,
2931
)
3032

3133
from ethereum_rlp import rlp
3234
from ethereum_types.bytes import Bytes
3335
from ethereum_types.frozen import slotted_freezable
3436
from ethereum_types.numeric import U256, Uint
37+
from typing_extensions import assert_type
3538

3639
from ethereum.cancun import trie as previous_trie
3740
from ethereum.crypto.hash import keccak256
@@ -95,12 +98,32 @@ class ExtensionNode:
9598
subnode: rlp.Extended
9699

97100

101+
BranchSubnodes = Tuple[
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+
rlp.Extended,
115+
rlp.Extended,
116+
rlp.Extended,
117+
rlp.Extended,
118+
]
119+
120+
98121
@slotted_freezable
99122
@dataclass
100123
class BranchNode:
101124
"""Branch node in the Merkle Trie"""
102125

103-
subnodes: List[rlp.Extended]
126+
subnodes: BranchSubnodes
104127
value: rlp.Extended
105128

106129

@@ -140,7 +163,7 @@ def encode_internal_node(node: Optional[InternalNode]) -> rlp.Extended:
140163
node.subnode,
141164
)
142165
elif isinstance(node, BranchNode):
143-
unencoded = node.subnodes + [node.value]
166+
unencoded = list(node.subnodes) + [node.value]
144167
else:
145168
raise AssertionError(f"Invalid internal node type {type(node)}!")
146169

@@ -461,10 +484,11 @@ def patricialize(
461484
else:
462485
branches[key[level]][key] = obj[key]
463486

487+
subnodes = tuple(
488+
encode_internal_node(patricialize(branches[k], level + Uint(1)))
489+
for k in range(16)
490+
)
464491
return BranchNode(
465-
[
466-
encode_internal_node(patricialize(branches[k], level + Uint(1)))
467-
for k in range(16)
468-
],
492+
cast(BranchSubnodes, assert_type(subnodes, Tuple[rlp.Extended, ...])),
469493
value,
470494
)

0 commit comments

Comments
 (0)