feat: added naive implementation of Smt::num_entries()

This commit is contained in:
Bobbin Threadbare 2025-03-15 17:41:08 -07:00
parent 660a667de1
commit 78e32a3824
No known key found for this signature in database
GPG key ID: 289C444AD87BC941
5 changed files with 46 additions and 34 deletions

View file

@ -497,14 +497,12 @@ fn test_bit_position_iterator() {
assert_eq!(TrueBitPositionIterator::new(3).collect::<Vec<u32>>(), vec![0, 1],);
assert_eq!(TrueBitPositionIterator::new(3).rev().collect::<Vec<u32>>(), vec![1, 0],);
assert_eq!(
TrueBitPositionIterator::new(0b11010101).collect::<Vec<u32>>(),
vec![0, 2, 4, 6, 7],
);
assert_eq!(
TrueBitPositionIterator::new(0b11010101).rev().collect::<Vec<u32>>(),
vec![7, 6, 4, 2, 0],
);
assert_eq!(TrueBitPositionIterator::new(0b11010101).collect::<Vec<u32>>(), vec![
0, 2, 4, 6, 7
],);
assert_eq!(TrueBitPositionIterator::new(0b11010101).rev().collect::<Vec<u32>>(), vec![
7, 6, 4, 2, 0
],);
}
#[test]

View file

@ -200,13 +200,10 @@ impl PartialMerkleTree {
pub fn to_paths(&self) -> Vec<(NodeIndex, ValuePath)> {
let mut paths = Vec::new();
self.leaves.iter().for_each(|&leaf| {
paths.push((
leaf,
ValuePath {
value: self.get_node(leaf).expect("Failed to get leaf node"),
path: self.get_path(leaf).expect("Failed to get path"),
},
));
paths.push((leaf, ValuePath {
value: self.get_node(leaf).expect("Failed to get leaf node"),
path: self.get_path(leaf).expect("Failed to get path"),
}));
});
paths
}

View file

@ -215,13 +215,10 @@ fn get_paths() {
let expected_paths: Vec<(NodeIndex, ValuePath)> = leaves
.iter()
.map(|&leaf| {
(
leaf,
ValuePath {
value: mt.get_node(leaf).unwrap(),
path: mt.get_path(leaf).unwrap(),
},
)
(leaf, ValuePath {
value: mt.get_node(leaf).unwrap(),
path: mt.get_path(leaf).unwrap(),
})
})
.collect();

View file

@ -159,10 +159,24 @@ impl Smt {
}
/// Returns the number of non-empty leaves in this tree.
///
/// Note that this may return a different value from [Self::num_entries()] as a single leaf may
/// contain more than one key-value pair.
pub fn num_leaves(&self) -> usize {
self.leaves.len()
}
/// Returns the number of key-value pairs with non-default values in this tree.
///
/// Note that this may return a different value from [Self::num_leaves()] as a single leaf may
/// contain more than one key-value pair.
///
/// Also note that this is currently an expensive operation is counting the number of entries
/// requires iterating over all leaves of the tree.
pub fn num_entries(&self) -> usize {
self.entries().count()
}
/// Returns the leaf to which `key` maps
pub fn get_leaf(&self, key: &RpoDigest) -> SmtLeaf {
<Self as SparseMerkleTree<SMT_DEPTH>>::get_leaf(self, key)

View file

@ -661,10 +661,12 @@ fn test_empty_smt_leaf_serialization() {
#[test]
fn test_single_smt_leaf_serialization() {
let single_leaf = SmtLeaf::new_single(
RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]),
[1_u32.into(), 2_u32.into(), 3_u32.into(), 4_u32.into()],
);
let single_leaf = SmtLeaf::new_single(RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]), [
1_u32.into(),
2_u32.into(),
3_u32.into(),
4_u32.into(),
]);
let mut serialized = single_leaf.to_bytes();
// extend buffer with random bytes
@ -677,14 +679,18 @@ fn test_single_smt_leaf_serialization() {
#[test]
fn test_multiple_smt_leaf_serialization_success() {
let multiple_leaf = SmtLeaf::new_multiple(vec![
(
RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]),
[1_u32.into(), 2_u32.into(), 3_u32.into(), 4_u32.into()],
),
(
RpoDigest::from([100_u32, 101_u32, 102_u32, 13_u32]),
[11_u32.into(), 12_u32.into(), 13_u32.into(), 14_u32.into()],
),
(RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]), [
1_u32.into(),
2_u32.into(),
3_u32.into(),
4_u32.into(),
]),
(RpoDigest::from([100_u32, 101_u32, 102_u32, 13_u32]), [
11_u32.into(),
12_u32.into(),
13_u32.into(),
14_u32.into(),
]),
])
.unwrap();