//! An _unordered_ variant of a [crate::qmdb::current] authenticated database for variable-size //! values. //! //! This variant does not maintain key ordering, so it cannot generate exclusion proofs. Use //! [crate::qmdb::current::ordered::variable] if exclusion proofs are required. //! //! See [Db] for the main database type. pub use crate::qmdb::any::unordered::variable::{Operation, Update}; use crate::{ index::unordered::Index, journal::contiguous::variable::{Config as JournalConfig, Journal}, merkle::Location, qmdb::{current, value::VariableEncoding}, }; pub type Db = super::db::Db< F, E, Journal>, K, VariableEncoding, Index>, H, N, S, >; /// Configuration for a [Db]. pub type Config = current::Config, S>; pub mod partitioned { //! A variant of [super] that uses a partitioned index for the snapshot, dividing the key space //! into `2^(P*8)` partitions. See [Any's counterpart] for when to use one. //! //! See [Db] for the generic type, or use the convenience aliases: //! - [p256::Db] for 256 partitions (P=1) //! - [p64k::Db] for 65,536 partitions (P=2) //! //! [Any's counterpart]: crate::qmdb::any::unordered::variable::partitioned use super::*; pub use super::{Config, Operation, Update}; use crate::index::partitioned::unordered::Index; /// A 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 /// - `P = 3`: ~16 million partitions pub type Db = crate::qmdb::current::unordered::db::Db< F, E, Journal>, K, VariableEncoding, Index, P>, H, N, S, >; /// Convenience type aliases for 256 partitions (P=1). pub mod p256 { /// Variable-value DB with 256 partitions. pub type Db = super::Db; pub use super::Config; } /// Convenience type aliases for 65,536 partitions (P=2). pub mod p64k { /// Variable-value DB with 65,536 partitions. pub type Db = super::Db; pub use super::Config; } } #[cfg(test)] mod test { use super::*; use crate::{ mmr, qmdb::current::{tests::variable_config, unordered::tests as shared}, translator::TwoCap, }; use commonware_cryptography::{Hasher as _, Sha256, sha256::Digest}; use commonware_macros::test_traced; use commonware_runtime::{Runner as _, Supervisor as _, deterministic}; use commonware_utils::NZUsize; /// A type alias for the concrete [Db] type used in these unit tests. type CurrentTest = Db< mmr::Family, deterministic::Context, Digest, Digest, Sha256, TwoCap, 32, commonware_parallel::Sequential, >; /// Return a [Db] database initialized with a variable config. async fn open_db(context: deterministic::Context, partition_prefix: String) -> CurrentTest { let cfg = variable_config::(&partition_prefix); CurrentTest::init(context, cfg, None).await.unwrap() } #[test_traced("DEBUG")] pub fn test_current_db_verify_proof_over_bits_in_uncommitted_chunk() { shared::test_verify_proof_over_bits_in_uncommitted_chunk(open_db); } #[test_traced("DEBUG")] pub fn test_current_db_range_proofs() { shared::test_range_proofs(open_db); } #[test_traced("DEBUG")] pub fn test_current_db_key_value_proof() { shared::test_key_value_proof(open_db); } #[test_traced("WARN")] pub fn test_current_db_proving_repeated_updates() { shared::test_proving_repeated_updates(open_db); } /// A [Db] keyed by variable-length byte keys. type VecKeyTest = Db< mmr::Family, deterministic::Context, Vec, Digest, Sha256, TwoCap, 32, commonware_parallel::Sequential, >; #[test_traced("WARN")] pub fn test_current_db_variable_length_keys() { let executor = deterministic::Runner::default(); executor.start(|context| async move { // Configure the operation codec for variable-length keys. let base = variable_config::("vec-keys"); let cfg = Config { merkle: base.merkle.clone(), log: crate::journal::contiguous::variable::Config { partition: base.log.partition.clone(), items_per_section: base.log.items_per_section, compression: None, codec_config: (((0..).into(), ()), ()), page_cache: base.log.page_cache.clone(), write_buffer: base.log.write_buffer, replay_buffer: base.log.replay_buffer, }, grafted_metadata_partition: base.grafted_metadata_partition.clone(), translator: TwoCap, init_cache: base.init_cache, init_buffer: base.init_buffer, init_concurrency: NZUsize!(2), }; // Commit a value and verify its lookup and proof under a variable-length key. let db = VecKeyTest::init(context.child("first"), cfg.clone(), None) .await .unwrap(); let key = b"variable-length-key".to_vec(); let value = Sha256::hash(&[b"value"]); let merkleized = db .new_batch() .write(key.clone(), Some(value)) .merkleize(&db) .await .unwrap(); let db = db.apply_batch(merkleized).await.unwrap(); let db = db.commit().await.unwrap(); assert_eq!(db.get(&key).await.unwrap().unwrap(), value); let root = db.root(); let (proof, proved_value) = db.key_value_proof(&key).await.unwrap(); assert_eq!(proved_value, value); assert!(proof.verify::, _>( key.clone(), proved_value, &root )); drop(db); // Reopen the database and verify the committed root and value. let db = VecKeyTest::init(context.child("second"), cfg, None) .await .unwrap(); assert_eq!(db.root(), root); assert_eq!(db.get(&key).await.unwrap().unwrap(), value); db.destroy().await.unwrap(); }); } }