Skip to main content

commonware_sync/databases/
any.rs

1//! Any database types and helpers for the sync example.
2
3use crate::{Hasher, Key, Translator, Value};
4use commonware_cryptography::Hasher as CryptoHasher;
5use commonware_runtime::{buffer, BufferPooler, Clock, Metrics, Storage};
6use commonware_storage::{
7    mmr::{Location, Proof},
8    qmdb::{
9        self,
10        any::{
11            unordered::{
12                fixed::{Db, Operation as FixedOperation},
13                Update,
14            },
15            FixedConfig as Config,
16        },
17        operation::Committable,
18        store::LogStore,
19    },
20};
21use commonware_utils::{NZUsize, NZU16, NZU64};
22use std::{future::Future, num::NonZeroU64};
23use tracing::error;
24
25/// Database type alias.
26pub type Database<E> = Db<E, Key, Value, Hasher, Translator>;
27
28/// Operation type alias.
29pub type Operation = FixedOperation<Key, Value>;
30
31/// Create a database configuration for use in tests.
32pub fn create_config(context: &impl BufferPooler) -> Config<Translator> {
33    Config {
34        mmr_journal_partition: "mmr-journal".into(),
35        mmr_metadata_partition: "mmr-metadata".into(),
36        mmr_items_per_blob: NZU64!(4096),
37        mmr_write_buffer: NZUsize!(4096),
38        log_journal_partition: "log-journal".into(),
39        log_items_per_blob: NZU64!(4096),
40        log_write_buffer: NZUsize!(4096),
41        translator: Translator::default(),
42        thread_pool: None,
43        page_cache: buffer::paged::CacheRef::from_pooler(context, NZU16!(2048), NZUsize!(10)),
44    }
45}
46
47impl<E> crate::databases::Syncable for Database<E>
48where
49    E: Storage + Clock + Metrics,
50{
51    type Operation = Operation;
52
53    fn create_test_operations(count: usize, seed: u64) -> Vec<Self::Operation> {
54        let mut hasher = <Hasher as CryptoHasher>::new();
55        let mut operations = Vec::new();
56        for i in 0..count {
57            let key = {
58                hasher.update(&i.to_be_bytes());
59                hasher.update(&seed.to_be_bytes());
60                hasher.finalize()
61            };
62
63            let value = {
64                hasher.update(&key);
65                hasher.update(b"value");
66                hasher.finalize()
67            };
68
69            operations.push(Operation::Update(Update(key, value)));
70
71            if (i + 1) % 10 == 0 {
72                operations.push(Operation::CommitFloor(None, Location::from(i + 1)));
73            }
74        }
75
76        // Always end with a commit
77        operations.push(Operation::CommitFloor(None, Location::from(count)));
78        operations
79    }
80
81    async fn add_operations(
82        &mut self,
83        operations: Vec<Self::Operation>,
84    ) -> Result<(), commonware_storage::qmdb::Error> {
85        if operations.last().is_none() || !operations.last().unwrap().is_commit() {
86            // Ignore bad inputs rather than return errors.
87            error!("operations must end with a commit");
88            return Ok(());
89        }
90
91        let mut batch = self.new_batch();
92        for operation in operations {
93            match operation {
94                Operation::Update(Update(key, value)) => {
95                    batch.write(key, Some(value));
96                }
97                Operation::Delete(key) => {
98                    batch.write(key, None);
99                }
100                Operation::CommitFloor(metadata, _) => {
101                    let finalized = batch.merkleize(metadata).await?.finalize();
102                    self.apply_batch(finalized).await?;
103                    batch = self.new_batch();
104                }
105            }
106        }
107        Ok(())
108    }
109
110    fn root(&self) -> Key {
111        self.root()
112    }
113
114    async fn size(&self) -> Location {
115        LogStore::bounds(self).await.end
116    }
117
118    async fn inactivity_floor(&self) -> Location {
119        self.inactivity_floor_loc()
120    }
121
122    fn historical_proof(
123        &self,
124        op_count: Location,
125        start_loc: Location,
126        max_ops: NonZeroU64,
127    ) -> impl Future<Output = Result<(Proof<Key>, Vec<Self::Operation>), qmdb::Error>> + Send {
128        self.historical_proof(op_count, start_loc, max_ops)
129    }
130
131    fn name() -> &'static str {
132        "any"
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use crate::databases::Syncable;
140    use commonware_runtime::deterministic;
141
142    type AnyDb = Database<deterministic::Context>;
143
144    #[test]
145    fn test_create_test_operations() {
146        let ops = <AnyDb as Syncable>::create_test_operations(5, 12345);
147        assert_eq!(ops.len(), 6); // 5 operations + 1 commit
148
149        if let Operation::CommitFloor(_, loc) = &ops[5] {
150            assert_eq!(*loc, 5);
151        } else {
152            panic!("Last operation should be a commit");
153        }
154    }
155
156    #[test]
157    fn test_deterministic_operations() {
158        // Operations should be deterministic based on seed
159        let ops1 = <AnyDb as Syncable>::create_test_operations(3, 12345);
160        let ops2 = <AnyDb as Syncable>::create_test_operations(3, 12345);
161        assert_eq!(ops1, ops2);
162
163        // Different seeds should produce different operations
164        let ops3 = <AnyDb as Syncable>::create_test_operations(3, 54321);
165        assert_ne!(ops1, ops3);
166    }
167}