Mmr

Struct Mmr 

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

Source

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 context
  • pinned_nodes - Digest values in the order returned by Proof::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.
Source

pub async fn init( context: E, hasher: &mut impl Hasher<H>, cfg: Config, ) -> Result<Self, Error>

Initialize a new Mmr instance.

Source

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.

  1. Fresh Start: existing_size < lower_bound

    • Deletes existing data (if any)
    • Creates new Journal with pruning boundary and size lower_bound
  2. Prune and Reuse: lower_bound ≤ existing_size ≤ upper_bound

    • Sets in-memory MMR size to existing_size
    • Prunes the journal to lower_bound
  3. 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
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 is_dirty(&self) -> bool

Returns whether there are pending updates.

Source

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

Source

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.

Source

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.

Source

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.

Source

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

Return the root of the MMR.

§Warning

Panics if there are unprocessed updates.

Source

pub fn process_updates(&mut self, h: &mut impl Hasher<H>)

Process all batched updates without syncing to disk.

Source

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.

Source

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.

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, or ElementPruned error if some element needed to generate the proof has been pruned.

§Warning

Panics if there are unprocessed updates.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Return the position of the oldest retained node in the MMR, not including pinned nodes.

Source

pub async fn close(self, h: &mut impl Hasher<H>) -> Result<(), Error>

Close the MMR, syncing any cached elements to disk and closing the journal.

Source

pub async fn destroy(self) -> Result<(), Error>

Close and permanently remove any disk resources.

Trait Implementations§

Source§

impl<E: RStorage + Clock + Metrics, H: CHasher> Builder<H> for Mmr<E, 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<E: RStorage + Clock + Metrics, H: CHasher> Storage<<H as Hasher>::Digest> for Mmr<E, H>

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<E, H> Freeze for Mmr<E, H>
where <H as Hasher>::Digest: Freeze, E: Freeze, <E as Storage>::Blob: Freeze,

§

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>
where <H as Hasher>::Digest: Unpin, E: Unpin, <E as Storage>::Blob: Unpin,

§

impl<E, H> !UnwindSafe for Mmr<E, 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,