Skip to content

Commit

Permalink
fix : Add .leaves() to MerkleTree
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Oct 28, 2024
1 parent 6a770eb commit 1a74ada
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crypto-primitives/src/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,12 @@ impl<P: Config> MerkleTree<P> {
}
Ok(true)
}

/// Returns a reference to the leaf nodes of the Merkle tree.
/// The leaves are returned in their original order from left to right.
pub fn leaves(&self) -> &[P::LeafDigest] {
&self.leaf_nodes
}
}

/// Returns the height of the tree, given the number of leaves.
Expand Down
19 changes: 19 additions & 0 deletions crypto-primitives/src/merkle_tree/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,23 @@ mod field_mt_tests {
],
)
}

#[test]
fn test_leaves_access() {


let tree = MerkleTree::blank().unwrap();

Check failure on line 315 in crypto-primitives/src/merkle_tree/tests/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

this function takes 3 arguments but 0 arguments were supplied

// Get leaf digests
let leaf_digests = tree.leaves();

// Verify the number of leaves matches
assert_eq!(leaf_digests.len(), 0);

// Verify each leaf digest matches what we expect
for (i, leaf) in leaves.iter().enumerate() {

Check failure on line 324 in crypto-primitives/src/merkle_tree/tests/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

cannot find value `leaves` in this scope
let expected_digest = P::LeafHash::evaluate(&leaf_hash_param, leaf).unwrap();

Check failure on line 325 in crypto-primitives/src/merkle_tree/tests/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

failed to resolve: use of undeclared type `P`

Check failure on line 325 in crypto-primitives/src/merkle_tree/tests/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

cannot find value `leaf_hash_param` in this scope
assert_eq!(leaf_digests[i], expected_digest);
}
}
}

0 comments on commit 1a74ada

Please sign in to comment.