Skip to content

Commit 57f43d2

Browse files
Update twenty-first/src/util_types/merkle_tree.rs
Co-authored-by: Jan Ferdinand Sauer <ferdinand@neptune.cash>
1 parent 1fb1ea1 commit 57f43d2

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

twenty-first/src/util_types/merkle_tree.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,14 @@ impl MerkleTree {
316316

317317
/// All leafs of the Merkle tree.
318318
pub fn leafs(&self) -> impl Iterator<Item = &Digest> {
319-
self.nodes.iter().skip(
320-
(self.num_nodes() / 2)
321-
.try_into()
322-
.expect("MerkleTreeNodeIndex to usize conversion error"),
319+
// This conversion can only fail if the number of leafs is larger than
320+
// usize::MAX. This implies that the number of nodes is larger than
321+
// usize::MAX. Since the nodes are stored in a Vec, the number of nodes
322+
// can never exceed usize::MAX.
323+
// This proof by contradiction shows that unwrapping is fine.
324+
let num_leafs = usize::try_from(self.num_leafs()).unwrap();
325+
326+
self.nodes.iter().skip(num_leafs)
323327
)
324328
}
325329

0 commit comments

Comments
 (0)