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>
where Db: PreimageDb<M::Output>, M: MerkleHash + Default,

source

pub fn new() -> Self

Constructs an empty merkle tree with a default hasher

source§

impl<Db, M> MerkleTree<Db, M>
where Db: PreimageDb<M::Output>, M: MerkleHash,

source

pub fn with_hasher(hasher: M) -> Self

Constructs an empty merkle tree with the given hasher

source

pub fn push_raw_leaf(&mut self, raw_leaf: &[u8])

Appends the given leaf to the tree

source

pub fn push_leaf_with_hash(&mut self, leaf_with_hash: LeafWithHash<M>)

Appends a pre-hashed leaf to the tree

source

pub fn root(&mut self) -> M::Output

Returns the root of the tree, computing it if necessary. Repeated queries return a cached result.

source

pub fn get_leaves(&self, range: Range<usize>) -> Vec<Vec<u8>>

Returns the requested range of leaves

source

pub fn leaves(&self) -> &[LeafWithHash<M>]

Returns all leaves in the tree

source

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

source

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

source

pub fn get_range_with_proof( &mut self, leaf_range: Range<usize>, ) -> (Vec<Vec<u8>>, Proof<M>)

Fetches the requested range of leaves, along with a proof of correctness.

source

pub fn get_index_with_proof(&mut self, idx: usize) -> (Vec<u8>, Proof<M>)

Fetches the leaf at the given index, along with a proof of inclusion.

Trait Implementations§

source§

impl<Db: PreimageDb<<M as MerkleHash>::Output>, M: MerkleHash + Default> Default for MerkleTree<Db, M>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Db, M> Freeze for MerkleTree<Db, M>
where Db: Freeze, M: Freeze, <M as MerkleHash>::Output: Freeze,

§

impl<Db, M> !RefUnwindSafe for MerkleTree<Db, M>

§

impl<Db, M> !Send for MerkleTree<Db, M>

§

impl<Db, M> !Sync for MerkleTree<Db, M>

§

impl<Db, M> Unpin for MerkleTree<Db, M>
where Db: Unpin, M: Unpin, <M as MerkleHash>::Output: Unpin,

§

impl<Db, M> !UnwindSafe for MerkleTree<Db, M>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.