//! [`Qmdb`] implementation for QMDB [`compact`](commonware_storage::qmdb::compact) databases. //! //! Compact databases retain only the current Merkle peaks. Batches support merkleization but no //! historical reads. Keyless batches add operations with `append` and immutable batches with //! `set` (see the `keyless` and `immutable` modules). use crate::stateful::db::qmdb::{Qmdb, Unmerkleized}; use commonware_cryptography::Hasher; use commonware_parallel::Strategy; use commonware_runtime::Handle; use commonware_storage::{ Context, merkle::{Family, Location}, qmdb::{ Error, compact::{Db, MerkleizedBatch, Operation, UnmerkleizedBatch}, sync, }, }; use std::sync::Arc; impl Unmerkleized> where Db: Qmdb>, F: Family, E: Context, O: Operation, H: Hasher, S: Strategy, { /// Sets the metadata committed by [`merkleize`](crate::stateful::db::Unmerkleized::merkleize). /// `None` clears it. pub fn with_metadata(mut self, metadata: impl Into>) -> Self { self.batch = self.batch.with_metadata(metadata); self } /// Sets the inactivity floor committed by /// [`merkleize`](crate::stateful::db::Unmerkleized::merkleize) (inherited from the database or /// parent batch when unset). pub fn with_inactivity_floor(mut self, floor: Location) -> Self { self.batch = self.batch.with_inactivity_floor(floor); self } } impl Qmdb for Db where Self: sync::Database, F: Family, E: Context, O: Operation, H: Hasher, S: Strategy, { type Batch = UnmerkleizedBatch; type MerkleizedBatch = MerkleizedBatch; fn new_batch(&self) -> Self::Batch { self.new_batch() } fn new_child(parent: &Arc) -> Self::Batch { parent.new_batch::() } async fn merkleize(&self, batch: Self::Batch) -> Result, Error> { batch.merkleize(self).await } async fn apply_batch(self, batch: Arc) -> Result> { self.apply_batch(batch).await } async fn start_sync(self) -> Result<(Self, Handle<()>), Error> { self.start_sync().await } } #[cfg(test)] mod tests { use super::*; use crate::stateful::db::{ DatabaseSet, InitError, ManagedDb, Merkleized as MerkleizedTrait, Shared, StateSyncDb, SyncEngineConfig, Unmerkleized as UnmerkleizedTrait, }; use commonware_cryptography::{Sha256, sha256::Digest}; use commonware_macros::select; use commonware_parallel::Sequential; use commonware_runtime::{ Clock as _, Metrics as _, Runner as _, Spawner as _, Supervisor as _, buffer::paged::CacheRef, deterministic, }; use commonware_storage::{ journal::{ authenticated::Config as MerkleConfig, contiguous::{fixed::Config as FixedJournalConfig, variable::Config as JournalConfig}, }, merkle::{mmb, mmr}, qmdb::{ProofExt as _, compact::Config, immutable, keyless, sync::source}, translator::TwoCap, }; use commonware_utils::{ NZU16, NZU64, NZUsize, channel::{mpsc, ring}, non_empty_range, sequence::U64, }; use futures::pin_mut; use std::time::Duration; type TestDb = Db; type TestBatch = UnmerkleizedBatch; /// Bounded initialization through [`ManagedDb::init`] reports target mismatches, restores an /// exact target that then serves and verifies, and fails once that target is pruned. fn bounded_initialization( codec_config: O::Cfg, mutate: impl Fn(TestBatch, u64) -> TestBatch, metadata: impl Fn(u64) -> O::Metadata, ) where F: Family, O: Operation, { deterministic::Runner::default().start(|context| async move { let cfg = Config { strategy: Sequential, witness: JournalConfig { partition: "compact-initialization-matrix".into(), items_per_section: NZU64!(1), compression: None, codec_config, page_cache: CacheRef::new(NZU16!(101), NZUsize!(11)), write_buffer: NZUsize!(1024), replay_buffer: NZUsize!(1024), }, }; let db = as ManagedDb<_>>::init(context.child("seed"), cfg.clone(), None) .await .unwrap(); let batch = mutate(mutate(mutate(db.new_batch(), 1), 2), 3) .with_metadata(Some(metadata(11))) .merkleize(&db) .await .unwrap(); let db = db.apply_batch(batch).await.unwrap(); let db = db.sync().await.unwrap(); let first = db.target(); assert_eq!(first.range.end(), Location::new(5)); let batch = mutate(db.new_batch(), 4) .with_metadata(Some(metadata(22))) .with_inactivity_floor(Location::new(1)) .merkleize(&db) .await .unwrap(); let db = db.apply_batch(batch).await.unwrap(); let db = db.sync().await.unwrap(); let latest = db.target(); drop(db); let mut wrong_root = latest.clone(); wrong_root.root = Sha256::fill(0xff); let result = as ManagedDb<_>>::init( context.child("wrong_root"), cfg.clone(), Some(wrong_root.clone()), ) .await; let Err(InitError::TargetMismatch { expected, recovered, }) = result else { panic!("a wrong root must be a target mismatch"); }; assert_eq!(expected, wrong_root); assert_eq!(recovered, latest); // A cap inside a batch selects the preceding commit, but is not an exact sync target. let mut between = first.clone(); between.range = non_empty_range!(between.range.start() + 1, between.range.end() + 1); let result = as ManagedDb<_>>::init( context.child("between"), cfg.clone(), Some(between.clone()), ) .await; let Err(InitError::TargetMismatch { expected, recovered, }) = result else { panic!("a size between commits must be a target mismatch"); }; assert_eq!(expected, between); assert_eq!(recovered, first); let db = as ManagedDb<_>>::init( context.child("exact"), cfg.clone(), Some(first.clone()), ) .await .unwrap(); assert_eq!(db.get_metadata(), Some(metadata(11))); assert_eq!(db.inactivity_floor(), Location::new(0)); let request = sync::Request::Boundary { size: first.range.end(), start: first.range.start(), }; let (response, _) = sync::Source::serve(&db, request).await.unwrap(); let sync::Response::Boundary { proof, op, pinned_nodes, } = response else { panic!("expected boundary response"); }; assert!(proof.verify_with_pinned_nodes::( first.range.start(), [&op], &pinned_nodes, &first.root )); drop(db); let db = TestDb::::init(context.child("reopen"), cfg.clone(), None) .await .unwrap(); assert_eq!(db.target(), first); let batch = mutate(db.new_batch(), 5) .with_metadata(Some(metadata(33))) .with_inactivity_floor(first.range.start()) .merkleize(&db) .await .unwrap(); let db = db.apply_batch(batch).await.unwrap(); let db = db.sync().await.unwrap(); let latest = db.target(); let db = db.prune(latest.range.end()).await.unwrap(); drop(db); assert!(matches!( as ManagedDb<_>>::init( context.child("pruned"), cfg.clone(), Some(first) ) .await, Err(InitError::Database(Error::NoRetainedCommit(_))) )); let db = as ManagedDb<_>>::init(context.child("latest"), cfg, Some(latest)) .await .unwrap(); assert_eq!(db.get_metadata(), Some(metadata(33))); db.destroy().await.unwrap(); }); } #[test] fn test_compact_bounded_initialization_keyless_fixed_mmr() { bounded_initialization::>( (), |batch, seed| batch.append(U64::new(seed)), U64::new, ); } #[test] fn test_compact_bounded_initialization_keyless_variable_mmr() { bounded_initialization::>>( ((..).into(), ()), |batch, seed| batch.append(seed.to_le_bytes().to_vec()), |seed| seed.to_le_bytes().to_vec(), ); } #[test] fn test_compact_bounded_initialization_immutable_fixed_mmr() { bounded_initialization::< mmr::Family, immutable::fixed::Operation, >( (), |batch, seed| { batch.set( Sha256::hash(&[&seed.to_be_bytes()]), Sha256::hash(&[&seed.to_le_bytes()]), ) }, |seed| Sha256::hash(&[&seed.to_le_bytes()]), ); } #[test] fn test_compact_bounded_initialization_immutable_variable_mmr() { bounded_initialization::< mmr::Family, immutable::variable::Operation>, >( ((), ((..).into(), ())), |batch, seed| { batch.set( Sha256::hash(&[&seed.to_be_bytes()]), seed.to_le_bytes().to_vec(), ) }, |seed| seed.to_le_bytes().to_vec(), ); } #[test] fn test_compact_bounded_initialization_keyless_fixed_mmb() { bounded_initialization::>( (), |batch, seed| batch.append(U64::new(seed)), U64::new, ); } #[test] fn test_compact_bounded_initialization_keyless_variable_mmb() { bounded_initialization::>>( ((..).into(), ()), |batch, seed| batch.append(seed.to_le_bytes().to_vec()), |seed| seed.to_le_bytes().to_vec(), ); } #[test] fn test_compact_bounded_initialization_immutable_fixed_mmb() { bounded_initialization::< mmb::Family, immutable::fixed::Operation, >( (), |batch, seed| { batch.set( Sha256::hash(&[&seed.to_be_bytes()]), Sha256::hash(&[&seed.to_le_bytes()]), ) }, |seed| Sha256::hash(&[&seed.to_le_bytes()]), ); } #[test] fn test_compact_bounded_initialization_immutable_variable_mmb() { bounded_initialization::< mmb::Family, immutable::variable::Operation>, >( ((), ((..).into(), ())), |batch, seed| { batch.set( Sha256::hash(&[&seed.to_be_bytes()]), seed.to_le_bytes().to_vec(), ) }, |seed| seed.to_le_bytes().to_vec(), ); } type Target = sync::Target; type AdapterDb = TestDb; type AdapterBatch = Unmerkleized>; type KeylessOp = keyless::fixed::Operation; type ImmutableOp = immutable::fixed::Operation; type FullKeylessDb = keyless::fixed::Db; type FullImmutableDb = immutable::fixed::Db< mmr::Family, deterministic::Context, Digest, Digest, Sha256, TwoCap, Sequential, >; /// An operation type the shared adapter tests run over. trait AdapterOperation: Operation { /// The full db that serves the same operations. type Full: sync::Source + 'static; /// Add the mutation derived from `seed` to a storage batch. fn mutate(batch: TestBatch, seed: u64) -> TestBatch; /// The commit metadata for `seed`. fn commit_metadata(seed: u64) -> Self::Metadata; /// A full db with one synced batch per seed, and the target after each. async fn full_source( context: deterministic::Context, seeds: &[u64], ) -> (Arc, Vec); } fn full_merkle_config() -> MerkleConfig { MerkleConfig { partition: "stateful-full-metadata".into(), replay_buffer: NZUsize!(1024), strategy: Sequential, resident_height: Default::default(), } } fn full_log_config(page_cache: CacheRef) -> FixedJournalConfig { FixedJournalConfig { partition: "stateful-full-log".into(), items_per_blob: NZU64!(7), page_cache, write_buffer: NZUsize!(1024), replay_buffer: NZUsize!(1024), } } impl AdapterOperation for KeylessOp { type Full = FullKeylessDb; fn mutate(batch: TestBatch, seed: u64) -> TestBatch { batch.append(U64::new(seed)) } fn commit_metadata(seed: u64) -> U64 { U64::new(seed + 1000) } async fn full_source( context: deterministic::Context, seeds: &[u64], ) -> (Arc, Vec) { let page_cache = CacheRef::new(NZU16!(101), NZUsize!(11)); let config = keyless::fixed::Config { merkle: full_merkle_config(), log: full_log_config(page_cache), init_concurrency: NZUsize!(2), }; let mut source = FullKeylessDb::init(context, config, None).await.unwrap(); let mut targets = Vec::new(); for &seed in seeds { let batch = source .new_batch() .append(U64::new(seed)) .with_metadata(Some(Self::commit_metadata(seed))) .merkleize(&source) .await .unwrap(); source = source.apply_batch(batch).await.unwrap(); source = source.sync().await.unwrap(); targets.push(Target { root: source.root(), range: non_empty_range!(source.bounds().end - 1, source.bounds().end), }); } (Arc::new(source), targets) } } fn immutable_key(seed: u64) -> Digest { Sha256::hash(&[&seed.to_be_bytes()]) } fn immutable_value(seed: u64) -> Digest { Sha256::hash(&[&seed.to_le_bytes()]) } impl AdapterOperation for ImmutableOp { type Full = FullImmutableDb; fn mutate(batch: TestBatch, seed: u64) -> TestBatch { batch.set(immutable_key(seed), immutable_value(seed)) } fn commit_metadata(seed: u64) -> Digest { immutable_value(seed + 1000) } async fn full_source( context: deterministic::Context, seeds: &[u64], ) -> (Arc, Vec) { let page_cache = CacheRef::new(NZU16!(101), NZUsize!(11)); let config = immutable::fixed::Config { merkle: full_merkle_config(), log: full_log_config(page_cache), translator: TwoCap, init_buffer: NZUsize!(1 << 21), init_concurrency: NZUsize!(2), }; let mut source = FullImmutableDb::init(context, config, None).await.unwrap(); let mut targets = Vec::new(); for &seed in seeds { let batch = source .new_batch() .set(immutable_key(seed), immutable_value(seed)) .with_metadata(Some(Self::commit_metadata(seed))) .merkleize(&source) .await .unwrap(); source = source.apply_batch(batch).await.unwrap(); source = source.sync().await.unwrap(); targets.push(Target { root: source.root(), range: non_empty_range!(source.bounds().end - 1, source.bounds().end), }); } (Arc::new(source), targets) } } /// Add the mutation derived from `seed` to an adapter batch. fn mutate_managed( mut batch: AdapterBatch, seed: u64, ) -> AdapterBatch { batch.batch = O::mutate(batch.batch, seed); batch } fn compact_config(suffix: &str) -> Config<(), Sequential> { Config { strategy: Sequential, witness: JournalConfig { partition: format!("stateful-compact-{suffix}-witness"), items_per_section: NZU64!(64), compression: None, codec_config: (), page_cache: CacheRef::new(NZU16!(101), NZUsize!(11)), write_buffer: NZUsize!(1024), replay_buffer: NZUsize!(1024), }, } } const fn sync_config() -> SyncEngineConfig { SyncEngineConfig { fetch_batch_size: NZU64!(1), apply_batch_size: NZU64!(1), max_outstanding_requests: NZUsize!(1), } } /// A compact db with one synced batch per seed, and the target after each. async fn compact_source( context: deterministic::Context, seeds: &[u64], ) -> (Arc>, Vec) { let config = compact_config("source"); let mut source = AdapterDb::::init(context, config, None).await.unwrap(); let mut targets = Vec::new(); for &seed in seeds { let batch = O::mutate(source.new_batch(), seed) .with_metadata(Some(O::commit_metadata(seed))) .merkleize(&source) .await .unwrap(); source = source.apply_batch(batch).await.unwrap(); source = source.sync().await.unwrap(); targets.push(source.target()); } (Arc::new(source), targets) } /// Serves `source`, except that a request at `stale_target`'s size signals /// `stale_request_tx` and never completes. struct SupersedingSource { source: Arc, stale_target: Target, stale_request_tx: mpsc::Sender<()>, } impl Clone for SupersedingSource { fn clone(&self) -> Self { Self { source: Arc::clone(&self.source), stale_target: self.stale_target.clone(), stale_request_tx: self.stale_request_tx.clone(), } } } impl sync::Source for SupersedingSource where S: sync::Source + 'static, { type Family = mmr::Family; type Digest = Digest; type Op = S::Op; type Error = as sync::Source>::Error; async fn serve(&self, request: sync::Request) -> source::Result { if request.size() == self.stale_target.range.end() { let _ = self.stale_request_tx.send(()).await; return futures::future::pending().await; } self.source.serve(request).await } } #[test] fn trait_impls_compile() { fn assert_state_sync_db>>() {} assert_state_sync_db::>(); assert_state_sync_db::>>>(); assert_state_sync_db::>(); assert_state_sync_db::< AdapterDb>>, >(); } fn managed_db_apply_and_finalize_persists_batches() { deterministic::Runner::default().start(|context| async move { let config = compact_config("managed-db"); let db = AdapterDb::::init(context.child("db"), config.clone(), None) .await .unwrap(); let db = Shared::new("test", db); let batch = mutate_managed::(db.new_batch_for_test::<_>().await, 7) .with_inactivity_floor(Location::new(1)) .with_metadata(O::commit_metadata(7)); let merkleized = UnmerkleizedTrait::merkleize(batch).await.unwrap(); let expected_root = merkleized.root(); { let (slot, database) = db.write().await; let database = as ManagedDb<_>>::apply(database, merkleized) .await .unwrap(); let (database, sync) = as ManagedDb<_>>::finalize(database) .await .unwrap(); slot.put(database); sync.await.expect("database sync failed"); } let guard = db.read().await; assert_eq!(guard.root(), expected_root); assert_eq!(guard.get_metadata(), Some(O::commit_metadata(7))); let target = as ManagedDb<_>>::sync_target(&guard); assert_eq!(target.root, expected_root); assert_eq!(target.range.end(), Location::new(3)); drop(guard); drop(db); let reopened = as ManagedDb<_>>::init(context.child("reopen"), config, None) .await .unwrap(); assert_eq!( as ManagedDb<_>>::sync_target(&reopened), target ); assert_eq!(reopened.get_metadata(), Some(O::commit_metadata(7))); }); } fn managed_db_apply_retains_each_bounded_initialization_target() { deterministic::Runner::default().start(|context| async move { let config = compact_config("apply-checkpoints"); let db = AdapterDb::::init(context.child("db"), config.clone(), None) .await .unwrap(); let db = Shared::new("test", db); let first = mutate_managed::(db.new_batch_for_test::<_>().await, 1) .with_metadata(O::commit_metadata(1)); let first = UnmerkleizedTrait::merkleize(first).await.unwrap(); let first_target = Target { root: first.root(), range: non_empty_range!(first.bounds().tip.size - 1, first.bounds().tip.size), }; let (slot, database) = db.write().await; let database = as ManagedDb<_>>::apply(database, first) .await .unwrap(); slot.put(database); let second = mutate_managed::(db.new_batch_for_test::<_>().await, 2) .with_metadata(O::commit_metadata(2)); let second = UnmerkleizedTrait::merkleize(second).await.unwrap(); let (slot, database) = db.write().await; let database = as ManagedDb<_>>::apply(database, second) .await .unwrap(); let (database, sync) = as ManagedDb<_>>::finalize(database) .await .unwrap(); sync.await.expect("database sync failed"); slot.put(database); drop(db); let database = as ManagedDb<_>>::init( context.child("reopen"), config, Some(first_target.clone()), ) .await .unwrap(); assert_eq!( as ManagedDb<_>>::sync_target(&database), first_target, ); }); } fn managed_db_matches_sync_target_rejects_wrong_size() { deterministic::Runner::default().start(|context| async move { let config = compact_config("matches-sync-target"); let db = AdapterDb::::init(context.child("db"), config, None) .await .unwrap(); let db = Shared::new("test", db); let batch = mutate_managed::(db.new_batch_for_test::<_>().await, 7) .with_inactivity_floor(Location::new(1)) .with_metadata(O::commit_metadata(7)); let merkleized = UnmerkleizedTrait::merkleize(batch).await.unwrap(); let valid_target = Target { root: merkleized.root(), range: non_empty_range!( merkleized.bounds().tip.size - 1, merkleized.bounds().tip.size ), }; assert!( as ManagedDb<_>>::matches_sync_target( &merkleized, &valid_target, )); let wrong_size = Target { root: merkleized.root(), range: non_empty_range!( merkleized.bounds().tip.size - 2, merkleized.bounds().tip.size - 1 ), }; assert!(! as ManagedDb<_>>::matches_sync_target( &merkleized, &wrong_size, )); }); } fn database_set_bounded_initialization_persists_aligned_target() { deterministic::Runner::default().start(|context| async move { let config = compact_config("aligned-bounded-init"); let db = AdapterDb::::init(context.child("db"), config.clone(), None) .await .unwrap(); let db = Shared::new("test", db); let batch = mutate_managed::(db.new_batch_for_test::<_>().await, 1) .with_metadata(O::commit_metadata(1)); let batch = UnmerkleizedTrait::merkleize(batch).await.unwrap(); DatabaseSet::apply(&db, batch).await; let target = DatabaseSet::committed_targets(&db).await; assert!(DatabaseSet::finalize(&db).await.durable().await); drop(db); let db = > as DatabaseSet<_>>::init( context.child("aligned_cap"), config.clone(), Some(target.clone()), ) .await; drop(db); let database = AdapterDb::::init(context.child("reopen"), config, None) .await .unwrap(); assert_eq!( as ManagedDb<_>>::sync_target(&database), target ); }); } fn state_sync_fetches_compact_state() { deterministic::Runner::default().start(|context| async move { let (source, targets) = compact_source::(context.child("source"), &[7]).await; let (_update_tx, update_rx) = ring::channel(NZUsize!(1)); let synced = as StateSyncDb<_, _>>::sync_db( context.child("target"), compact_config("target"), source, targets[0].clone(), update_rx, None, None, sync_config(), ) .await .unwrap(); assert_eq!(synced.target(), targets[0]); assert_eq!(synced.get_metadata(), Some(O::commit_metadata(7))); }); } /// A queued update is adopted before the initial target is reached, so only the update is /// reported. fn state_sync_adopts_queued_target_before_reaching_initial() { deterministic::Runner::default().start(|context| async move { let (source, targets) = O::full_source(context.child("source"), &[7, 8]).await; let (update_tx, update_rx) = ring::channel(NZUsize!(1)); assert!(update_tx.send_lossy(targets[1].clone())); let (reached_tx, mut reached_rx) = ring::channel(NZUsize!(2)); let synced = as StateSyncDb<_, _>>::sync_db( context.child("target"), compact_config("target"), source, targets[0].clone(), update_rx, None, Some(reached_tx), sync_config(), ) .await .unwrap(); assert_eq!(reached_rx.recv().await, Some(targets[1].clone())); assert_eq!(reached_rx.recv().await, None); assert_eq!(synced.target(), targets[1]); assert_eq!(synced.get_metadata(), Some(O::commit_metadata(8))); }); } async fn state_sync_reports_compact_progress( context: deterministic::Context, source: Arc, target: Target, ) where O: AdapterOperation, S: sync::Source + 'static, { // A larger target the source never serves. Its sync attempt // hangs so the test can observe the gauges while they diverge. let unservable_target = Target { root: Sha256::hash(&[&[0xFF]]), range: non_empty_range!(target.range.end(), target.range.end() + 1), }; let (stale_request_tx, mut stale_request_rx) = mpsc::channel(1); let superseding_source = SupersedingSource { source, stale_target: unservable_target.clone(), stale_request_tx, }; let (update_tx, update_rx) = ring::channel(NZUsize!(1)); let (_finish_tx, finish_rx) = mpsc::channel(1); let (reached_tx, mut reached_rx) = ring::channel(NZUsize!(1)); let client_context = context.child("client"); let client_config = compact_config("client"); let sync = as StateSyncDb<_, _>>::sync_db( client_context, client_config, superseding_source, target.clone(), update_rx, Some(finish_rx), Some(reached_tx), sync_config(), ); pin_mut!(sync); select! { _ = sync.as_mut() => panic!("sync completed before explicit finish signal"), reached = reached_rx.recv() => assert_eq!(reached, Some(target.clone())), } let synced_size = *target.range.end(); let encoded = context.encode(); assert!( encoded.contains(&format!("\nclient_sync_target_size {synced_size}")), "missing compact sync target gauge: {encoded}" ); assert!( encoded.contains(&format!("\nclient_sync_size {synced_size}")), "missing compact sync progress gauge: {encoded}" ); // Supersede with the unservable target and wait for its fetch to // start. The target gauge advances while the synced gauge still // reports the previously reached target. assert!(update_tx.send_lossy(unservable_target.clone())); select! { _ = sync.as_mut() => panic!("sync completed with an unservable target"), request = stale_request_rx.recv() => assert_eq!(request, Some(())), } let target_size_val = *unservable_target.range.end(); let encoded = context.encode(); assert!( encoded.contains(&format!("\nclient_sync_target_size {target_size_val}")), "target gauge should advance to the superseding target: {encoded}" ); assert!( encoded.contains(&format!("\nclient_sync_size {synced_size}")), "synced gauge should still report the reached target: {encoded}" ); } async fn state_sync_supersedes_in_flight_stale_compact_target( context: deterministic::Context, source: Arc, targets: Vec, latest_metadata: O::Metadata, ) where O: AdapterOperation, S: sync::Source + 'static, { let [stale_target, latest_target] = <[Target; 2]>::try_from(targets).unwrap(); let (stale_request_tx, mut stale_request_rx) = mpsc::channel(1); let superseding_source = SupersedingSource { source, stale_target: stale_target.clone(), stale_request_tx, }; let (update_tx, update_rx) = ring::channel(NZUsize!(1)); let sync_handle = context.child("sync").spawn(move |context| async move { as StateSyncDb<_, _>>::sync_db( context.child("target"), compact_config("supersede-target"), superseding_source, stale_target, update_rx, None, None, sync_config(), ) .await }); context .timeout(Duration::from_secs(1), async move { stale_request_rx.recv().await.unwrap(); }) .await .expect("sync should request the stale target first"); assert!(update_tx.send_lossy(latest_target.clone())); let synced = context .timeout(Duration::from_secs(1), sync_handle) .await .expect("sync should switch to the latest target") .expect("spawned sync task should complete") .unwrap(); assert_eq!(synced.target(), latest_target); assert_eq!(synced.get_metadata(), Some(latest_metadata)); } fn managed_db_initializes_multiple_commit_ranges() { deterministic::Runner::default().start(|context| async move { let config = compact_config("bounded-init"); let mut db = AdapterDb::::init(context.child("db"), config.clone(), None) .await .unwrap(); // Commit three ranges so reopening at the first target spans multiple commits. let mut targets = Vec::new(); for seed in [1u64, 2, 3] { let batch = O::mutate(db.new_batch(), seed) .with_metadata(Some(O::commit_metadata(seed))) .merkleize(&db) .await .unwrap(); db = db.apply_batch(batch).await.unwrap(); db = db.sync().await.unwrap(); targets.push( as ManagedDb<_>>::sync_target(&db)); } assert_ne!(targets[2], targets[0]); drop(db); let db = as ManagedDb<_>>::init( context.child("cap"), config, Some(targets[0].clone()), ) .await .unwrap(); assert_eq!( as ManagedDb<_>>::sync_target(&db), targets[0]); assert_eq!(db.get_metadata(), Some(O::commit_metadata(1))); }); } fn managed_db_prune_bounds_bounded_initialization_history() { deterministic::Runner::default().start(|context| async move { // One witness entry per section so pruning takes effect at entry granularity. let mut config = compact_config("prune"); config.witness.items_per_section = NZU64!(1); let mut db = AdapterDb::::init(context.child("db"), config.clone(), None) .await .unwrap(); // Commit three ranges, recording each target. let mut targets = Vec::new(); for seed in [1u64, 2, 3] { let batch = O::mutate(db.new_batch(), seed) .with_metadata(Some(O::commit_metadata(seed))) .merkleize(&db) .await .unwrap(); db = db.apply_batch(batch).await.unwrap(); db = db.sync().await.unwrap(); targets.push( as ManagedDb<_>>::sync_target(&db)); } assert_ne!(targets[0], targets[1]); // Pruning at the second target retains it but excludes the first. let db = as ManagedDb<_>>::prune(db, &targets[1]) .await .unwrap(); drop(db); let db = as ManagedDb<_>>::init( context.child("cap"), config.clone(), Some(targets[1].clone()), ) .await .unwrap(); assert_eq!( as ManagedDb<_>>::sync_target(&db), targets[1]); drop(db); assert!(matches!( as ManagedDb<_>>::init( context.child("pruned_cap"), config, Some(targets[0].clone()) ) .await, Err(InitError::Database(Error::NoRetainedCommit(_))) )); }); } fn unset_inactivity_floor_inherits_parent_floor() { deterministic::Runner::default().start(|context| async move { let config = compact_config("inherit-parent-floor"); let db = AdapterDb::::init(context.child("db"), config, None) .await .unwrap(); let db = Shared::new("test", db); let parent = mutate_managed::(db.new_batch_for_test::<_>().await, 7) .with_inactivity_floor(Location::new(1)); let parent = UnmerkleizedTrait::merkleize(parent).await.unwrap(); let child = mutate_managed::(MerkleizedTrait::new_batch(&parent), 8); let child = UnmerkleizedTrait::merkleize(child).await.unwrap(); assert_eq!(child.bounds().inactivity_floor, Location::new(1)); }); } fn unset_inactivity_floor_inherits_applied_floor() { deterministic::Runner::default().start(|context| async move { let config = compact_config("inherit-applied-floor"); let db = AdapterDb::::init(context.child("db"), config, None) .await .unwrap(); let db = Shared::new("test", db); let first = mutate_managed::(db.new_batch_for_test::<_>().await, 7) .with_inactivity_floor(Location::new(1)); let first = UnmerkleizedTrait::merkleize(first).await.unwrap(); { let (slot, database) = db.write().await; let database = as ManagedDb<_>>::apply(database, first) .await .unwrap(); slot.put(database); } let next = mutate_managed::(db.new_batch_for_test::<_>().await, 9); let next = UnmerkleizedTrait::merkleize(next).await.unwrap(); assert_eq!(next.bounds().inactivity_floor, Location::new(1)); let (slot, database) = db.write().await; let database = as ManagedDb<_>>::apply(database, next) .await .unwrap(); assert_eq!(database.inactivity_floor(), Location::new(1)); slot.put(database); }); } macro_rules! adapter_tests { ($module:ident, $operation:ty) => { mod $module { use super::*; #[test] fn unset_inactivity_floor_inherits_parent_floor() { super::unset_inactivity_floor_inherits_parent_floor::<$operation>(); } #[test] fn unset_inactivity_floor_inherits_applied_floor() { super::unset_inactivity_floor_inherits_applied_floor::<$operation>(); } #[test] fn managed_db_apply_and_finalize_persists_batches() { super::managed_db_apply_and_finalize_persists_batches::<$operation>(); } #[test] fn managed_db_apply_retains_each_bounded_initialization_target() { super::managed_db_apply_retains_each_bounded_initialization_target::< $operation, >(); } #[test] fn managed_db_matches_sync_target_rejects_wrong_size() { super::managed_db_matches_sync_target_rejects_wrong_size::<$operation>(); } #[test] fn database_set_bounded_initialization_persists_aligned_target() { super::database_set_bounded_initialization_persists_aligned_target::< $operation, >(); } #[test] fn state_sync_fetches_compact_state() { super::state_sync_fetches_compact_state::<$operation>(); } #[test] fn state_sync_adopts_queued_target_before_reaching_initial() { super::state_sync_adopts_queued_target_before_reaching_initial::<$operation>(); } #[test] fn state_sync_reports_compact_progress_from_compact_source() { deterministic::Runner::default().start(|context| async move { let (source, targets) = compact_source::<$operation>(context.child("source"), &[7]).await; super::state_sync_reports_compact_progress::<$operation, _>( context, source, targets[0].clone(), ) .await; }); } #[test] fn state_sync_reports_compact_progress_from_full_source() { deterministic::Runner::default().start(|context| async move { let (source, targets) = <$operation>::full_source(context.child("source"), &[7]).await; super::state_sync_reports_compact_progress::<$operation, _>( context, source, targets[0].clone(), ) .await; }); } #[test] fn state_sync_supersedes_in_flight_stale_target_from_compact_source() { deterministic::Runner::default().start(|context| async move { let (source, targets) = compact_source::<$operation>(context.child("source"), &[7, 8]).await; super::state_sync_supersedes_in_flight_stale_compact_target::< $operation, _, >(context, source, targets, <$operation>::commit_metadata(8)) .await; }); } #[test] fn state_sync_supersedes_in_flight_stale_target_from_full_source() { deterministic::Runner::default().start(|context| async move { let (source, targets) = <$operation>::full_source(context.child("source"), &[7, 8]).await; super::state_sync_supersedes_in_flight_stale_compact_target::< $operation, _, >(context, source, targets, <$operation>::commit_metadata(8)) .await; }); } #[test] fn managed_db_initializes_multiple_commit_ranges() { super::managed_db_initializes_multiple_commit_ranges::<$operation>(); } #[test] fn managed_db_prune_bounds_bounded_initialization_history() { super::managed_db_prune_bounds_bounded_initialization_history::<$operation>(); } } }; } adapter_tests!(keyless_fixed, KeylessOp); adapter_tests!(immutable_fixed, ImmutableOp); }