AuthenticatedBitMap

Struct AuthenticatedBitMap 

Source
pub struct AuthenticatedBitMap<D: Digest, const N: usize, S: State<D> = Clean<D>> { /* private fields */ }
Expand description

A bitmap supporting inclusion proofs through Merkelization.

Merkelization of the bitmap is performed over chunks of N bytes. If the goal is to minimize proof sizes, choose an N that is equal to the size or double the size of the hasher’s digest.

§Type States

The bitmap uses the type-state pattern to enforce at compile-time whether the bitmap has pending updates that must be merkleized before computing proofs. CleanBitMap represents a bitmap whose root digest has been computed and cached. DirtyBitMap represents a bitmap with pending updates. A dirty bitmap can be converted into a clean bitmap by calling DirtyBitMap::merkleize.

§Warning

Even though we use u64 identifiers for bits, on 32-bit machines, the maximum addressable bit is limited to (u32::MAX * N * 8).

Implementations§

Source§

impl<D: Digest, const N: usize, S: State<D>> BitMap<D, N, S>

Source

pub const CHUNK_SIZE_BITS: u64 = PrunableBitMap<N>::CHUNK_SIZE_BITS

The size of a chunk in bits.

Source

pub fn size(&self) -> Position

Return the size of the bitmap in bits.

Source

pub const fn len(&self) -> u64

Return the number of bits currently stored in the bitmap, irrespective of any pruning.

Source

pub const fn is_empty(&self) -> bool

Returns true if the bitmap is empty.

Source

pub const fn pruned_bits(&self) -> u64

Return the number of bits that have been pruned from this bitmap.

Source

pub fn last_chunk(&self) -> (&[u8; N], u64)

Return the last chunk of the bitmap and its size in bits. The size can be 0 (meaning the last chunk is empty).

Source

pub fn get_chunk_containing(&self, bit: u64) -> &[u8; N]

Returns the bitmap chunk containing the specified bit.

§Warning

Panics if the bit doesn’t exist or has been pruned.

Source

pub fn get_bit(&self, bit: u64) -> bool

Get the value of a bit.

§Warning

Panics if the bit doesn’t exist or has been pruned.

Source

pub const fn get_bit_from_chunk(chunk: &[u8; N], bit: u64) -> bool

Get the value of a bit from its chunk. bit is an index into the entire bitmap, not just the chunk.

Source

pub fn partial_chunk_root( hasher: &mut impl Hasher<Digest = D>, mmr_root: &D, next_bit: u64, last_chunk_digest: &D, ) -> D

Returns a root digest that incorporates bits that aren’t part of the MMR yet because they belong to the last (unfilled) chunk.

Source

pub fn verify_bit_inclusion( hasher: &mut impl Hasher<D>, proof: &Proof<D>, chunk: &[u8; N], bit: u64, root: &D, ) -> bool

Verify whether proof proves that the chunk containing the given bit belongs to the bitmap corresponding to root.

Source

pub async fn destroy<C: RStorage + Metrics + Clock>( context: C, partition: &str, ) -> Result<(), Error>

Destroy the bitmap metadata from disk.

Source§

impl<D: Digest, const N: usize> BitMap<D, N>

Source

pub fn new(hasher: &mut impl Hasher<D>, pool: Option<ThreadPool>) -> Self

Return a new empty bitmap.

Source

pub fn get_node(&self, position: Position) -> Option<D>

Source

pub async fn restore_pruned<C: RStorage + Metrics + Clock>( context: C, partition: &str, pool: Option<ThreadPool>, hasher: &mut impl Hasher<D>, ) -> Result<Self, Error>

Restore the fully pruned state of a bitmap from the metadata in the given partition. (The caller must still replay retained elements to restore its full state.)

The metadata must store the number of pruned chunks and the pinned digests corresponding to that pruning boundary.

Returns an error if the bitmap could not be restored, e.g. because of data corruption or underlying storage error.

Source

pub async fn write_pruned<C: RStorage + Metrics + Clock>( &self, context: C, partition: &str, ) -> Result<(), Error>

Write the information necessary to restore the bitmap in its fully pruned state at its last pruning boundary. Restoring the entire bitmap state is then possible by replaying the retained elements.

Source

pub fn prune_to_bit(&mut self, bit: u64) -> Result<(), Error>

Prune all complete chunks before the chunk containing the given bit.

The chunk containing bit and all subsequent chunks are retained. All chunks before it are pruned from the bitmap and the underlying MMR.

If bit equals the bitmap length, this prunes all complete chunks while retaining the empty trailing chunk, preparing the bitmap for appending new data.

Source

pub const fn root(&self) -> D

Return the cached root digest against which inclusion proofs can be verified.

§Format

The root digest is simply that of the underlying MMR whenever the bit count falls on a chunk boundary. Otherwise, the root is computed as follows in order to capture the bits that are not yet part of the MMR:

hash(mmr_root || next_bit as u64 be_bytes || last_chunk_digest)

The root is computed during merkleization and cached, so this method is cheap to call.

Source

pub async fn proof( &self, hasher: &mut impl Hasher<D>, bit: u64, ) -> Result<(Proof<D>, [u8; N]), Error>

Return an inclusion proof for the specified bit, along with the chunk of the bitmap containing that bit. The proof can be used to prove any bit in the chunk.

The bitmap proof stores the number of bits in the bitmap within the size field of the proof instead of MMR size since the underlying MMR’s size does not reflect the number of bits in any partial chunk. The underlying MMR size can be derived from the number of bits as leaf_num_to_pos(proof.size / BitMap<_, N>::CHUNK_SIZE_BITS).

§Errors

Returns Error::BitOutOfBounds if bit is out of bounds.

Source

pub fn into_dirty(self) -> DirtyBitMap<D, N>

Convert this clean bitmap into a dirty bitmap without making any changes to it.

Source§

impl<D: Digest, const N: usize> BitMap<D, N, Dirty>

Source

pub fn push(&mut self, bit: bool)

Add a single bit to the end of the bitmap.

§Warning

The update will not affect the root until merkleize is called.

Source

pub fn set_bit(&mut self, bit: u64, value: bool)

Set the value of the given bit.

§Warning

The update will not impact the root until merkleize is called.

Source

pub fn dirty_chunks(&self) -> Vec<Location>

The chunks that have been modified or added since the last call to merkleize.

Source

pub async fn merkleize( self, hasher: &mut impl Hasher<D>, ) -> Result<CleanBitMap<D, N>, Error>

Merkleize all updates not yet reflected in the bitmap’s root.

Auto Trait Implementations§

§

impl<D, const N: usize, S> Freeze for BitMap<D, N, S>
where S: Freeze, D: Freeze,

§

impl<D, const N: usize, S = Clean<D>> !RefUnwindSafe for BitMap<D, N, S>

§

impl<D, const N: usize, S> Send for BitMap<D, N, S>
where S: Send,

§

impl<D, const N: usize, S> Sync for BitMap<D, N, S>
where S: Sync,

§

impl<D, const N: usize, S> Unpin for BitMap<D, N, S>
where S: Unpin, D: Unpin,

§

impl<D, const N: usize, S = Clean<D>> !UnwindSafe for BitMap<D, N, S>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

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
§

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

§

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
§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,