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 metrics::StoredScoringMetricsU64,
20};
21
22pub(crate) trait Store: Send + Sync {
24 fn write(&self, write_batch: WriteBatch) -> ConsensusResult<()>;
26
27 fn read_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<Option<VerifiedBlock>>>;
29
30 fn contains_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<bool>>;
32
33 #[allow(dead_code)]
35 fn contains_block_at_slot(&self, slot: crate::block::Slot) -> ConsensusResult<bool>;
36
37 fn scan_blocks_by_author(
39 &self,
40 authority: AuthorityIndex,
41 start_round: Round,
42 ) -> ConsensusResult<Vec<VerifiedBlock>>;
43
44 fn scan_metrics(&self) -> ConsensusResult<Vec<(AuthorityIndex, StoredScoringMetricsU64)>>;
47
48 #[allow(dead_code)]
53 fn scan_last_blocks_by_author(
54 &self,
55 author: AuthorityIndex,
56 num_of_rounds: u64,
57 before_round: Option<Round>,
58 ) -> ConsensusResult<Vec<VerifiedBlock>>;
59
60 fn read_last_commit(&self) -> ConsensusResult<Option<TrustedCommit>>;
62
63 fn scan_commits(&self, range: CommitRange) -> ConsensusResult<Vec<TrustedCommit>>;
65
66 fn read_commit_votes(&self, commit_index: CommitIndex) -> ConsensusResult<Vec<BlockRef>>;
68
69 fn read_last_commit_info(&self) -> ConsensusResult<Option<(CommitRef, CommitInfo)>>;
71}
72
73#[derive(Debug, Default)]
75pub(crate) struct WriteBatch {
76 pub(crate) blocks: Vec<VerifiedBlock>,
77 pub(crate) commits: Vec<TrustedCommit>,
78 pub(crate) commit_info: Vec<(CommitRef, CommitInfo)>,
79 pub(crate) scoring_metrics: Vec<(AuthorityIndex, StoredScoringMetricsU64)>,
80}
81
82impl WriteBatch {
83 pub(crate) fn new(
84 blocks: Vec<VerifiedBlock>,
85 commits: Vec<TrustedCommit>,
86 commit_info: Vec<(CommitRef, CommitInfo)>,
87 scoring_metrics: Vec<(AuthorityIndex, StoredScoringMetricsU64)>,
88 ) -> Self {
89 WriteBatch {
90 blocks,
91 commits,
92 commit_info,
93 scoring_metrics,
94 }
95 }
96
97 #[cfg(test)]
100 pub(crate) fn blocks(mut self, blocks: Vec<VerifiedBlock>) -> Self {
101 self.blocks = blocks;
102 self
103 }
104
105 #[cfg(test)]
106 pub(crate) fn commits(mut self, commits: Vec<TrustedCommit>) -> Self {
107 self.commits = commits;
108 self
109 }
110
111 #[cfg(test)]
112 pub(crate) fn commit_info(mut self, commit_info: Vec<(CommitRef, CommitInfo)>) -> Self {
113 self.commit_info = commit_info;
114 self
115 }
116}