consensus_core/storage/
mod.rs1#[cfg(test)]
6pub(crate) mod mem_store;
7pub(crate) mod rocksdb_store;
8
9#[cfg(test)]
10mod store_tests;
11
12use consensus_config::AuthorityIndex;
13
14use crate::{
15 CommitIndex,
16 block::{BlockRef, Round, VerifiedBlock},
17 commit::{CommitInfo, CommitRange, CommitRef, TrustedCommit},
18 error::ConsensusResult,
19};
20
21pub(crate) trait Store: Send + Sync {
23 fn write(&self, write_batch: WriteBatch) -> ConsensusResult<()>;
25
26 fn read_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<Option<VerifiedBlock>>>;
28
29 fn contains_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<bool>>;
31
32 #[allow(dead_code)]
34 fn contains_block_at_slot(&self, slot: crate::block::Slot) -> ConsensusResult<bool>;
35
36 fn scan_blocks_by_author(
38 &self,
39 authority: AuthorityIndex,
40 start_round: Round,
41 ) -> ConsensusResult<Vec<VerifiedBlock>>;
42
43 #[allow(dead_code)]
48 fn scan_last_blocks_by_author(
49 &self,
50 author: AuthorityIndex,
51 num_of_rounds: u64,
52 before_round: Option<Round>,
53 ) -> ConsensusResult<Vec<VerifiedBlock>>;
54
55 fn read_last_commit(&self) -> ConsensusResult<Option<TrustedCommit>>;
57
58 fn scan_commits(&self, range: CommitRange) -> ConsensusResult<Vec<TrustedCommit>>;
60
61 fn read_commit_votes(&self, commit_index: CommitIndex) -> ConsensusResult<Vec<BlockRef>>;
63
64 fn read_last_commit_info(&self) -> ConsensusResult<Option<(CommitRef, CommitInfo)>>;
66}
67
68#[derive(Debug, Default)]
70pub(crate) struct WriteBatch {
71 pub(crate) blocks: Vec<VerifiedBlock>,
72 pub(crate) commits: Vec<TrustedCommit>,
73 pub(crate) commit_info: Vec<(CommitRef, CommitInfo)>,
74}
75
76impl WriteBatch {
77 pub(crate) fn new(
78 blocks: Vec<VerifiedBlock>,
79 commits: Vec<TrustedCommit>,
80 commit_info: Vec<(CommitRef, CommitInfo)>,
81 ) -> Self {
82 WriteBatch {
83 blocks,
84 commits,
85 commit_info,
86 }
87 }
88
89 #[cfg(test)]
92 pub(crate) fn blocks(mut self, blocks: Vec<VerifiedBlock>) -> Self {
93 self.blocks = blocks;
94 self
95 }
96
97 #[cfg(test)]
98 pub(crate) fn commits(mut self, commits: Vec<TrustedCommit>) -> Self {
99 self.commits = commits;
100 self
101 }
102
103 #[cfg(test)]
104 pub(crate) fn commit_info(mut self, commit_info: Vec<(CommitRef, CommitInfo)>) -> Self {
105 self.commit_info = commit_info;
106 self
107 }
108}