commonware_sync/databases/
immutable.rs1use crate::{Hasher, Key, Translator, Value};
4use commonware_cryptography::{Hasher as CryptoHasher, Sha256};
5use commonware_runtime::{Clock, Metrics, Storage};
6use commonware_storage::{
7 adb::{
8 self,
9 immutable::{self, Config},
10 },
11 mmr::{hasher::Standard, verification::Proof},
12 store::operation,
13};
14use commonware_utils::{NZUsize, NZU64};
15use std::{future::Future, num::NonZeroU64};
16
17pub type Database<E> = immutable::Immutable<E, Key, Value, Hasher, Translator>;
19
20pub type Operation = operation::Variable<Key, Value>;
22
23pub fn create_config() -> Config<Translator, ()> {
25 Config {
26 mmr_journal_partition: "mmr_journal".into(),
27 mmr_metadata_partition: "mmr_metadata".into(),
28 mmr_items_per_blob: NZU64!(4096),
29 mmr_write_buffer: NZUsize!(1024),
30 log_journal_partition: "log_journal".into(),
31 log_items_per_section: NZU64!(512),
32 log_compression: None,
33 log_codec_config: (),
34 log_write_buffer: NZUsize!(1024),
35 locations_journal_partition: "locations_journal".into(),
36 locations_items_per_blob: NZU64!(4096),
37 translator: commonware_storage::translator::EightCap,
38 thread_pool: None,
39 buffer_pool: commonware_runtime::buffer::PoolRef::new(NZUsize!(1024), NZUsize!(10)),
40 }
41}
42
43pub fn create_test_operations(count: usize, seed: u64) -> Vec<Operation> {
46 let mut operations = Vec::new();
47 let mut hasher = <Hasher as CryptoHasher>::new();
48
49 for i in 0..count {
50 let key = {
51 hasher.update(&i.to_be_bytes());
52 hasher.update(&seed.to_be_bytes());
53 hasher.finalize()
54 };
55
56 let value = {
57 hasher.update(&key);
58 hasher.update(b"value");
59 hasher.finalize()
60 };
61
62 operations.push(Operation::Set(key, value));
63
64 if (i + 1) % 10 == 0 {
65 operations.push(Operation::Commit(None));
66 }
67 }
68
69 operations.push(Operation::Commit(Some(Sha256::fill(1))));
71 operations
72}
73
74impl<E> super::Syncable for Database<E>
75where
76 E: Storage + Clock + Metrics,
77{
78 type Operation = Operation;
79
80 fn create_test_operations(count: usize, seed: u64) -> Vec<Self::Operation> {
81 create_test_operations(count, seed)
82 }
83
84 async fn add_operations(
85 database: &mut Self,
86 operations: Vec<Self::Operation>,
87 ) -> Result<(), commonware_storage::adb::Error> {
88 for operation in operations {
89 match operation {
90 Operation::Set(key, value) => {
91 database.set(key, value).await?;
92 }
93 Operation::Commit(metadata) => {
94 database.commit(metadata).await?;
95 }
96 _ => {}
97 }
98 }
99 Ok(())
100 }
101
102 async fn commit(&mut self) -> Result<(), commonware_storage::adb::Error> {
103 self.commit(None).await
104 }
105
106 fn root(&self, hasher: &mut Standard<commonware_cryptography::Sha256>) -> Key {
107 self.root(hasher)
108 }
109
110 fn op_count(&self) -> u64 {
111 self.op_count()
112 }
113
114 fn lower_bound_ops(&self) -> u64 {
115 self.oldest_retained_loc().unwrap_or(0)
116 }
117
118 fn historical_proof(
119 &self,
120 size: u64,
121 start_loc: u64,
122 max_ops: NonZeroU64,
123 ) -> impl Future<Output = Result<(Proof<Key>, Vec<Self::Operation>), adb::Error>> + Send {
124 self.historical_proof(size, start_loc, max_ops)
125 }
126
127 fn name() -> &'static str {
128 "immutable"
129 }
130}