//! Immutable database types and helpers for the sync example. use crate::{Hasher, Key, Translator, Value}; use commonware_cryptography::{Hasher as CryptoHasher, Sha256}; use commonware_runtime::{BufferPooler, Clock, Metrics, Storage}; use commonware_storage::{ mmr::{Location, Proof}, qmdb::{ self, immutable::{self, Config}, store::LogStore, }, }; use commonware_utils::{NZUsize, NZU16, NZU64}; use std::{future::Future, num::NonZeroU64}; use tracing::error; /// Database type alias. pub type Database = immutable::Immutable; /// Operation type alias. pub type Operation = immutable::Operation; /// Create a database configuration with appropriate partitioning for Immutable. pub fn create_config(context: &impl BufferPooler) -> Config { Config { mmr_journal_partition: "mmr-journal".into(), mmr_metadata_partition: "mmr-metadata".into(), mmr_items_per_blob: NZU64!(4096), mmr_write_buffer: NZUsize!(4096), log_partition: "log".into(), log_items_per_section: NZU64!(4096), log_compression: None, log_codec_config: (), log_write_buffer: NZUsize!(4096), translator: commonware_storage::translator::EightCap, thread_pool: None, page_cache: commonware_runtime::buffer::paged::CacheRef::from_pooler( context, NZU16!(2048), NZUsize!(10), ), } } /// Create deterministic test operations for demonstration purposes. /// Generates Set operations and periodic Commit operations. pub fn create_test_operations(count: usize, seed: u64) -> Vec { let mut operations = Vec::new(); let mut hasher = ::new(); for i in 0..count { let key = { hasher.update(&i.to_be_bytes()); hasher.update(&seed.to_be_bytes()); hasher.finalize() }; let value = { hasher.update(&key); hasher.update(b"value"); hasher.finalize() }; operations.push(Operation::Set(key, value)); if (i + 1) % 10 == 0 { operations.push(Operation::Commit(None)); } } // Always end with a commit operations.push(Operation::Commit(Some(Sha256::fill(1)))); operations } impl super::Syncable for Database where E: Storage + Clock + Metrics, { type Operation = Operation; fn create_test_operations(count: usize, seed: u64) -> Vec { create_test_operations(count, seed) } async fn add_operations( &mut self, operations: Vec, ) -> Result<(), commonware_storage::qmdb::Error> { if operations.last().is_none() || !operations.last().unwrap().is_commit() { // Ignore bad inputs rather than return errors. error!("operations must end with a commit"); return Ok(()); } let mut batch = self.new_batch(); for operation in operations { match operation { Operation::Set(key, value) => { batch.set(key, value); } Operation::Commit(metadata) => { let finalized = batch.merkleize(metadata).finalize(); self.apply_batch(finalized).await?; batch = self.new_batch(); } } } Ok(()) } fn root(&self) -> Key { self.root() } async fn size(&self) -> Location { LogStore::bounds(self).await.end } async fn inactivity_floor(&self) -> Location { // For Immutable databases, all retained operations are active, // so the inactivity floor equals the pruning boundary. LogStore::bounds(self).await.start } fn historical_proof( &self, op_count: Location, start_loc: Location, max_ops: NonZeroU64, ) -> impl Future, Vec), qmdb::Error>> + Send { self.historical_proof(op_count, start_loc, max_ops) } fn name() -> &'static str { "immutable" } }