add get_value() to SparseMerkleTree trait

This commit is contained in:
Qyriad 2024-08-16 19:31:48 -06:00
parent 2fac7f11d9
commit 3d9c82bbe5
3 changed files with 19 additions and 0 deletions

View file

@ -249,6 +249,15 @@ impl SparseMerkleTree<SMT_DEPTH> for Smt {
}
}
fn get_value(&self, key: &RpoDigest) -> Word {
let leaf_pos = LeafIndex::<SMT_DEPTH>::from(*key).value();
self.leaves
.get(&leaf_pos)
.map(|leaf| leaf.get_value(key).unwrap_or_default())
.unwrap_or_default()
}
fn get_leaf(&self, key: &RpoDigest) -> Self::Leaf {
let leaf_pos = LeafIndex::<SMT_DEPTH>::from(*key).value();

View file

@ -157,6 +157,9 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
/// Inserts a leaf node, and returns the value at the key if already exists
fn insert_value(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value>;
/// Returns the value at the specified key, without modifying the tree.
fn get_value(&self, key: &Self::Key) -> Self::Value;
/// Returns the leaf at the specified index.
fn get_leaf(&self, key: &Self::Key) -> Self::Leaf;

View file

@ -288,6 +288,13 @@ impl<const DEPTH: u8> SparseMerkleTree<DEPTH> for SimpleSmt<DEPTH> {
}
}
fn get_value(&self, key: &LeafIndex<DEPTH>) -> Word {
let leaf_pos = key.value();
*self.leaves.get(&leaf_pos)
.unwrap_or(&Self::EMPTY_VALUE)
}
fn get_leaf(&self, key: &LeafIndex<DEPTH>) -> Word {
let leaf_pos = key.value();
match self.leaves.get(&leaf_pos) {