Skip to content

Commit 3a8d38c

Browse files
committed
update: leafs to leaves
1 parent b618223 commit 3a8d38c

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

merkly/mtree.py

+33-35
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,44 @@
1515
validate_leafs,
1616
)
1717

18-
1918
class MerkleTree:
2019
"""
2120
# 🌳 Merkle Tree implementation
2221
2322
## Args:
24-
- leafs: List of raw data
25-
- hash_function (Callable[[str], str], optional): Function that hashes the data.
26-
* Defaults to `keccak` if not provided. It must have the signature (data: str) -> str.
27-
23+
- leaves: List of raw data
24+
- hash_function (Callable[[bytes, bytes], bytes], optional): Function that hashes the data.
25+
* Defaults to `keccak` if not provided
2826
"""
2927

3028
def __init__(
3129
self,
32-
leafs: List[str],
30+
leaves: List[str],
3331
hash_function: Callable[[bytes, bytes], bytes] = lambda x, y: keccak(x + y),
3432
) -> None:
35-
validate_leafs(leafs)
33+
validate_leafs(leaves)
3634
validate_hash_function(hash_function)
3735
self.hash_function: Callable[[bytes, bytes], bytes] = hash_function
38-
self.raw_leafs: List[str] = leafs
39-
self.leafs: List[str] = self.__hash_leafs(leafs)
40-
self.short_leafs: List[str] = self.short(self.leafs)
36+
self.raw_leaves: List[str] = leaves
37+
self.leaves: List[str] = self.__hash_leaves(leaves)
38+
self.short_leaves: List[str] = self.short(self.leaves)
4139

42-
def __hash_leafs(self, leafs: List[str]) -> List[str]:
43-
return list(map(lambda x: self.hash_function(x.encode(), bytes()), leafs))
40+
def __hash_leaves(self, leaves: List[str]) -> List[str]:
41+
return list(map(lambda x: self.hash_function(x.encode(), bytes()), leaves))
4442

4543
def __repr__(self) -> str:
46-
return f"""MerkleTree(\nraw_leafs: {self.raw_leafs}\nleafs: {self.leafs}\nshort_leafs: {self.short(self.leafs)})"""
44+
return f"""MerkleTree(\nraw_leaves: {self.raw_leaves}\nleaves: {self.leaves}\nshort_leaves: {self.short(self.leaves)})"""
4745

4846
def short(self, data: List[str]) -> List[str]:
4947
return [x[:2] for x in data]
5048

5149
@property
5250
def root(self) -> bytes:
53-
return self.make_root(self.leafs)
51+
return self.make_root(self.leaves)
5452

5553
def proof(self, raw_leaf: str) -> List[Node]:
5654
return self.make_proof(
57-
self.leafs, [], self.hash_function(raw_leaf.encode(), bytes())
55+
self.leaves, [], self.hash_function(raw_leaf.encode(), bytes())
5856
)
5957

6058
def verify(self, proof: List[bytes], raw_leaf: str) -> bool:
@@ -80,31 +78,31 @@ def concat_nodes(left: Node, right: Node) -> Node:
8078

8179
return reduce(concat_nodes, full_proof).data == self.root
8280

83-
def make_root(self, leafs: List[bytes]) -> List[str]:
84-
while len(leafs) > 1:
81+
def make_root(self, leaves: List[bytes]) -> bytes:
82+
while len(leaves) > 1:
8583
next_level = []
86-
for i in range(0, len(leafs) - 1, 2):
87-
next_level.append(self.hash_function(leafs[i], leafs[i + 1]))
84+
for i in range(0, len(leaves) - 1, 2):
85+
next_level.append(self.hash_function(leaves[i], leaves[i + 1]))
8886

89-
if len(leafs) % 2 == 1:
90-
next_level.append(leafs[-1])
87+
if len(leaves) % 2 == 1:
88+
next_level.append(leaves[-1])
9189

92-
leafs = next_level
90+
leaves = next_level
9391

94-
return leafs[0]
92+
return leaves[0]
9593

9694
def make_proof(
97-
self, leafs: List[bytes], proof: List[Node], leaf: bytes
95+
self, leaves: List[bytes], proof: List[Node], leaf: bytes
9896
) -> List[Node]:
9997
"""
10098
# Make a proof
10199
102100
## Dev:
103-
- if the `leaf` index is less than half the size of the `leafs`
101+
- if the `leaf` index is less than half the size of the `leaves`
104102
list then the right side must reach root and vice versa
105103
106104
## Args:
107-
- leafs: List of leafs
105+
- leaves: List of leaves
108106
- proof: Accumulated proof
109107
- leaf: Leaf for which to create the proof
110108
@@ -113,25 +111,25 @@ def make_proof(
113111
"""
114112

115113
try:
116-
index = leafs.index(leaf)
114+
index = leaves.index(leaf)
117115
except ValueError as err:
118-
msg = f"Leaf: {leaf} does not exist in the tree: {leafs}"
116+
msg = f"Leaf: {leaf} does not exist in the tree: {leaves}"
119117
raise ValueError(msg) from err
120118

121-
if is_power_2(len(leafs)) is False:
122-
return self.mix_tree(leafs, [], index)
119+
if is_power_2(len(leaves)) is False:
120+
return self.mix_tree(leaves, [], index)
123121

