Mmr

Struct Mmr 

Source
pub struct Mmr<H: CHasher> { /* private fields */ }
Expand description

Implementation of Mmr.

§Max Capacity

The maximum number of elements that can be stored is usize::MAX (u32::MAX on 32-bit architectures).

Implementations§

Source§

impl<H: CHasher> Mmr<H>

Source

pub fn new() -> Self

Return a new (empty) Mmr.

Source

pub fn init(config: Config<H>) -> Self

Return an Mmr initialized with the given config.

Source

pub fn size(&self) -> u64

Return the total number of nodes in the MMR, irrespective of any pruning. The next added element’s position will have this value.

Source

pub fn leaves(&self) -> u64

Return the total number of leaves in the MMR.

Source

pub fn last_leaf_pos(&self) -> Option<u64>

Return the position of the last leaf in this MMR, or None if the MMR is empty.

Source

pub fn pruned_to_pos(&self) -> u64

Source

pub fn oldest_retained_pos(&self) -> Option<u64>

Return the position of the oldest retained node in the MMR, not including those cached in pinned_nodes.

Source

pub fn get_node_unchecked(&self, pos: u64) -> &H::Digest

Returns the requested node, assuming it is either retained or known to exist in the pinned_nodes map.

Source

pub fn get_node(&self, pos: u64) -> Option<H::Digest>

Returns the requested node or None if it is not stored in the MMR.

Source

pub fn add(&mut self, hasher: &mut impl Hasher<H>, element: &[u8]) -> u64

Add element to the MMR and return its position in the MMR. The element can be an arbitrary byte slice, and need not be converted to a digest first.

§Warning

Panics if there are unprocessed batch updates.

Source

pub fn add_batched( &mut self, hasher: &mut impl Hasher<H>, element: &[u8], ) -> u64

Add element to the MMR and return its position in the MMR, but without updating ancestors until sync is called. The element can be an arbitrary byte slice, and need not be converted to a digest first.

Source

pub fn pop(&mut self) -> Result<u64, Error>

Pop the most recent leaf element out of the MMR if it exists, returning Empty or ElementPruned errors otherwise.

§Warning

Panics if there are unprocessed batch updates.

Source

pub fn update_leaf( &mut self, hasher: &mut impl Hasher<H>, pos: u64, element: &[u8], )

Change the digest of any retained leaf. This is useful if you want to use the MMR implementation as an updatable binary Merkle tree, and otherwise should be avoided.

§Warning
  • Panics if pos does not correspond to a leaf, or if the leaf has been pruned.

  • This method will change the root and invalidate any previous inclusion proofs.

  • Use of this method will prevent using this structure as a base mmr for grafting.

Source

pub fn update_leaf_batched<T: AsRef<[u8]> + Sync>( &mut self, hasher: &mut impl Hasher<H>, updates: &[(u64, T)], )

Batch update the digests of multiple retained leaves.

§Warning

Panics if any of the updated leaves has been pruned.

Source

pub fn is_dirty(&self) -> bool

Returns whether there are pending updates.

Source

pub fn sync(&mut self, hasher: &mut impl Hasher<H>)

Process any pending batched updates.

Source

pub fn root(&self, hasher: &mut impl Hasher<H>) -> H::Digest

Computes the root of the MMR.

§Warning

Panics if there are unprocessed batch updates.

Source

pub async fn proof(&self, element_pos: u64) -> Result<Proof<H::Digest>, Error>

Return an inclusion proof for the specified element. Returns ElementPruned error if some element needed to generate the proof has been pruned.

§Warning

Panics if there are unprocessed batch updates.

Source

pub async fn range_proof( &self, start_element_pos: u64, end_element_pos: u64, ) -> Result<Proof<H::Digest>, Error>

Return an inclusion proof for the specified range of elements, inclusive of both endpoints. Returns ElementPruned error if some element needed to generate the proof has been pruned.

§Warning

Panics if there are unprocessed batch updates.

Source

pub fn prune_all(&mut self)

Prune all nodes and pin the O(log2(n)) number of them required for proof generation going forward.

§Warning

Panics if there are unprocessed batch updates.

Source

pub fn prune_to_pos(&mut self, pos: u64)

Prune all nodes up to but not including the given position, and pin the O(log2(n)) number of them required for proof generation.

§Warning

Panics if there are unprocessed batch updates.

Source

pub fn clone_pruned(&self) -> Self

A lightweight cloning operation that “clones” only the fully pruned state of this MMR. The output is exactly the same as the result of mmr.prune_all(), only you get a copy without mutating the original, and the thread pool if any is not cloned.

Runtime is Log_2(n) in the number of elements even if the original MMR is never pruned.

§Warning

Panics if there are unprocessed batch updates.

Trait Implementations§

Source§

impl<H: CHasher> Builder<H> for Mmr<H>

Source§

async fn add( &mut self, hasher: &mut impl Hasher<H>, element: &[u8], ) -> Result<u64, Error>

Add element to the MMR and return its position within it. The element can be an arbitrary byte slice, and need not be converted to a digest first.
Source§

fn root(&self, hasher: &mut impl Hasher<H>) -> H::Digest

Return the root hash of the MMR.
Source§

impl<H: CHasher> Default for Mmr<H>

Source§

fn default() -> Self

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

impl<H> Storage<<H as Hasher>::Digest> for Mmr<H>
where H: CHasher,

Source§

fn size(&self) -> u64

Return the number of elements in the MMR.
Source§

async fn get_node(&self, position: u64) -> Result<Option<H::Digest>, Error>

Return the specified node of the MMR if it exists & hasn’t been pruned.

Auto Trait Implementations§

§

impl<H> Freeze for Mmr<H>
where <H as Hasher>::Digest: Freeze,

§

impl<H> !RefUnwindSafe for Mmr<H>

§

impl<H> Send for Mmr<H>

§

impl<H> Sync for Mmr<H>

§

impl<H> Unpin for Mmr<H>
where <H as Hasher>::Digest: Unpin,

§

impl<H> !UnwindSafe for Mmr<H>

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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,