//! Shared synchronization logic for [crate::qmdb::any] databases. //! Contains the implementation of [crate::qmdb::sync::Database] for [Db], covering every //! variant (ordered/unordered, fixed/variable, any snapshot index). //! //! Callers verifying `any` sync proofs directly should use [`crate::qmdb::ProofExt::verify`]. use crate::{ Context, index::{Factory as IndexFactory, Unordered}, journal::{authenticated, contiguous::Mutable}, merkle::{self, Location}, qmdb::{ self, SnapshotBuild, any::{ Config, db::Db, operation::{Operation, update::Update}, }, metrics::Metrics, sync, }, }; use commonware_codec::Codec; use commonware_cryptography::Hasher; use commonware_parallel::Strategy; use commonware_runtime::Spawner; use commonware_utils::range::NonEmptyRange; use core::num::{NonZeroU64, NonZeroUsize}; #[cfg(test)] pub(crate) mod tests; /// Shared helper to build a [Db] from sync components. #[allow(clippy::too_many_arguments)] async fn build_db( context: E, merkle_config: authenticated::Config, log: C, translator: I::Translator, pinned_nodes: Option>, range: NonEmptyRange>, apply_batch_size: NonZeroU64, init_concurrency: NonZeroUsize, init_buffer: NonZeroUsize, cache_size: Option, ) -> Result, qmdb::Error> where F: merkle::Family, E: Context + Spawner, U: Update, I: IndexFactory + SnapshotBuild, H: Hasher, C: Mutable> + 'static, S: Strategy, Operation: Codec, { let hasher = qmdb::hasher::(); let index = I::new(context.child("index"), translator); let log = authenticated::Journal::::from_components( context.child("merkle"), &merkle_config, log, hasher, range.start(), pinned_nodes.unwrap_or_default(), apply_batch_size, ) .await?; let snapshot_context = context.child("snapshot"); let metrics = Metrics::new(context); let db = Db::init_from_log( snapshot_context, index, log, None, init_concurrency, init_buffer, cache_size, metrics, ) .await?; Ok(db) } impl sync::Database for Db where F: merkle::Family, E: Context + Spawner, C: crate::journal::authenticated::Backing< E, Item = Operation, Config = >::Config, > + sync::Journal> + 'static, I: IndexFactory + SnapshotBuild + Unordered>, H: Hasher, U: Update, S: Strategy, Operation: Codec, { type Family = F; type Context = E; type Op = Operation; type Journal = C; type Hasher = H; type Config = Config>::Config, S>; type Digest = H::Digest; async fn init( context: E, config: Self::Config, max_size: Option>, ) -> Result> { crate::qmdb::any::init_with_bitmap(context, config, None, max_size, None).await } fn target(&self) -> sync::Target { sync::Target { root: Self::root(self), range: commonware_utils::non_empty_range!(self.sync_boundary(), self.bounds().end), } } async fn prune_to_target( self, target: &sync::Target, ) -> Result> { self.prune(target.range.start()).await } } impl sync::Hooks for Db where F: merkle::Family, E: Context + Spawner, C: crate::journal::authenticated::Backing< E, Item = Operation, Config = >::Config, > + sync::Journal> + 'static, I: IndexFactory + SnapshotBuild + Unordered>, H: Hasher, U: Update, S: Strategy, Operation: Codec, { async fn from_sync_result( context: E, config: ::Config, log: C, pinned_nodes: Option>, range: NonEmptyRange>, apply_batch_size: NonZeroU64, ) -> Result> { build_db::( context, config.merkle, log, config.translator, pinned_nodes, range, apply_batch_size, config.init_concurrency, config.init_buffer, config.init_cache, ) .await } async fn persist_sync_result(mut self) -> Result> { self.log = self.log.persist_boundary().await?; Ok(self) } async fn discard_local_state( context: E, config: &::Config, ) -> Result<(), qmdb::Error> { Ok(authenticated::discard_frontier::(context, &config.merkle).await?) } async fn local_pinned_nodes( context: E, config: &::Config, target: &sync::Target, journal: &C, ) -> Result>, qmdb::Error> { qmdb::sync::local_pinned_nodes::(context, &config.merkle, target, journal).await } }