Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sorted pairs fix #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions evm_sc_utils/merkle_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def __check_input(leaves: List[HexBytes]):
@staticmethod
def __compute_tree(leaves: List[HexBytes]):
tree: Dict = {}
leaves.sort()
# sorted leaves
#leaves.sort()
tree[0] = leaves
node_level = 0
while len(tree[node_level]) != 1:
Expand All @@ -42,21 +43,24 @@ def __compute_tree(leaves: List[HexBytes]):
is_odd = num_of_nodes % 2 != 0
if is_odd:
for i in range(0, num_of_nodes - 1, 2):
pair = [nodes[i], nodes[i + 1]]
# sorted pairs
pair.sort()
next_nodes.append(
Web3.solidityKeccak(
["bytes32", "bytes32"], [nodes[i], nodes[i + 1]]
["bytes32", "bytes32"], [pair[0], pair[1]]
)
)
next_nodes.append(nodes[-1])
else:
for i in range(0, num_of_nodes, 2):
pair = [nodes[i], nodes[i + 1]]
pair.sort()
next_nodes.append(
Web3.solidityKeccak(
["bytes32", "bytes32"], [nodes[i], nodes[i + 1]]
["bytes32", "bytes32"], [pair[0], pair[1]]
)
)
# sort in place
next_nodes.sort()
tree[node_level + 1] = next_nodes
node_level = len(tree.keys()) - 1
return tree
Expand Down Expand Up @@ -88,16 +92,22 @@ def get_proof(self, leaf: HexBytes) -> List[HexStr]:
break
if loc % 2 == 0 and loc != len(self.tree[i]) - 1:
proof.append(self.tree[i][loc + 1].hex())
pair = [self.tree[i][loc], self.tree[i][loc + 1]]
# sorted pairs
pair.sort()
lookup_val = Web3.solidityKeccak(
["bytes32", "bytes32"],
[self.tree[i][loc], self.tree[i][loc + 1]],
[pair[0], pair[1]],
)
elif loc % 2 == 0 and loc == len(self.tree[i]) - 1:
pass
else:
proof.append(self.tree[i][loc - 1].hex())
pair = [self.tree[i][loc - 1], self.tree[i][loc]]
# sorted pairs
pair.sort()
lookup_val = Web3.solidityKeccak(
["bytes32", "bytes32"],
[self.tree[i][loc - 1], self.tree[i][loc]],
[pair[0], pair[1]],
)
return proof