commonware_sync/databases/
mod.rs1use crate::Key;
4use commonware_codec::{Encode, Read};
5use commonware_storage::{
6 adb,
7 mmr::{hasher::Standard, verification::Proof},
8};
9use std::{future::Future, num::NonZeroU64};
10
11pub mod any;
12pub mod immutable;
13
14#[derive(Debug, Clone, Copy)]
16pub enum DatabaseType {
17 Any,
18 Immutable,
19}
20
21impl std::str::FromStr for DatabaseType {
22 type Err = String;
23
24 fn from_str(s: &str) -> Result<Self, Self::Err> {
25 match s.to_lowercase().as_str() {
26 "any" => Ok(DatabaseType::Any),
27 "immutable" => Ok(DatabaseType::Immutable),
28 _ => Err(format!(
29 "Invalid database type: '{s}'. Must be 'any' or 'immutable'",
30 )),
31 }
32 }
33}
34
35impl DatabaseType {
36 pub fn as_str(&self) -> &'static str {
37 match self {
38 DatabaseType::Any => "any",
39 DatabaseType::Immutable => "immutable",
40 }
41 }
42}
43
44pub trait Syncable {
46 type Operation: Clone + Read<Cfg = ()> + Encode + Send + Sync + 'static;
48
49 fn create_test_operations(count: usize, seed: u64) -> Vec<Self::Operation>;
51
52 fn add_operations(
54 database: &mut Self,
55 operations: Vec<Self::Operation>,
56 ) -> impl Future<Output = Result<(), adb::Error>>;
57
58 fn commit(&mut self) -> impl Future<Output = Result<(), adb::Error>>;
60
61 fn root(&self, hasher: &mut Standard<commonware_cryptography::Sha256>) -> Key;
63
64 fn op_count(&self) -> u64;
66
67 fn lower_bound_ops(&self) -> u64;
69
70 fn historical_proof(
72 &self,
73 size: u64,
74 start_loc: u64,
75 max_ops: NonZeroU64,
76 ) -> impl Future<Output = Result<(Proof<Key>, Vec<Self::Operation>), adb::Error>> + Send;
77
78 fn name() -> &'static str;
80}