commonware_sync/databases/
mod.rs

1//! Database-specific modules for the sync example.
2
3use 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/// Database type to sync.
15#[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
44/// Helper trait for databases that can be synced.
45pub trait Syncable {
46    /// The type of operations in the database.
47    type Operation: Clone + Read<Cfg = ()> + Encode + Send + Sync + 'static;
48
49    /// Create test operations with the given count and seed.
50    fn create_test_operations(count: usize, seed: u64) -> Vec<Self::Operation>;
51
52    /// Add operations to the database.
53    fn add_operations(
54        database: &mut Self,
55        operations: Vec<Self::Operation>,
56    ) -> impl Future<Output = Result<(), adb::Error>>;
57
58    /// Commit pending operations to the database.
59    fn commit(&mut self) -> impl Future<Output = Result<(), adb::Error>>;
60
61    /// Get the database's root digest.
62    fn root(&self, hasher: &mut Standard<commonware_cryptography::Sha256>) -> Key;
63
64    /// Get the operation count of the database.
65    fn op_count(&self) -> u64;
66
67    /// Get the lower bound for operations (inactivity floor or oldest retained location).
68    fn lower_bound_ops(&self) -> u64;
69
70    /// Get historical proof and operations.
71    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    /// Get the database type name for logging.
79    fn name() -> &'static str;
80}