Skip to main content

MerkleTree

Struct MerkleTree 

Source
pub struct MerkleTree { /* private fields */ }
Available on crate feature unstable only.
Expand description

A Blake2b256 Merkle tree.

The tree is built once from a set of leaves and then queried for its root or for MerkleProofs.

Implementations§

Source§

impl MerkleTree

Source

pub fn build_from_serialized<I>(iter: I) -> Self

Build a tree by hashing each input as a leaf.

Source

pub fn build_from_unserialized<I>(iter: I) -> Result<Self, MerkleError>

Build a tree by BCS-encoding each input and using its hash as a leaf.

Returns MerkleError::InvalidInput if any leaf fails BCS serialization, which in practice can only happen for unusual Serialize impls (e.g. infinite recursion); ordinary value types do not fail.

Source

pub fn build_from_leaf_hashes<I>(iter: I) -> Self

Build a tree directly from already-hashed leaves.

Each input node is interpreted as a leaf hash; callers are responsible for applying the LEAF_PREFIX before hashing.

Source

pub fn root(&self) -> Node

The Merkle root.

An empty tree (built from zero leaves) returns Node::Empty.

Source

pub fn n_leaves(&self) -> usize

The number of leaves the tree was built from.

Source

pub fn get_proof(&self, leaf_index: usize) -> Result<MerkleProof, MerkleError>

Build an inclusion proof for the leaf at leaf_index.

Returns MerkleError::InvalidInput if leaf_index >= n_leaves.

Source

pub fn compute_non_inclusion_proof<L>( &self, sorted_leaves: &[L], target: &L, ) -> Result<MerkleNonInclusionProof<L>, MerkleError>
where L: Ord + Clone,

Build a non-inclusion proof showing that target does not appear in this tree.

The tree must have been built from sorted_leaves in sorted order; the caller is responsible for passing the same sorted slice it built the tree from. Returns MerkleError::InvalidInput if target is actually present in sorted_leaves.

Trait Implementations§

Source§

impl Debug for MerkleTree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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
§

impl<U> As for U

§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V