consensus_core/storage/
mod.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4pub mod mem_store;
5pub mod rocksdb_store;
6
7#[cfg(test)]
8mod store_tests;
9
10use std::collections::BTreeMap;
11
12use consensus_config::AuthorityIndex;
13use consensus_types::block::{BlockRef, Round, TransactionIndex};
14
15use crate::{
16    CommitIndex,
17    block::VerifiedBlock,
18    commit::{CommitInfo, CommitRange, CommitRef, TrustedCommit},
19    error::ConsensusResult,
20};
21
22/// A common interface for consensus storage.
23pub trait Store: Send + Sync {
24    /// Writes blocks, consensus commits and other data to store atomically.
25    fn write(&self, write_batch: WriteBatch) -> ConsensusResult<()>;
26
27    /// Reads blocks for the given refs.
28    fn read_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<Option<VerifiedBlock>>>;
29
30    /// Checks if blocks exist in the store.
31    fn contains_blocks(&self, refs: &[BlockRef]) -> ConsensusResult<Vec<bool>>;
32
33    /// Reads blocks for an authority, from start_round.
34    fn scan_blocks_by_author(
35        &self,
36        authority: AuthorityIndex,
37        start_round: Round,
38    ) -> ConsensusResult<Vec<VerifiedBlock>>;
39
40    /// Reads blocks for an authority in [start_round, end_round), up to limit.
41    fn scan_blocks_by_author_in_range(
42        &self,
43        author: AuthorityIndex,
44        start_round: Round,
45        end_round: Round,
46        limit: usize,
47    ) -> ConsensusResult<Vec<VerifiedBlock>>;
48
49    // The method returns the last `num_of_rounds` rounds blocks by author in round ascending order.
50    // When a `before_round` is defined then the blocks of round `<=before_round` are returned. If not
51    // then the max value for round will be used as cut off.
52    fn scan_last_blocks_by_author(
53        &self,
54        author: AuthorityIndex,
55        num_of_rounds: u64,
56        before_round: Option<Round>,
57    ) -> ConsensusResult<Vec<VerifiedBlock>>;
58
59    /// Reads the last commit.
60    fn read_last_commit(&self) -> ConsensusResult<Option<TrustedCommit>>;
61
62    /// Reads all commits from start (inclusive) until end (inclusive).
63    fn scan_commits(&self, range: CommitRange) -> ConsensusResult<Vec<TrustedCommit>>;
64
65    /// Reads all blocks voting on a particular commit.
66    fn read_commit_votes(&self, commit_index: CommitIndex) -> ConsensusResult<Vec<BlockRef>>;
67
68    /// Reads the last commit info, written atomically with the last commit.
69    fn read_last_commit_info(&self) -> ConsensusResult<Option<(CommitRef, CommitInfo)>>;
70
71    /// Reads the last finalized commit.
72    fn read_last_finalized_commit(&self) -> ConsensusResult<Option<CommitRef>>;
73
74    // Reads rejected transactions by block for a given commit.
75    fn read_rejected_transactions(
76        &self,
77        commit_ref: CommitRef,
78    ) -> ConsensusResult<Option<BTreeMap<BlockRef, Vec<TransactionIndex>>>>;
79}
80
81/// Represents data to be written to the store together atomically.
82#[derive(Debug, Default)]
83pub struct WriteBatch {
84    pub blocks: Vec<VerifiedBlock>,
85    pub commits: Vec<TrustedCommit>,
86    pub commit_info: Vec<(CommitRef, CommitInfo)>,
87    pub finalized_commits: Vec<(CommitRef, BTreeMap<BlockRef, Vec<TransactionIndex>>)>,
88}
89
90impl WriteBatch {
91    pub fn new(
92        blocks: Vec<VerifiedBlock>,
93        commits: Vec<TrustedCommit>,
94        commit_info: Vec<(CommitRef, CommitInfo)>,
95        finalized_commits: Vec<(CommitRef, BTreeMap<BlockRef, Vec<TransactionIndex>>)>,
96    ) -> Self {
97        WriteBatch {
98            blocks,
99            commits,
100            commit_info,
101            finalized_commits,
102        }
103    }
104
105    // Test setters.
106
107    #[cfg(test)]
108    pub fn blocks(mut self, blocks: Vec<VerifiedBlock>) -> Self {
109        self.blocks = blocks;
110        self
111    }
112
113    #[cfg(test)]
114    pub fn commits(mut self, commits: Vec<TrustedCommit>) -> Self {
115        self.commits = commits;
116        self
117    }
118
119    #[cfg(test)]
120    pub fn commit_info(mut self, commit_info: Vec<(CommitRef, CommitInfo)>) -> Self {
121        self.commit_info = commit_info;
122        self
123    }
124}