//! An authenticated database that provides succinct proofs of _any_ value ever associated //! with a key, maintains a next-key ordering for each active key, and allows values to have //! variable sizes. //! //! _If the values you wish to store all have the same size, use [crate::qmdb::any::ordered::fixed] //! instead for better performance._ use crate::{ index::ordered::Index, journal::contiguous::variable::Journal, mmr::Location, qmdb::{ any::{init_variable, ordered, value::VariableEncoding, VariableConfig, VariableValue}, operation::Key, Error, }, translator::Translator, }; use commonware_codec::{Codec, Read}; use commonware_cryptography::Hasher; use commonware_runtime::{Clock, Metrics, Storage}; pub type Update = ordered::Update>; pub type Operation = ordered::Operation>; /// A key-value QMDB based on an authenticated log of operations, supporting authentication of any /// value ever associated with a key. pub type Db = super::Db>, Index, H, Update>; impl Db where Operation: Codec, { /// Returns a [Db] QMDB initialized from `cfg`. Any uncommitted log operations will be /// discarded and the state of the db will be as of the last committed operation. pub async fn init( context: E, cfg: VariableConfig as Read>::Cfg>, ) -> Result { Self::init_with_callback(context, cfg, None, |_, _| {}).await } /// Initialize the DB, invoking `callback` for each operation processed during recovery. /// /// If `known_inactivity_floor` is provided and is less than the log's actual inactivity floor, /// `callback` is invoked with `(false, None)` for each location in the gap. Then, as the /// snapshot is built from the log, `callback` is invoked for each operation with its activity /// status and previous location (if any). pub(crate) async fn init_with_callback( context: E, cfg: VariableConfig as Read>::Cfg>, known_inactivity_floor: Option, callback: impl FnMut(bool, Option), ) -> Result { init_variable(context, cfg, known_inactivity_floor, callback, |ctx, t| { Index::new(ctx, t) }) .await } } /// Partitioned index variants that divide the key space into `2^(P*8)` partitions. /// /// See [partitioned::Db] for the generic type, or use the convenience aliases: /// - [partitioned::p256::Db] for 256 partitions (P=1) /// - [partitioned::p64k::Db] for 65,536 partitions (P=2) pub mod partitioned { pub use super::{Operation, Update}; use crate::{ index::partitioned::ordered::Index, journal::contiguous::variable::Journal, mmr::Location, qmdb::{ any::{init_variable, VariableConfig, VariableValue}, operation::Key, Error, }, translator::Translator, }; use commonware_codec::{Codec, Read}; use commonware_cryptography::Hasher; use commonware_runtime::{Clock, Metrics, Storage}; /// An ordered key-value QMDB with a partitioned snapshot index and variable-size values. /// /// This is the partitioned variant of [super::Db]. The const generic `P` specifies /// the number of prefix bytes used for partitioning: /// - `P = 1`: 256 partitions /// - `P = 2`: 65,536 partitions /// /// Use partitioned indices when you have a large number of keys (>> 2^(P*8)) and memory /// efficiency is important. Keys should be uniformly distributed across the prefix space. pub type Db = crate::qmdb::any::ordered::Db< E, Journal>, Index, H, Update, >; impl< E: Storage + Clock + Metrics, K: Key, V: VariableValue, H: Hasher, T: Translator, const P: usize, > Db where Operation: Codec, { /// Returns a [Db] QMDB initialized from `cfg`. Uncommitted log operations will be /// discarded and the state of the db will be as of the last committed operation. pub async fn init( context: E, cfg: VariableConfig as Read>::Cfg>, ) -> Result { Self::init_with_callback(context, cfg, None, |_, _| {}).await } /// Initialize the DB, invoking `callback` for each operation processed during recovery. /// /// If `known_inactivity_floor` is provided and is less than the log's actual inactivity floor, /// `callback` is invoked with `(false, None)` for each location in the gap. Then, as the /// snapshot is built from the log, `callback` is invoked for each operation with its activity /// status and previous location (if any). pub(crate) async fn init_with_callback( context: E, cfg: VariableConfig as Read>::Cfg>, known_inactivity_floor: Option, callback: impl FnMut(bool, Option), ) -> Result { init_variable(context, cfg, known_inactivity_floor, callback, |ctx, t| { Index::new(ctx, t) }) .await } } /// Convenience type aliases for 256 partitions (P=1). pub mod p256 { /// Variable-value DB with 256 partitions. pub type Db = super::Db; } /// Convenience type aliases for 65,536 partitions (P=2). pub mod p64k { /// Variable-value DB with 65,536 partitions. pub type Db = super::Db; } } #[cfg(test)] pub(crate) mod test { use super::*; use crate::{ kv::tests::{assert_gettable, assert_send}, mmr::{Location, Position}, qmdb::{ any::{ ordered::test::{ test_ordered_any_db_basic, test_ordered_any_db_empty, test_ordered_any_update_collision_edge_case, }, test::variable_db_config, }, store::tests::{assert_log_store, assert_merkleized_store, assert_prunable_store}, }, translator::TwoCap, }; use commonware_cryptography::{sha256::Digest, Sha256}; use commonware_macros::test_traced; use commonware_math::algebra::Random; use commonware_runtime::{ buffer::paged::CacheRef, deterministic::{self, Context}, BufferPooler, Runner as _, }; use commonware_utils::{sequence::FixedBytes, test_rng_seeded, NZUsize, NZU16, NZU64}; use rand::RngCore; // Janky page & cache sizes to exercise boundary conditions. const PAGE_SIZE: u16 = 103; const PAGE_CACHE_SIZE: usize = 13; pub(crate) type VarConfig = VariableConfig, ()))>; /// Type alias for the concrete [Db] type used in these unit tests. pub(crate) type AnyTest = Db, Sha256, TwoCap>; pub(crate) fn create_test_config(seed: u64, pooler: &impl BufferPooler) -> VarConfig { VariableConfig { mmr_journal_partition: format!("mmr-journal-{seed}"), mmr_metadata_partition: format!("mmr-metadata-{seed}"), mmr_items_per_blob: NZU64!(12), // intentionally small and janky size mmr_write_buffer: NZUsize!(64), log_partition: format!("log-journal-{seed}"), log_items_per_blob: NZU64!(14), // intentionally small and janky size log_write_buffer: NZUsize!(64), log_compression: None, log_codec_config: ((), ((0..=10000).into(), ())), translator: TwoCap, thread_pool: None, page_cache: CacheRef::from_pooler(pooler, NZU16!(PAGE_SIZE), NZUsize!(PAGE_CACHE_SIZE)), } } /// Create a test database with unique partition names pub(crate) async fn create_test_db(mut context: Context) -> AnyTest { let seed = context.next_u64(); let config = create_test_config(seed, &context); AnyTest::init(context, config).await.unwrap() } /// Deterministic byte vector generator for variable-value tests. fn to_bytes(i: u64) -> Vec { let len = ((i % 13) + 7) as usize; vec![(i % 255) as u8; len] } /// Create n random operations using the default seed (0). Some portion of /// the updates are deletes. create_test_ops(n) is a prefix of /// create_test_ops(n') for n < n'. pub(crate) fn create_test_ops(n: usize) -> Vec>> { create_test_ops_seeded(n, 0) } /// Create n random operations using a specific seed. Use different seeds /// when you need non-overlapping keys in the same test. pub(crate) fn create_test_ops_seeded(n: usize, seed: u64) -> Vec>> { let mut rng = test_rng_seeded(seed); let mut prev_key = Digest::random(&mut rng); let mut ops = Vec::new(); for i in 0..n { if i % 10 == 0 && i > 0 { ops.push(Operation::Delete(prev_key)); } else { let key = Digest::random(&mut rng); let next_key = Digest::random(&mut rng); let value = to_bytes(rng.next_u64()); ops.push(Operation::Update(ordered::Update { key, value, next_key, })); prev_key = key; } } ops } /// Applies the given operations to the database. pub(crate) async fn apply_ops(db: &mut AnyTest, ops: Vec>>) { let mut batch = db.new_batch(); for op in ops { match op { Operation::Update(data) => { batch.write(data.key, Some(data.value)); } Operation::Delete(key) => { batch.write(key, None); } Operation::CommitFloor(_, _) => { // CommitFloor consumes self - not supported in this helper. // Test data from create_test_ops never includes CommitFloor. panic!("CommitFloor not supported in apply_ops"); } } } let finalized = batch.merkleize(None).await.unwrap().finalize(); db.apply_batch(finalized).await.unwrap(); } // Tests using FixedBytes<4> keys (for edge cases that require specific key patterns) /// Type alias for a variable db with FixedBytes<4> keys. type VariableDb = Db, Digest, Sha256, TwoCap>; /// Return a variable db with FixedBytes<4> keys. async fn open_variable_db(context: Context) -> VariableDb { let cfg = variable_db_config("fixed-bytes-var-partition", &context); VariableDb::init(context, cfg).await.unwrap() } #[test_traced("WARN")] fn test_ordered_any_variable_db_empty() { let executor = deterministic::Runner::default(); executor.start(|context| async move { let db = open_variable_db(context.with_label("initial")).await; test_ordered_any_db_empty(context, db, |ctx| Box::pin(open_variable_db(ctx))).await; }); } #[test_traced("WARN")] fn test_ordered_any_variable_db_basic() { let executor = deterministic::Runner::default(); executor.start(|context| async move { let db = open_variable_db(context.with_label("initial")).await; test_ordered_any_db_basic(context, db, |ctx| Box::pin(open_variable_db(ctx))).await; }); } #[test_traced("WARN")] fn test_ordered_any_update_collision_edge_case_variable() { let executor = deterministic::Runner::default(); executor.start(|context| async move { let db = open_variable_db(context.clone()).await; test_ordered_any_update_collision_edge_case(db).await; }); } /// Builds a db with two colliding keys, and creates a new one between them using a batch /// update. #[test_traced("WARN")] fn test_ordered_any_update_batch_create_between_collisions() { let executor = deterministic::Runner::default(); executor.start(|context| async move { let mut db = open_variable_db(context.clone()).await; // This DB uses a TwoCap so we use equivalent two byte prefixes for each key to ensure // collisions. let key1 = FixedBytes::from([0xFFu8, 0xFFu8, 5u8, 5u8]); let key2 = FixedBytes::from([0xFFu8, 0xFFu8, 6u8, 6u8]); let key3 = FixedBytes::from([0xFFu8, 0xFFu8, 7u8, 0u8]); let val = Sha256::fill(1u8); let finalized = { let mut batch = db.new_batch(); batch.write(key1.clone(), Some(val)); batch.write(key3.clone(), Some(val)); batch.merkleize(None).await.unwrap().finalize() }; db.apply_batch(finalized).await.unwrap(); assert_eq!(db.get(&key1).await.unwrap().unwrap(), val); assert!(db.get(&key2).await.unwrap().is_none()); assert_eq!(db.get(&key3).await.unwrap().unwrap(), val); // Batch-insert the middle key. let finalized = { let mut batch = db.new_batch(); batch.write(key2.clone(), Some(val)); batch.merkleize(None).await.unwrap().finalize() }; db.apply_batch(finalized).await.unwrap(); assert_eq!(db.get(&key1).await.unwrap().unwrap(), val); assert_eq!(db.get(&key2).await.unwrap().unwrap(), val); assert_eq!(db.get(&key3).await.unwrap().unwrap(), val); let span1 = db.get_span(&key1).await.unwrap().unwrap(); assert_eq!(span1.1.next_key, key2); let span2 = db.get_span(&key2).await.unwrap().unwrap(); assert_eq!(span2.1.next_key, key3); let span3 = db.get_span(&key3).await.unwrap().unwrap(); assert_eq!(span3.1.next_key, key1); db.destroy().await.unwrap(); }); } /// Batch create/delete cases where the deleted key is the previous key of a newly created key, /// and vice-versa. #[test_traced("WARN")] fn test_ordered_any_batch_create_delete_prev_links() { let executor = deterministic::Runner::default(); executor.start(|context| async move { let key1 = FixedBytes::from([0x10u8, 0x00, 0x00, 0x00]); let key2 = FixedBytes::from([0x20u8, 0x00, 0x00, 0x00]); let key3 = FixedBytes::from([0x30u8, 0x00, 0x00, 0x00]); let val1 = Sha256::fill(1u8); let val2 = Sha256::fill(2u8); let val3 = Sha256::fill(3u8); // Delete the previous key of a newly created key. let mut db = open_variable_db(context.with_label("first")).await; let finalized = { let mut batch = db.new_batch(); batch.write(key1.clone(), Some(val1)); batch.write(key3.clone(), Some(val3)); batch.merkleize(None).await.unwrap().finalize() }; db.apply_batch(finalized).await.unwrap(); let finalized = { let mut batch = db.new_batch(); batch.write(key1.clone(), None); batch.write(key2.clone(), Some(val2)); batch.merkleize(None).await.unwrap().finalize() }; db.apply_batch(finalized).await.unwrap(); assert!(db.get(&key1).await.unwrap().is_none()); assert_eq!(db.get(&key2).await.unwrap(), Some(val2)); assert_eq!(db.get(&key3).await.unwrap(), Some(val3)); let span2 = db.get_span(&key2).await.unwrap().unwrap(); assert_eq!(span2.1.next_key, key3); let span3 = db.get_span(&key3).await.unwrap().unwrap(); assert_eq!(span3.1.next_key, key2); db.destroy().await.unwrap(); // Create a key that becomes the previous key of a concurrently deleted key. let mut db = open_variable_db(context.with_label("second")).await; let finalized = { let mut batch = db.new_batch(); batch.write(key1.clone(), Some(val1)); batch.write(key3.clone(), Some(val3)); batch.merkleize(None).await.unwrap().finalize() }; db.apply_batch(finalized).await.unwrap(); let finalized = { let mut batch = db.new_batch(); batch.write(key2.clone(), Some(val2)); batch.write(key3.clone(), None); batch.merkleize(None).await.unwrap().finalize() }; db.apply_batch(finalized).await.unwrap(); assert_eq!(db.get(&key1).await.unwrap(), Some(val1)); assert_eq!(db.get(&key2).await.unwrap(), Some(val2)); assert!(db.get(&key3).await.unwrap().is_none()); let span1 = db.get_span(&key1).await.unwrap().unwrap(); assert_eq!(span1.1.next_key, key2); let span2 = db.get_span(&key2).await.unwrap().unwrap(); assert_eq!(span2.1.next_key, key1); db.destroy().await.unwrap(); }); } #[allow(dead_code)] fn assert_merkleized_db_futures_are_send(db: &mut AnyTest, key: Digest, loc: Location) { assert_gettable(db, &key); assert_log_store(db); assert_prunable_store(db, loc); assert_merkleized_store(db, loc); assert_send(db.sync()); } #[allow(dead_code)] fn assert_mutable_db_futures_are_send(db: &mut AnyTest, key: Digest) { assert_gettable(db, &key); assert_log_store(db); assert_send(db.get_all(&key)); assert_send(db.get_with_loc(&key)); assert_send(db.get_span(&key)); } // FromSyncTestable implementation for from_sync_result tests mod from_sync_testable { use super::*; use crate::{ mmr::{iterator::nodes_to_pin, journaled::Mmr}, qmdb::any::sync::tests::FromSyncTestable, }; use futures::future::join_all; type TestMmr = Mmr; impl FromSyncTestable for AnyTest { type Mmr = TestMmr; fn into_log_components(self) -> (Self::Mmr, Self::Journal) { (self.log.mmr, self.log.journal) } async fn pinned_nodes_at(&self, pos: Position) -> Vec { join_all(nodes_to_pin(pos).map(|p| self.log.mmr.get_node(p))) .await .into_iter() .map(|n| n.unwrap().unwrap()) .collect() } fn pinned_nodes_from_map(&self, pos: Position) -> Vec { let map = self.log.mmr.get_pinned_nodes(); nodes_to_pin(pos).map(|p| *map.get(&p).unwrap()).collect() } } } }