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>
impl<H: CHasher> Mmr<H>
Sourcepub fn size(&self) -> u64
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.
Sourcepub fn last_leaf_pos(&self) -> Option<u64>
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.
pub fn pruned_to_pos(&self) -> u64
Sourcepub fn oldest_retained_pos(&self) -> Option<u64>
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.
Sourcepub fn get_node_unchecked(&self, pos: u64) -> &H::Digest
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.
Sourcepub fn get_node(&self, pos: u64) -> Option<H::Digest>
pub fn get_node(&self, pos: u64) -> Option<H::Digest>
Returns the requested node or None if it is not stored in the MMR.
Sourcepub fn add(&mut self, hasher: &mut impl Hasher<H>, element: &[u8]) -> u64
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.
Sourcepub fn add_batched(
&mut self,
hasher: &mut impl Hasher<H>,
element: &[u8],
) -> u64
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.
Sourcepub fn pop(&mut self) -> Result<u64, Error>
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.
Sourcepub fn update_leaf(
&mut self,
hasher: &mut impl Hasher<H>,
pos: u64,
element: &[u8],
)
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.
Sourcepub fn update_leaf_batched<T: AsRef<[u8]> + Sync>(
&mut self,
hasher: &mut impl Hasher<H>,
updates: &[(u64, T)],
)
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.
Sourcepub async fn proof(&self, element_pos: u64) -> Result<Proof<H::Digest>, Error>
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.
Sourcepub async fn range_proof(
&self,
start_element_pos: u64,
end_element_pos: u64,
) -> Result<Proof<H::Digest>, Error>
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.
Sourcepub fn prune_all(&mut self)
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.
Sourcepub fn prune_to_pos(&mut self, pos: u64)
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.
Sourcepub fn clone_pruned(&self) -> Self
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>
impl<H: CHasher> Builder<H> for Mmr<H>
Auto Trait Implementations§
impl<H> Freeze for Mmr<H>
impl<H> !RefUnwindSafe for Mmr<H>
impl<H> Send for Mmr<H>
impl<H> Sync for Mmr<H>
impl<H> Unpin for Mmr<H>
impl<H> !UnwindSafe for Mmr<H>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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