commonware_sync/databases/
mod.rs1use crate::Key;
4use commonware_codec::Encode;
5use commonware_storage::{
6 mmr::{Location, Proof},
7 qmdb::{self, operation::Operation},
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(Self::Any),
27 "immutable" => Ok(Self::Immutable),
28 _ => Err(format!(
29 "Invalid database type: '{s}'. Must be 'any' or 'immutable'",
30 )),
31 }
32 }
33}
34
35impl DatabaseType {
36 pub const fn as_str(&self) -> &'static str {
37 match self {
38 Self::Any => "any",
39 Self::Immutable => "immutable",
40 }
41 }
42}
43
44pub trait Syncable {
46 type Operation: Operation + Encode + 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<(), qmdb::Error>>;
57
58 fn commit(&mut self) -> impl Future<Output = Result<(), qmdb::Error>>;
60
61 fn root(&self) -> Key;
63
64 fn op_count(&self) -> Location;
66
67 fn lower_bound(&self) -> Location;
69
70 fn historical_proof(
72 &self,
73 op_count: Location,
74 start_loc: Location,
75 max_ops: NonZeroU64,
76 ) -> impl Future<Output = Result<(Proof<Key>, Vec<Self::Operation>), qmdb::Error>> + Send;
77
78 fn name() -> &'static str;
80}