Skip to main content

heika_node/
store.rs

1//! 中核に渡す保存。[`Db`] を、読んだ状態を覚えておく [`CachedSource`] で包む。コミットで変わった行とノードは
2//! キャッシュにも伝え、巻き戻したら空にする。[`Db`] は `Arc` で持ち、API などほかのスレッドとも分け合う
3//! (redb は、書いている最中でも読むトランザクションを並べて開ける)。
4
5use std::sync::Arc;
6
7use heika_chain::ChainError;
8use heika_chain::store::{BlockWrite, ChainStore, Change, Prune, Pruned, StateRow};
9use heika_state_transition::executor::ExecError;
10use heika_state_transition::executor::source::{CachedSource, StateSource};
11use heika_store_redb::Db;
12
13pub struct CachedStore {
14    source: CachedSource<Arc<Db>>,
15}
16
17impl CachedStore {
18    /// `db` を包み、読んだ状態を `capacity` バイトまで覚える
19    pub fn new(db: Arc<Db>, capacity: usize) -> Self {
20        Self {
21            source: CachedSource::new(db, capacity),
22        }
23    }
24
25    pub fn db(&self) -> &Db {
26        self.source.inner()
27    }
28
29    /// ほかのスレッドに渡す、同じ DB
30    pub fn shared_db(&self) -> Arc<Db> {
31        Arc::clone(self.source.inner())
32    }
33}
34
35impl StateSource for CachedStore {
36    fn row(&self, key: &[u8]) -> Result<Option<Vec<u8>>, ExecError> {
37        self.source.row(key)
38    }
39
40    fn node(&self, key: &[u8]) -> Result<Option<Vec<u8>>, ExecError> {
41        self.source.node(key)
42    }
43
44    fn for_each_row(
45        &self,
46        prefix: &[u8],
47        visit: &mut dyn FnMut(&[u8], &[u8]),
48    ) -> Result<(), ExecError> {
49        self.source.for_each_row(prefix, visit)
50    }
51}
52
53impl ChainStore for CachedStore {
54    async fn load_state(&self) -> Result<(Vec<u8>, Vec<StateRow>, Vec<StateRow>), ChainError> {
55        self.db().load_state().await
56    }
57
58    async fn load_partial_state(
59        &self,
60    ) -> Result<(Vec<u8>, Vec<StateRow>, Vec<StateRow>), ChainError> {
61        self.db().load_partial_state().await
62    }
63
64    async fn block(&self, height: u64) -> Result<Option<Vec<u8>>, ChainError> {
65        ChainStore::block(self.db(), height).await
66    }
67
68    async fn blocks_from(&self) -> Result<u64, ChainError> {
69        ChainStore::blocks_from(self.db()).await
70    }
71
72    async fn proof(&self, epoch: u32) -> Result<Option<Vec<u8>>, ChainError> {
73        ChainStore::proof(self.db(), epoch).await
74    }
75
76    async fn proofs_tip(&self) -> Result<u32, ChainError> {
77        ChainStore::proofs_tip(self.db()).await
78    }
79
80    async fn voters(&self, epoch: u32) -> Result<Option<Vec<u8>>, ChainError> {
81        ChainStore::voters(self.db(), epoch).await
82    }
83
84    async fn put_proof(&self, epoch: u32, proof: &[u8]) -> Result<(), ChainError> {
85        self.db().put_proof(epoch, proof).await
86    }
87
88    async fn commit_block(&self, write: BlockWrite<'_>) -> Result<(), ChainError> {
89        let (state, nodes) = (write.state.clone(), write.nodes.clone());
90        self.db().commit_block(write).await?;
91        self.source.record(state, nodes);
92        Ok(())
93    }
94
95    async fn prune(&self, prune: Prune) -> Result<Pruned, ChainError> {
96        ChainStore::prune(self.db(), prune).await
97    }
98
99    async fn checkpoint_at_or_below(
100        &self,
101        height: u64,
102    ) -> Result<Option<(u64, Vec<u8>)>, ChainError> {
103        ChainStore::checkpoint_at_or_below(self.db(), height).await
104    }
105
106    async fn changes_above(&self, height: u64) -> Result<Vec<Change>, ChainError> {
107        ChainStore::changes_above(self.db(), height).await
108    }
109
110    async fn rollback(
111        &self,
112        height: u64,
113        voters_from: u32,
114        orphans: &[(u64, [u8; 32], &[u8])],
115    ) -> Result<(), ChainError> {
116        let rolled = ChainStore::rollback(self.db(), height, voters_from, orphans).await;
117        // 失敗しても、どこまで戻ったかわからないので捨てる
118        self.source.clear();
119        rolled
120    }
121}