|
| 1 | +//! Commonware-based QMDB backend implementation. |
| 2 | +
|
| 3 | +use alloy_primitives::B256; |
| 4 | +use async_trait::async_trait; |
| 5 | +use kora_handlers::{HandleError, RootProvider}; |
| 6 | +use kora_qmdb::StateRoot; |
| 7 | + |
| 8 | +use crate::{AccountStore, BackendError, CodeStore, QmdbBackendConfig, StorageStore}; |
| 9 | + |
| 10 | +/// Commonware-based QMDB backend. |
| 11 | +/// |
| 12 | +/// Provides storage for accounts, storage slots, and code using |
| 13 | +/// commonware-storage primitives. |
| 14 | +#[derive(Debug)] |
| 15 | +pub struct CommonwareBackend { |
| 16 | + accounts: AccountStore, |
| 17 | + storage: StorageStore, |
| 18 | + code: CodeStore, |
| 19 | +} |
| 20 | + |
| 21 | +impl CommonwareBackend { |
| 22 | + /// Create a new backend with default in-memory stores. |
| 23 | + pub fn new() -> Self { |
| 24 | + Self { accounts: AccountStore::new(), storage: StorageStore::new(), code: CodeStore::new() } |
| 25 | + } |
| 26 | + |
| 27 | + /// Open a backend with the given configuration. |
| 28 | + /// |
| 29 | + /// Note: Currently this creates in-memory stores regardless of config. |
| 30 | + /// Full persistent storage will be added when commonware-storage QMDB |
| 31 | + /// is available. |
| 32 | + pub async fn open(_config: QmdbBackendConfig) -> Result<Self, BackendError> { |
| 33 | + // For now, we just create in-memory stores |
| 34 | + // In the future, this will use the config to open persistent stores |
| 35 | + Ok(Self::new()) |
| 36 | + } |
| 37 | + |
| 38 | + /// Get a reference to the accounts store. |
| 39 | + pub const fn accounts(&self) -> &AccountStore { |
| 40 | + &self.accounts |
| 41 | + } |
| 42 | + |
| 43 | + /// Get a mutable reference to the accounts store. |
| 44 | + pub const fn accounts_mut(&mut self) -> &mut AccountStore { |
| 45 | + &mut self.accounts |
| 46 | + } |
| 47 | + |
| 48 | + /// Get a reference to the storage store. |
| 49 | + pub const fn storage(&self) -> &StorageStore { |
| 50 | + &self.storage |
| 51 | + } |
| 52 | + |
| 53 | + /// Get a mutable reference to the storage store. |
| 54 | + pub const fn storage_mut(&mut self) -> &mut StorageStore { |
| 55 | + &mut self.storage |
| 56 | + } |
| 57 | + |
| 58 | + /// Get a reference to the code store. |
| 59 | + pub const fn code(&self) -> &CodeStore { |
| 60 | + &self.code |
| 61 | + } |
| 62 | + |
| 63 | + /// Get a mutable reference to the code store. |
| 64 | + pub const fn code_mut(&mut self) -> &mut CodeStore { |
| 65 | + &mut self.code |
| 66 | + } |
| 67 | + |
| 68 | + /// Get the current state root. |
| 69 | + pub async fn get_state_root(&self) -> Result<B256, BackendError> { |
| 70 | + let accounts_root = self.accounts.root().await?; |
| 71 | + let storage_root = self.storage.root().await?; |
| 72 | + let code_root = self.code.root().await?; |
| 73 | + Ok(StateRoot::compute(accounts_root, storage_root, code_root)) |
| 74 | + } |
| 75 | + |
| 76 | + /// Compute the state root. |
| 77 | + /// |
| 78 | + /// This is the same as `get_state_root` for now since we compute |
| 79 | + /// roots incrementally. |
| 80 | + pub async fn compute_state_root(&mut self) -> Result<B256, BackendError> { |
| 81 | + self.get_state_root().await |
| 82 | + } |
| 83 | + |
| 84 | + /// Commit pending changes and return the new state root. |
| 85 | + /// |
| 86 | + /// For now, this just computes the root since we apply changes |
| 87 | + /// immediately in write_batch. |
| 88 | + pub async fn commit(&mut self) -> Result<B256, BackendError> { |
| 89 | + self.get_state_root().await |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl Default for CommonwareBackend { |
| 94 | + fn default() -> Self { |
| 95 | + Self::new() |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[async_trait] |
| 100 | +impl RootProvider for CommonwareBackend { |
| 101 | + async fn state_root(&self) -> Result<B256, HandleError> { |
| 102 | + self.get_state_root().await.map_err(|e| HandleError::RootComputation(e.to_string())) |
| 103 | + } |
| 104 | + |
| 105 | + async fn compute_root(&mut self) -> Result<B256, HandleError> { |
| 106 | + self.compute_state_root().await.map_err(|e| HandleError::RootComputation(e.to_string())) |
| 107 | + } |
| 108 | + |
| 109 | + async fn commit_and_get_root(&mut self) -> Result<B256, HandleError> { |
| 110 | + self.commit().await.map_err(|e| HandleError::RootComputation(e.to_string())) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +#[cfg(test)] |
| 115 | +mod tests { |
| 116 | + use super::*; |
| 117 | + use std::path::PathBuf; |
| 118 | + |
| 119 | + #[tokio::test] |
| 120 | + async fn backend_new() { |
| 121 | + let backend = CommonwareBackend::new(); |
| 122 | + let root = backend.get_state_root().await.unwrap(); |
| 123 | + // Initial root should be deterministic (based on empty stores) |
| 124 | + assert_ne!(root, B256::ZERO); // MMR has non-zero initial root |
| 125 | + } |
| 126 | + |
| 127 | + #[tokio::test] |
| 128 | + async fn backend_default() { |
| 129 | + let backend = CommonwareBackend::default(); |
| 130 | + assert!(backend.accounts().root().await.is_ok()); |
| 131 | + } |
| 132 | + |
| 133 | + #[tokio::test] |
| 134 | + async fn backend_open() { |
| 135 | + let config = QmdbBackendConfig::new(PathBuf::from("/tmp/test"), 1000); |
| 136 | + let backend = CommonwareBackend::open(config).await.unwrap(); |
| 137 | + assert!(backend.get_state_root().await.is_ok()); |
| 138 | + } |
| 139 | +} |
0 commit comments