From 1a74adaab20d70be207ae2c1584cacc793590bbe Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 28 Oct 2024 13:16:31 +0100 Subject: [PATCH 1/2] fix : Add .leaves() to MerkleTree --- crypto-primitives/src/merkle_tree/mod.rs | 6 ++++++ .../src/merkle_tree/tests/mod.rs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/crypto-primitives/src/merkle_tree/mod.rs b/crypto-primitives/src/merkle_tree/mod.rs index 1a40045..d2f0ccd 100644 --- a/crypto-primitives/src/merkle_tree/mod.rs +++ b/crypto-primitives/src/merkle_tree/mod.rs @@ -723,6 +723,12 @@ impl MerkleTree

{ } 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. diff --git a/crypto-primitives/src/merkle_tree/tests/mod.rs b/crypto-primitives/src/merkle_tree/tests/mod.rs index 4f18d6e..6a24e14 100644 --- a/crypto-primitives/src/merkle_tree/tests/mod.rs +++ b/crypto-primitives/src/merkle_tree/tests/mod.rs @@ -307,4 +307,23 @@ mod field_mt_tests { ], ) } + + #[test] + fn test_leaves_access() { + + + let tree = MerkleTree::blank().unwrap(); + + // 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() { + let expected_digest = P::LeafHash::evaluate(&leaf_hash_param, leaf).unwrap(); + assert_eq!(leaf_digests[i], expected_digest); + } + } } From f23c810fca9df70296c5b1c1ceaaff0704ee95f7 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 28 Oct 2024 15:29:08 +0100 Subject: [PATCH 2/2] fix : failing ci --- crypto-primitives/src/merkle_tree/mod.rs | 2 +- crypto-primitives/src/merkle_tree/tests/mod.rs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/crypto-primitives/src/merkle_tree/mod.rs b/crypto-primitives/src/merkle_tree/mod.rs index d2f0ccd..bc6abaf 100644 --- a/crypto-primitives/src/merkle_tree/mod.rs +++ b/crypto-primitives/src/merkle_tree/mod.rs @@ -724,7 +724,7 @@ impl MerkleTree

{ Ok(true) } - /// Returns a reference to the leaf nodes of the Merkle tree. + /// 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 diff --git a/crypto-primitives/src/merkle_tree/tests/mod.rs b/crypto-primitives/src/merkle_tree/tests/mod.rs index 6a24e14..2a27353 100644 --- a/crypto-primitives/src/merkle_tree/tests/mod.rs +++ b/crypto-primitives/src/merkle_tree/tests/mod.rs @@ -310,16 +310,14 @@ mod field_mt_tests { #[test] fn test_leaves_access() { - - let tree = MerkleTree::blank().unwrap(); // 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() { let expected_digest = P::LeafHash::evaluate(&leaf_hash_param, leaf).unwrap();