Skip to content

Commit b151dfe

Browse files
authored
change type of branch node subnodes (#1066) (#1095)
1 parent 0c56b70 commit b151dfe

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ python_requires = >=3.10
119119
install_requires =
120120
pycryptodome>=3,<4
121121
coincurve>=20,<21
122-
typing_extensions>=4
122+
typing_extensions>=4.2
123123
py_ecc @ git+https://github.com/petertdavies/py_ecc.git@127184f4c57b1812da959586d0fe8f43bb1a2389
124124
ethereum-types>=0.2.1,<0.3
125125
ethereum-rlp>=0.1.1,<0.2

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)