//! The immutable [`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, operation::Key, value::ValueEncoding}, }; use commonware_codec::CodecShared; use commonware_cryptography::Hasher; use commonware_parallel::Strategy; use std::collections::BTreeMap; impl compact::sealed::Sealed for Operation { type Mutations = BTreeMap; const NAME: &'static str = "immutable"; fn mutation((key, value): (K, V::Value)) -> Self { Self::Set(key, 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::Set(..) => None, } } } /// An immutable compact db. pub type Db = compact::Db, H, S>; /// A speculative batch for an immutable compact db. pub type UnmerkleizedBatch = compact::UnmerkleizedBatch, S>; /// A speculative batch for an immutable compact db whose root digest has been computed. pub type MerkleizedBatch = compact::MerkleizedBatch, S>; impl UnmerkleizedBatch where F: Family, H: Hasher, K: Key, V: ValueEncoding, S: Strategy, Operation: CodecShared, { /// Set `key` to `value`. A later `set` of the same key in this batch replaces it. pub fn set(mut self, key: K, value: V::Value) -> Self { self.mutations.insert(key, 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, sha256::Digest}; use commonware_macros::test_traced; use commonware_runtime::{Runner as _, Supervisor as _, deterministic}; type TestOp = Operation>; impl TestOperation for TestOp { type Family = F; fn codec_config() {} fn value(seed: u64) -> Digest { Sha256::hash(&[&seed.to_le_bytes()]) } fn mutate(batch: TestBatch, seed: u64) -> TestBatch { batch.set(Sha256::hash(&[&seed.to_be_bytes()]), Self::value(seed)) } fn op(seed: u64) -> Self { Self::Set(Sha256::hash(&[&seed.to_be_bytes()]), Self::value(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.set(Sha256::hash(&[&seed.to_be_bytes()]), Self::value(seed)) } fn op(seed: u64) -> Self { Self::Set(Sha256::hash(&[&seed.to_be_bytes()]), 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); } /// Setting a key twice in one batch keeps the later value and emits one operation. #[test_traced("INFO")] fn test_compact_set_same_key_twice_keeps_last() { deterministic::Runner::default().start(|context| async move { let key = Sha256::hash(&[b"key"]); let (first, last) = (Sha256::fill(1), Sha256::fill(2)); let twice = open_db::(context.child("twice"), "compact-set-twice").await; let batch = twice .new_batch() .set(key, first) .set(key, last) .merkleize(&twice) .await .unwrap(); let applied_start = twice.size(); let twice = twice.apply_batch(batch).await.unwrap(); assert_eq!( applied_start..twice.size(), Location::new(1)..Location::new(3) ); let once = open_db::(context.child("once"), "compact-set-once").await; let batch = once .new_batch() .set(key, last) .merkleize(&once) .await .unwrap(); let once = once.apply_batch(batch).await.unwrap(); assert_eq!(twice.root(), once.root()); }); } /// Sets are applied in key order: insertion order does not affect the root. #[test_traced("INFO")] fn test_compact_set_order_is_insignificant() { deterministic::Runner::default().start(|context| async move { let (k1, k2) = (Sha256::hash(&[b"k1"]), Sha256::hash(&[b"k2"])); let (v1, v2) = (Sha256::fill(1), Sha256::fill(2)); let forward = open_db::(context.child("forward"), "compact-set-forward").await; let batch = forward .new_batch() .set(k1, v1) .set(k2, v2) .merkleize(&forward) .await .unwrap(); let forward = forward.apply_batch(batch).await.unwrap(); let reverse = open_db::(context.child("reverse"), "compact-set-reverse").await; let batch = reverse .new_batch() .set(k2, v2) .set(k1, v1) .merkleize(&reverse) .await .unwrap(); let reverse = reverse.apply_batch(batch).await.unwrap(); assert_eq!(forward.size(), reverse.size()); assert_eq!(forward.root(), reverse.root()); }); } }