Skip to content

Commit

Permalink
check that tree is non-empty for root generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanc414 committed Dec 27, 2023
1 parent 638f3fc commit eb8e9d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions merkly/mtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def concat_nodes(left: Node, right: Node) -> Node:
return reduce(concat_nodes, full_proof).data == self.root

def make_root(self, leaves: List[bytes]) -> bytes:
if len(leaves) == 0:
raise ValueError("Cannot get root of an empty tree")

while len(leaves) > 1:
next_level = []
for i in range(0, len(leaves) - 1, 2):
Expand Down
10 changes: 10 additions & 0 deletions test/errors/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ def invalid_hash_function(data):
["a", "b", "c", "d"],
invalid_hash_function,
)


def test_empty_tree_root():
mtree = MerkleTree(['a', 'b', 'c', 'd'])
mtree.leaves = []

with raises(ValueError) as error:
mtree.root

assert str(error.value) == 'Cannot get root of an empty tree'

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

0 comments on commit eb8e9d8

Please sign in to comment.