pub struct Mmr<E: RStorage + Clock + Metrics, H: CHasher> { /* private fields */ }
Expand description
A MMR backed by a fixed-item-length journal.
Implementations§
Source§impl<E: RStorage + Clock + Metrics, H: CHasher> Mmr<E, H>
impl<E: RStorage + Clock + Metrics, H: CHasher> Mmr<E, H>
Sourcepub async fn init_from_pinned_nodes(
context: E,
pinned_nodes: Vec<H::Digest>,
mmr_size: u64,
config: Config,
) -> Result<Self, Error>
pub async fn init_from_pinned_nodes( context: E, pinned_nodes: Vec<H::Digest>, mmr_size: u64, config: Config, ) -> Result<Self, Error>
Initialize a new journaled MMR from an MMR’s size and set of pinned nodes.
This creates a journaled MMR that appears to have mmr_size
elements, all of which
are pruned, leaving only the minimal set of pinned_nodes
required for proof generation.
The next element added will be at position mmr_size
.
The returned MMR is functionally equivalent to a journaled MMR that was created, populated, and then pruned up to its size.
§Arguments
context
- Storage contextpinned_nodes
- Digest values in the order returned byProof::nodes_to_pin(mmr_size)
mmr_size
- The logical size of the MMR (all elements before this are considered pruned)config
- Journaled MMR configuration. Any data in the given journal and metadata partitions will be overwritten.
Sourcepub async fn init(
context: E,
hasher: &mut impl Hasher<H>,
cfg: Config,
) -> Result<Self, Error>
pub async fn init( context: E, hasher: &mut impl Hasher<H>, cfg: Config, ) -> Result<Self, Error>
Initialize a new Mmr
instance.
Sourcepub async fn init_sync(
context: E,
cfg: SyncConfig<H::Digest>,
) -> Result<Self, Error>
pub async fn init_sync( context: E, cfg: SyncConfig<H::Digest>, ) -> Result<Self, Error>
Initialize an MMR for synchronization, reusing existing data if possible.
Handles three sync scenarios based on existing journal data vs. the given sync boundaries.
-
Fresh Start: existing_size < lower_bound
- Deletes existing data (if any)
- Creates new Journal with pruning boundary and size
lower_bound
-
Prune and Reuse: lower_bound ≤ existing_size ≤ upper_bound
- Sets in-memory MMR size to
existing_size
- Prunes the journal to
lower_bound
- Sets in-memory MMR size to
-
Prune and Rewind: existing_size > upper_bound
- Rewinds the journal to size
upper_bound+1
- Sets in-memory MMR size to
upper_bound+1
- Prunes the journal to
lower_bound
- Rewinds the journal to size
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 async fn get_node(&self, position: u64) -> Result<Option<H::Digest>, Error>
Sourcepub async fn add(
&mut self,
h: &mut impl Hasher<H>,
element: &[u8],
) -> Result<u64, Error>
pub async fn add( &mut self, h: &mut impl Hasher<H>, element: &[u8], ) -> Result<u64, Error>
Add an element to the MMR and return its position in the MMR. Elements added to the MMR
aren’t persisted to disk until sync
is called.
§Warning
Panics if there are unprocessed updates.
Sourcepub async fn add_batched(
&mut self,
h: &mut impl Hasher<H>,
element: &[u8],
) -> Result<u64, Error>
pub async fn add_batched( &mut self, h: &mut impl Hasher<H>, element: &[u8], ) -> Result<u64, Error>
Add an element to the MMR, delaying the computation of ancestor digests
until the next sync
.
Sourcepub async fn pop(&mut self, leaves_to_pop: usize) -> Result<(), Error>
pub async fn pop(&mut self, leaves_to_pop: usize) -> Result<(), Error>
Pop the given number of elements from the tip of the MMR assuming they exist, and otherwise return Empty or ElementPruned errors. The backing journal is synced to disk before returning.
§Warning
Panics if there are unprocessed batch updates.
Sourcepub fn process_updates(&mut self, h: &mut impl Hasher<H>)
pub fn process_updates(&mut self, h: &mut impl Hasher<H>)
Process all batched updates without syncing to disk.
Sourcepub async fn sync(&mut self, h: &mut impl Hasher<H>) -> Result<(), Error>
pub async fn sync(&mut self, h: &mut impl Hasher<H>) -> Result<(), Error>
Process all batched updates and sync the MMR to disk. If pool
is non-null, then it will be
used to parallelize the sync.
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, or ElementPruned error if some element needed to generate the proof has been pruned.
§Warning
Panics if there are unprocessed 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, or ElementPruned error if some element needed to generate the proof has been pruned.
§Warning
Panics if there are unprocessed updates.
Sourcepub async fn historical_range_proof(
&self,
size: u64,
start_element_pos: u64,
end_element_pos: u64,
) -> Result<Proof<H::Digest>, Error>
pub async fn historical_range_proof( &self, size: u64, start_element_pos: u64, end_element_pos: u64, ) -> Result<Proof<H::Digest>, Error>
Analogous to range_proof but for a previous database state.
Specifically, the state when the MMR had size
elements.
Sourcepub async fn prune_all(&mut self, h: &mut impl Hasher<H>) -> Result<(), Error>
pub async fn prune_all(&mut self, h: &mut impl Hasher<H>) -> Result<(), Error>
Prune as many nodes as possible, leaving behind at most items_per_blob nodes in the current blob.
Sourcepub async fn prune_to_pos(
&mut self,
h: &mut impl Hasher<H>,
pos: u64,
) -> Result<(), Error>
pub async fn prune_to_pos( &mut self, h: &mut impl Hasher<H>, pos: u64, ) -> Result<(), Error>
Prune all nodes up to but not including the given position and update the pinned nodes.
This implementation ensures that no failure can leave the MMR in an unrecoverable state, requiring it sync the MMR to write any potential unprocessed updates.
Sourcepub fn pruned_to_pos(&self) -> u64
pub fn pruned_to_pos(&self) -> u64
The highest position for which this MMR has been pruned, or 0 if this MMR has never been pruned.
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 pinned nodes.
Trait Implementations§
Source§impl<E: RStorage + Clock + Metrics, H: CHasher> Builder<H> for Mmr<E, H>
impl<E: RStorage + Clock + Metrics, H: CHasher> Builder<H> for Mmr<E, H>
Auto Trait Implementations§
impl<E, H> Freeze for Mmr<E, H>
impl<E, H> !RefUnwindSafe for Mmr<E, H>
impl<E, H> Send for Mmr<E, H>
impl<E, H> Sync for Mmr<E, H>
impl<E, H> Unpin for Mmr<E, H>
impl<E, H> !UnwindSafe for Mmr<E, 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