consensus_core/storage/
mod.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5#[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
21/// A common interface for consensus storage.
22pub(crate) trait Store: Send + Sync {
23    /// Writes blocks, consensus commits and other data to store atomically.
24    fn write(&self, write_batch: WriteBatch) -> ConsensusResult<()>;
25
26    /// Reads blocks for the given refs.
27    fn read_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<Option<VerifiedBlock>>>;
28
29    /// Checks if blocks exist in the store.
30    fn contains_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<bool>>;
31
32    /// Checks whether there is any block at the given slot
33    #[allow(dead_code)]
34    fn contains_block_at_slot(&self, slot: crate::block::Slot) -> ConsensusResult<bool>;
35
36    /// Reads blocks for an authority, from start_round.
37    fn scan_blocks_by_author(
38        &self,
39        authority: AuthorityIndex,
40        start_round: Round,
41    ) -> ConsensusResult<Vec<VerifiedBlock>>;
42
43    // The method returns the last `num_of_rounds` rounds blocks by author in round
44    // ascending order. When a `before_round` is defined then the blocks of
45    // round `<=before_round` are returned. If not then the max value for round
46    // will be used as cut off.
47    #[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    /// Reads the last commit.
56    fn read_last_commit(&self) -> ConsensusResult<Option<TrustedCommit>>;
57
58    /// Reads all commits from start (inclusive) until end (inclusive).
59    fn scan_commits(&self, range: CommitRange) -> ConsensusResult<Vec<TrustedCommit>>;
60
61    /// Reads all blocks voting on a particular commit.
62    fn read_commit_votes(&self, commit_index: CommitIndex) -> ConsensusResult<Vec<BlockRef>>;
63
64    /// Reads the last commit info, written atomically with the last commit.
65    fn read_last_commit_info(&self) -> ConsensusResult<Option<(CommitRef, CommitInfo)>>;
66}
67
68/// Represents data to be written to the store together atomically.
69#[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    // Test setters.
90
91    #[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}