//! The keyless [`compact::Db`]. //! //! See [`crate::qmdb::compact`] for the shared implementation. use super::operation::Operation; pub use crate::qmdb::compact::Config; use crate::{ merkle::{Family, Location}, qmdb::{compact, value::ValueEncoding}, }; use commonware_codec::CodecShared; use commonware_cryptography::Hasher; use commonware_parallel::Strategy; impl compact::sealed::Sealed for Operation { type Mutations = Vec; const NAME: &'static str = "keyless"; fn mutation(value: V::Value) -> Self { Self::Append(value) } } impl compact::Operation for Operation where Self: CodecShared, { type Metadata = V::Value; fn commit(metadata: Option, inactivity_floor_loc: Location) -> Self { Self::Commit(metadata, inactivity_floor_loc) } fn metadata(&self) -> Option<&V::Value> { match self { Self::Commit(metadata, _) => metadata.as_ref(), Self::Append(_) => None, } } } /// A keyless compact db. pub type Db = compact::Db, H, S>; /// A speculative batch for a keyless compact db. pub type UnmerkleizedBatch = compact::UnmerkleizedBatch, S>; /// A speculative batch for a keyless compact db whose root digest has been computed. pub type MerkleizedBatch = compact::MerkleizedBatch, S>; impl UnmerkleizedBatch where F: Family, H: Hasher, V: ValueEncoding, S: Strategy, Operation: CodecShared, { /// The location that the next appended value will be placed at. pub fn size(&self) -> Location { self.base.size + self.mutations.len() as u64 } /// Append `value`. pub fn append(mut self, value: V::Value) -> Self { self.mutations.push(value); self } } #[cfg(test)] mod tests { use super::*; use crate::{ merkle::{Location, mmb, mmr}, qmdb::{ compact::db::tests::{TestBatch, TestOperation, compact_db_tests, open_db}, value::{FixedEncoding, VariableEncoding}, }, }; use commonware_codec::RangeCfg; use commonware_cryptography::Sha256; use commonware_macros::test_traced; use commonware_runtime::{Runner as _, Supervisor as _, deterministic}; use commonware_utils::sequence::U64; type TestOp = Operation>; impl TestOperation for TestOp { type Family = F; fn codec_config() {} fn value(seed: u64) -> U64 { U64::new(seed) } fn mutate(batch: TestBatch, seed: u64) -> TestBatch { batch.append(U64::new(seed)) } fn op(seed: u64) -> Self { Self::Append(U64::new(seed)) } } type VariableTestOp = Operation>>; impl TestOperation for VariableTestOp { type Family = F; fn codec_config() -> (RangeCfg, ()) { ((..=64).into(), ()) } fn value(seed: u64) -> Vec { seed.to_be_bytes().to_vec() } fn mutate(batch: TestBatch, seed: u64) -> TestBatch { batch.append(Self::value(seed)) } fn op(seed: u64) -> Self { Self::Append(Self::value(seed)) } } compact_db_tests!(TestOp); mod mmb_tests { use super::*; compact_db_tests!(TestOp); } mod variable_tests { use super::*; compact_db_tests!(VariableTestOp); } mod variable_mmb_tests { use super::*; compact_db_tests!(VariableTestOp); } /// A batch's `size` is the location of its next append, from the database's size or its /// parent's tip. #[test_traced("INFO")] fn test_compact_batch_size() { deterministic::Runner::default().start(|context| async move { let db = open_db::(context.child("db"), "keyless-batch-size").await; let start = db.size(); let batch = db.new_batch(); assert_eq!(batch.size(), start); let batch = batch.append(U64::new(1)).append(U64::new(2)); assert_eq!(batch.size(), start + 2); // The commit follows the appends, and a child appends after it. let merkleized = batch.merkleize(&db).await.unwrap(); assert_eq!(merkleized.bounds().tip.size, start + 3); let child = merkleized.new_batch::().append(U64::new(3)); assert_eq!(child.size(), start + 4); let db = db.apply_batch(merkleized).await.unwrap(); assert_eq!(db.size(), start + 3); db.destroy().await.unwrap(); }); } /// Appends are ordered: the same values in a different order give a different root. #[test_traced("INFO")] fn test_compact_append_order_is_significant() { deterministic::Runner::default().start(|context| async move { let forward = open_db::(context.child("forward"), "compact-append-forward").await; let batch = forward .new_batch() .append(U64::new(1)) .append(U64::new(2)) .merkleize(&forward) .await .unwrap(); let applied_start = forward.size(); let forward = forward.apply_batch(batch).await.unwrap(); assert_eq!( applied_start..forward.size(), Location::new(1)..Location::new(4) ); let reverse = open_db::(context.child("reverse"), "compact-append-reverse").await; let batch = reverse .new_batch() .append(U64::new(2)) .append(U64::new(1)) .merkleize(&reverse) .await .unwrap(); let reverse = reverse.apply_batch(batch).await.unwrap(); assert_eq!(forward.size(), reverse.size()); assert_ne!(forward.root(), reverse.root()); }); } }