Struct nmt_rs::simple_merkle::tree::MerkleTree
source · pub struct MerkleTree<Db, M>where
M: MerkleHash,{ /* private fields */ }Expand description
Implements an RFC 6962 compatible merkle tree over an in-memory data store which maps preimages to hashes.
Implementations§
source§impl<Db, M> MerkleTree<Db, M>
impl<Db, M> MerkleTree<Db, M>
source§impl<Db, M> MerkleTree<Db, M>
impl<Db, M> MerkleTree<Db, M>
sourcepub fn with_hasher(hasher: M) -> Self
pub fn with_hasher(hasher: M) -> Self
Constructs an empty merkle tree with the given hasher
sourcepub fn push_raw_leaf(&mut self, raw_leaf: &[u8])
pub fn push_raw_leaf(&mut self, raw_leaf: &[u8])
Appends the given leaf to the tree
sourcepub fn push_leaf_with_hash(&mut self, leaf_with_hash: LeafWithHash<M>)
pub fn push_leaf_with_hash(&mut self, leaf_with_hash: LeafWithHash<M>)
Appends a pre-hashed leaf to the tree
sourcepub fn root(&mut self) -> M::Output
pub fn root(&mut self) -> M::Output
Returns the root of the tree, computing it if necessary. Repeated queries return a cached result.
sourcepub fn get_leaves(&self, range: Range<usize>) -> Vec<Vec<u8>>
pub fn get_leaves(&self, range: Range<usize>) -> Vec<Vec<u8>>
Returns the requested range of leaves
sourcepub fn leaves(&self) -> &[LeafWithHash<M>]
pub fn leaves(&self) -> &[LeafWithHash<M>]
Returns all leaves in the tree
sourcepub fn check_range_proof(
&self,
root: &M::Output,
leaves: &[M::Output],
proof: &[M::Output],
leaves_start_idx: usize,
) -> Result<(), RangeProofError>
pub fn check_range_proof( &self, root: &M::Output, leaves: &[M::Output], proof: &[M::Output], leaves_start_idx: usize, ) -> Result<(), RangeProofError>
Checks a given range proof
sourcepub fn build_range_proof(&mut self, leaf_range: Range<usize>) -> Proof<M>
pub fn build_range_proof(&mut self, leaf_range: Range<usize>) -> Proof<M>
Creates a range proof providing the sibling hashes required to show that a set of values really does occur in the merkle tree at some half-open range of indices. Intermediate hashes are identified by an in-order traversal and are returned in that same order. Panics if the range to prove is larger than the tree’s leaf array.
Example: consider the following merkle tree with leaves [C, D, E, F]
root
/ \
A B
/ \ / \
C D E F
A range proof of build_range_proof(1..3) would return the vector [C, F], since those two hashes, together with the two leaves in the range, are sufficient to reconstruct the tree