pub struct Bitmap<H: CHasher, const N: usize> { /* 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.
§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<H: CHasher, const N: usize> Bitmap<H, N>
impl<H: CHasher, const N: usize> Bitmap<H, N>
Sourcepub const CHUNK_SIZE: usize = N
pub const CHUNK_SIZE: usize = N
The size of a chunk in bytes.
Sourcepub const CHUNK_SIZE_BITS: u64
pub const CHUNK_SIZE_BITS: u64
The size of a chunk in bits.
pub fn size(&self) -> u64
pub fn get_node(&self, position: u64) -> Option<H::Digest>
Sourcepub async fn restore_pruned<C: RStorage + Metrics + Clock>(
context: C,
partition: &str,
pool: Option<ThreadPool>,
) -> Result<Self, Error>
pub async fn restore_pruned<C: RStorage + Metrics + Clock>( context: C, partition: &str, pool: Option<ThreadPool>, ) -> 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.
Sourcepub async fn write_pruned<C: RStorage + Metrics + Clock>(
&self,
context: C,
partition: &str,
) -> Result<(), Error>
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.
Sourcepub fn bit_count(&self) -> u64
pub fn bit_count(&self) -> u64
Return the number of bits currently stored in the bitmap, irrespective of any pruning.
Sourcepub fn pruned_bits(&self) -> u64
pub fn pruned_bits(&self) -> u64
Return the number of bits that have been pruned from this bitmap.
Sourcepub fn prune_to_bit(&mut self, bit_offset: u64)
pub fn prune_to_bit(&mut self, bit_offset: u64)
Prune the bitmap to the most recent chunk boundary that contains the referenced bit.
§Warning
-
Panics if the referenced bit is greater than the number of bits in the bitmap.
-
Panics if there are unprocessed updates.
Sourcepub fn last_chunk(&self) -> (&[u8; N], u64)
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).
Sourcepub fn get_chunk(&self, bit_offset: u64) -> &[u8; N]
pub fn get_chunk(&self, bit_offset: u64) -> &[u8; N]
Returns the bitmap chunk containing the specified bit.
§Warning
Panics if the bit doesn’t exist or has been pruned.
Sourcepub fn append_chunk_unchecked(&mut self, chunk: &[u8; N])
pub fn append_chunk_unchecked(&mut self, chunk: &[u8; N])
Efficiently add a chunk of bits to the bitmap.
§Warning
-
The update will not impact the root until
sync
is called. -
Assumes we are at a chunk boundary (that is,
self.next_bit
is 0) and panics otherwise.
Sourcepub fn append_byte_unchecked(&mut self, byte: u8)
pub fn append_byte_unchecked(&mut self, byte: u8)
Efficiently add a byte’s worth of bits to the bitmap.
§Warning
-
The update will not impact the root until
sync
is called. -
Assumes self.next_bit is currently byte aligned, and panics otherwise.
Sourcepub fn get_bit_from_chunk(chunk: &[u8; N], bit_offset: u64) -> bool
pub fn get_bit_from_chunk(chunk: &[u8; N], bit_offset: u64) -> bool
Get the value of a bit from its chunk.
Sourcepub fn set_bit(&mut self, bit_offset: u64, bit: bool)
pub fn set_bit(&mut self, bit_offset: u64, bit: bool)
Set the value of the referenced bit.
§Warning
The update will not impact the root until sync
is called.
Sourcepub fn is_dirty(&self) -> bool
pub fn is_dirty(&self) -> bool
Whether there are any updates that are not yet reflected in this bitmap’s root.
Sourcepub fn dirty_chunks(&self) -> Vec<u64>
pub fn dirty_chunks(&self) -> Vec<u64>
The chunks (identified by their number) that have been modified or added since the last sync
.
Sourcepub async fn sync(&mut self, hasher: &mut impl Hasher<H>) -> Result<(), Error>
pub async fn sync(&mut self, hasher: &mut impl Hasher<H>) -> Result<(), Error>
Process all updates not yet reflected in the bitmap’s root.
Sourcepub async fn root(
&self,
hasher: &mut impl Hasher<H>,
) -> Result<H::Digest, Error>
pub async fn root( &self, hasher: &mut impl Hasher<H>, ) -> Result<H::Digest, Error>
Return the 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)
§Warning
Panics if there are unprocessed updates.
Sourcepub fn partial_chunk_root(
hasher: &mut H,
mmr_root: &H::Digest,
next_bit: u64,
last_chunk_digest: &H::Digest,
) -> H::Digest
pub fn partial_chunk_root( hasher: &mut H, mmr_root: &H::Digest, next_bit: u64, last_chunk_digest: &H::Digest, ) -> H::Digest
Returns a root digest that incorporates bits that aren’t part of the MMR yet because they belong to the last (unfilled) chunk.
Sourcepub async fn proof(
&self,
hasher: &mut impl Hasher<H>,
bit_offset: u64,
) -> Result<(Proof<H::Digest>, [u8; N]), Error>
pub async fn proof( &self, hasher: &mut impl Hasher<H>, bit_offset: u64, ) -> Result<(Proof<H::Digest>, [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.
§Warning
Panics if there are unprocessed updates.
Trait Implementations§
Auto Trait Implementations§
impl<H, const N: usize> Freeze for Bitmap<H, N>
impl<H, const N: usize> !RefUnwindSafe for Bitmap<H, N>
impl<H, const N: usize> Send for Bitmap<H, N>
impl<H, const N: usize> Sync for Bitmap<H, N>
impl<H, const N: usize> Unpin for Bitmap<H, N>
impl<H, const N: usize> !UnwindSafe for Bitmap<H, N>
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