use crate::{
index::{Cursor, Unordered as Index},
journal::{
contiguous::{Contiguous, Mutable},
Error as JournalError,
},
merkle::{
hasher::{Hasher as MerkleHasher, Standard as StandardHasher},
Bagging, Family, Location,
},
qmdb::operation::Operation,
};
use commonware_codec::Encode;
use commonware_cryptography::Hasher;
use commonware_utils::{cache::Clock, NZUsize};
use core::num::NonZeroUsize;
use futures::{pin_mut, StreamExt as _};
use thiserror::Error;
pub mod any;
pub mod batch_chain;
pub(crate) mod bitmap;
pub(crate) mod compact;
#[cfg(test)]
mod conformance;
pub mod current;
pub mod immutable;
pub mod keyless;
mod metrics;
pub mod operation;
pub mod store;
pub mod sync;
pub mod verify;
pub use verify::{
create_multi_proof, create_proof_store, verify_multi_proof, verify_proof,
verify_proof_and_extract_digests, verify_proof_and_pinned_nodes,
};
pub(crate) const ROOT_BAGGING: Bagging = Bagging::BackwardFold;
pub const fn hasher<H: Hasher>() -> StandardHasher<H> {
StandardHasher::new(ROOT_BAGGING)
}
fn single_operation_root<F: Family, H: Hasher>(operation: &impl Encode) -> H::Digest {
let hasher = hasher::<H>();
let leaf = MerkleHasher::<F>::leaf_digest(
&hasher,
F::location_to_position(Location::new(0)),
&operation.encode(),
);
MerkleHasher::<F>::root(&hasher, Location::new(1), 0, [&leaf])
.expect("a single-leaf Merkle root is always valid")
}
pub(crate) async fn find_inactivity_floor_at<F, R>(
reader: &R,
op_count: Location<F>,
floor_of: impl Fn(&R::Item) -> Option<Location<F>>,
) -> Result<Location<F>, Error<F>>
where
F: Family,
R: Contiguous,
{
let Some(last_op) = op_count.checked_sub(1) else {
return Err(Error::HistoricalFloorPruned(op_count));
};
let last_op = *last_op;
let bounds = reader.bounds();
if last_op < bounds.start {
return Err(JournalError::ItemPruned(last_op).into());
}
let op = reader.read(last_op).await?;
let floor = floor_of(&op).ok_or(Error::HistoricalFloorPruned(op_count))?;
if floor > Location::new(last_op) {
return Err(Error::DataCorrupted(
"inactivity floor exceeds commit location",
));
}
Ok(floor)
}
pub(crate) async fn inactive_peaks_at<F, R>(
reader: &R,
op_count: Location<F>,
floor_of: impl Fn(&R::Item) -> Option<Location<F>>,
) -> Result<usize, Error<F>>
where
F: Family,
R: Contiguous,
{
if op_count == Location::new(0) {
return Ok(0);
}
let floor = find_inactivity_floor_at::<F, _>(reader, op_count, floor_of).await?;
Ok(F::inactive_peaks(F::location_to_position(op_count), floor))
}
#[derive(Error, Debug)]
pub enum Error<F: Family> {
#[error("data corrupted: {0}")]
DataCorrupted(&'static str),
#[error("merkle error: {0}")]
Merkle(#[from] crate::merkle::Error<F>),
#[error("metadata error: {0}")]
Metadata(#[from] crate::metadata::Error),
#[error("journal error: {0}")]
Journal(#[from] crate::journal::Error),
#[error("runtime error: {0}")]
Runtime(#[from] commonware_runtime::Error),
#[error("operation pruned: {0}")]
OperationPruned(Location<F>),
#[error("key not found")]
KeyNotFound,
#[error("key exists")]
KeyExists,
#[error("unexpected data at location: {0}")]
UnexpectedData(Location<F>),
#[error("location out of bounds: {0} >= {1}")]
LocationOutOfBounds(Location<F>, Location<F>),
#[error("prune location {0} beyond minimum required location {1}")]
PruneBeyondMinRequired(Location<F>, Location<F>),
#[error(
"stale batch: db has {db_size} ops, batch requires {batch_db_size}, {batch_base_size}, or an ancestor boundary"
)]
StaleBatch {
db_size: u64,
batch_db_size: u64,
batch_base_size: u64,
},
#[error("floor regressed: batch floor {0} < current floor {1}")]
FloorRegressed(Location<F>, Location<F>),
#[error("floor beyond commit location: floor {0} > commit loc {1}")]
FloorBeyondSize(Location<F>, Location<F>),
#[error("historical floor pruned for size: {0}")]
HistoricalFloorPruned(Location<F>),
}
impl<F: Family> From<crate::journal::authenticated::Error<F>> for Error<F> {
fn from(e: crate::journal::authenticated::Error<F>) -> Self {
match e {
crate::journal::authenticated::Error::Journal(j) => Self::Journal(j),
crate::journal::authenticated::Error::Merkle(m) => Self::Merkle(m),
}
}
}
const SNAPSHOT_READ_BUFFER_SIZE: NonZeroUsize = NZUsize!(1 << 16);
pub(super) async fn build_snapshot_from_log<F, C, I, Fn>(
inactivity_floor_loc: crate::merkle::Location<F>,
reader: &C,
snapshot: &mut I,
cache_size: Option<NonZeroUsize>,
mut callback: Fn,
) -> Result<usize, Error<F>>
where
F: crate::merkle::Family,
C: Contiguous<Item: Operation<F>>,
I: Index<Value = crate::merkle::Location<F>>,
Fn: FnMut(bool, Option<crate::merkle::Location<F>>),
{
let bounds = reader.bounds();
let stream = reader
.replay(*inactivity_floor_loc, SNAPSHOT_READ_BUFFER_SIZE)
.await?;
pin_mut!(stream);
let last_commit_loc = bounds.end.saturating_sub(1);
let mut cache = cache_size.map(Clock::<u64, <C::Item as Operation<F>>::Key>::new);
let mut active_keys: usize = 0;
while let Some(result) = stream.next().await {
let (loc, op) = result?;
if let Some(key) = op.key() {
if op.is_delete() {
let old_loc = delete_key(snapshot, reader, key, cache.as_mut()).await?;
callback(false, old_loc);
if old_loc.is_some() {
active_keys -= 1;
}
} else if op.is_update() {
let new_loc = crate::merkle::Location::new(loc);
let old_loc = update_key(snapshot, reader, key, new_loc, cache.as_mut()).await?;
callback(true, old_loc);
if old_loc.is_none() {
active_keys += 1;
}
if let Some(cache) = cache.as_mut() {
cache.put(loc, key.clone());
}
}
} else if op.has_floor().is_some() {
callback(loc == last_commit_loc, None);
}
}
Ok(active_keys)
}
async fn delete_key<F, I, R>(
snapshot: &mut I,
reader: &R,
key: &<R::Item as Operation<F>>::Key,
cache: Option<&mut Clock<u64, <R::Item as Operation<F>>::Key>>,
) -> Result<Option<Location<F>>, Error<F>>
where
F: Family,
I: Index<Value = Location<F>>,
R: Contiguous,
R::Item: Operation<F>,
{
let Some(mut cursor) = snapshot.get_mut(key) else {
return Ok(None);
};
let Some(loc) = find_update_op::<F, _>(reader, &mut cursor, key, cache).await? else {
return Ok(None);
};
cursor.delete();
Ok(Some(loc))
}
async fn update_key<F, I, R>(
snapshot: &mut I,
reader: &R,
key: &<R::Item as Operation<F>>::Key,
new_loc: Location<F>,
cache: Option<&mut Clock<u64, <R::Item as Operation<F>>::Key>>,
) -> Result<Option<Location<F>>, Error<F>>
where
F: Family,
I: Index<Value = Location<F>>,
R: Contiguous,
R::Item: Operation<F>,
{
let Some(mut cursor) = snapshot.get_mut_or_insert(key, new_loc) else {
return Ok(None);
};
if let Some(loc) = find_update_op::<F, _>(reader, &mut cursor, key, cache).await? {
assert!(new_loc > loc);
cursor.update(new_loc);
return Ok(Some(loc));
}
cursor.insert(new_loc);
Ok(None)
}
async fn find_update_op<F, R>(
reader: &R,
cursor: &mut impl Cursor<Value = Location<F>>,
key: &<R::Item as Operation<F>>::Key,
mut cache: Option<&mut Clock<u64, <R::Item as Operation<F>>::Key>>,
) -> Result<Option<Location<F>>, Error<F>>
where
F: Family,
R: Contiguous,
R::Item: Operation<F>,
{
while let Some(&loc) = cursor.next() {
let matches = if let Some(k) = cache.as_deref().and_then(|c| c.get(&*loc)) {
*k == *key
} else {
let op = reader.read(*loc).await?;
let k = op.key().expect("operation without key");
let matches = *k == *key;
if let Some(cache) = cache.as_deref_mut() {
cache.put(*loc, k.clone());
}
matches
};
if matches {
return Ok(Some(loc));
}
}
Ok(None)
}
fn update_known_loc<F: Family, I: Index<Value = Location<F>>>(
snapshot: &mut I,
key: &[u8],
old_loc: Location<F>,
new_loc: Location<F>,
) {
let mut cursor = snapshot.get_mut(key).expect("key should be known to exist");
assert!(
cursor.find(|&loc| *loc == old_loc),
"known key with given old_loc should have been found"
);
cursor.update(new_loc);
}
fn delete_known_loc<F: Family, I: Index<Value = Location<F>>>(
snapshot: &mut I,
key: &[u8],
old_loc: Location<F>,
) {
let mut cursor = snapshot.get_mut(key).expect("key should be known to exist");
assert!(
cursor.find(|&loc| *loc == old_loc),
"known key with given old_loc should have been found"
);
cursor.delete();
}
pub(crate) struct FloorHelper<
'a,
F: Family,
I: Index<Value = Location<F>>,
C: Mutable<Item: Operation<F>>,
> {
pub snapshot: &'a mut I,
pub log: &'a mut C,
}
impl<F, I, C> FloorHelper<'_, F, I, C>
where
F: Family,
I: Index<Value = Location<F>>,
C: Mutable<Item: Operation<F>>,
{
async fn move_op_if_active(
&mut self,
op: C::Item,
old_loc: Location<F>,
) -> Result<bool, Error<F>> {
let Some(key) = op.key() else {
return Ok(false); };
{
let Some(mut cursor) = self.snapshot.get_mut(key) else {
return Ok(false);
};
if !cursor.find(|&loc| loc == old_loc) {
return Ok(false);
}
cursor.update(Location::<F>::new(self.log.bounds().end));
}
self.log.append(&op).await?;
Ok(true)
}
async fn raise_floor(
&mut self,
mut inactivity_floor_loc: Location<F>,
) -> Result<Location<F>, Error<F>> {
let tip_loc: Location<F> = Location::new(self.log.bounds().end);
loop {
assert!(
*inactivity_floor_loc < tip_loc,
"no active operations above the inactivity floor"
);
let old_loc = inactivity_floor_loc;
inactivity_floor_loc += 1;
let op = self.log.read(*old_loc).await?;
if self.move_op_if_active(op, old_loc).await? {
return Ok(inactivity_floor_loc);
}
}
}
}