124-
if len(leafs) == 2:
122+
if len(leaves) == 2:
125123
if index == 1:
126-
proof.append(Node(data=leafs[0], side=Side.LEFT))
124+
proof.append(Node(data=leaves[0], side=Side.LEFT))
127125
else:
128-
proof.append(Node(data=leafs[1], side=Side.RIGHT))
126+
proof.append(Node(data=leaves[1], side=Side.RIGHT))
129127
proof.reverse()
130128
return proof
131129

132-
left, right = half(leafs)
130+
left, right = half(leaves)
133131

134-
if index < len(leafs) / 2:
132+
if index < len(leaves) / 2:
135133
proof.append(Node(data=self.make_root(right), side=Side.RIGHT))
136134
return self.make_proof(left, proof, leaf)
137135
else:

test/merkle_root/test_merkle_root.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66

77
def test_simple_merkle_tree_constructor():
8-
leafs = ["a", "b", "c", "d"]
9-
tree = MerkleTree(leafs)
8+
leaves = ["a", "b", "c", "d"]
9+
tree = MerkleTree(leaves)
1010

11-
assert tree.raw_leafs == leafs
11+
assert tree.raw_leaves == leaves
1212
for i, j in zip(
13-
tree.short_leafs,
13+
tree.short_leaves,
1414
[
1515
bytes.fromhex("3ac2"),
1616
bytes.fromhex("b555"),
@@ -19,7 +19,7 @@ def test_simple_merkle_tree_constructor():
1919
],
2020
):
2121
assert i == j
22-
assert tree.leafs == [
22+
assert tree.leaves == [
2323
bytes.fromhex(
2424
"3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1cb"
2525
),
@@ -41,7 +41,7 @@ def test_simple_merkle_tree_constructor():
4141

4242

4343
@mark.parametrize(
44-
"leafs, root",
44+
"leaves, root",
4545
[
4646
(
4747
["a", "b", "c", "d", "e", "f", "g", "h", "1"],
@@ -65,14 +65,14 @@ def test_simple_merkle_tree_constructor():
6565
),
6666
],
6767
)
68-
def test_simple_merkle_root_with_keccak256(leafs: List[str], root: str):
69-
tree = MerkleTree(leafs)
68+
def test_simple_merkle_root_with_keccak256(leaves: List[str], root: str):
69+
tree = MerkleTree(leaves)
7070
result = tree.root.hex()
7171
assert result == root
7272

7373

7474
@mark.parametrize(
75-
"leafs, root",
75+
"leaves, root",
7676
[
7777
(
7878
["a", "b", "c", "d", "e", "f", "g", "h", "1"],
@@ -96,20 +96,20 @@ def test_simple_merkle_root_with_keccak256(leafs: List[str], root: str):
9696
),
9797
],
9898
)
99-
def test_simple_merkle_root_with_sha_256(leafs: List[str], root: str):
99+
def test_simple_merkle_root_with_sha_256(leaves: List[str], root: str):
100100
def sha_256(x: bytes, y: bytes) -> bytes:
101101
data = x + y
102102
h = hashlib.sha256()
103103
h.update(data)
104104
return h.digest()
105105

106-
tree = MerkleTree(leafs, hash_function=sha_256)
106+
tree = MerkleTree(leaves, hash_function=sha_256)
107107
result = tree.root.hex()
108108
assert result == root
109109

110110

111111
@mark.parametrize(
112-
"leafs, root",
112+
"leaves, root",
113113
[
114114
(
115115
["a", "b", "c", "d", "e", "f", "g", "h", "1"],
@@ -133,20 +133,20 @@ def sha_256(x: bytes, y: bytes) -> bytes:
133133
),
134134
],
135135
)
136-
def test_simple_merkle_root_with_shake256(leafs: List[str], root: str):
136+
def test_simple_merkle_root_with_shake256(leaves: List[str], root: str):
137137
def shake_256(x: bytes, y: bytes) -> bytes:
138138
data = x + y
139139
h = hashlib.shake_256()
140140
h.update(data)
141141
return h.digest(32)
142142

143-
tree = MerkleTree(leafs, hash_function=shake_256)
143+
tree = MerkleTree(leaves, hash_function=shake_256)
144144
result = tree.root.hex()
145145
assert result == root
146146

147147

148148
@mark.parametrize(
149-
"leafs, root",
149+
"leaves, root",
150150
[
151151
(
152152
["a", "b", "c", "d", "e", "f", "g", "h", "1"],
@@ -170,13 +170,13 @@ def shake_256(x: bytes, y: bytes) -> bytes:
170170
),
171171
],
172172
)
173-
def test_simple_merkle_root_with_sha3_256(leafs: List[str], root: str):
173+
def test_simple_merkle_root_with_sha3_256(leaves: List[str], root: str):
174174
def sha3_256(x: bytes, y: bytes) -> bytes:
175175
data = x + y
176176
h = hashlib.sha3_256()
177177
h.update(data)
178178
return h.digest()
179179

180-
tree = MerkleTree(leafs, hash_function=sha3_256)
180+
tree = MerkleTree(leaves, hash_function=sha3_256)
181181
result = tree.root.hex()
182182
assert result == root

0 commit comments

Comments
 (0)