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    // The method returns the last `num_of_rounds` rounds blocks by author in round ascending order.
41    // When a `before_round` is defined then the blocks of round `<=before_round` are returned. If not
42    // then the max value for round will be used as cut off.
43    fn scan_last_blocks_by_author(
44        &self,
45        author: AuthorityIndex,
46        num_of_rounds: u64,
47        before_round: Option<Round>,
48    ) -> ConsensusResult<Vec<VerifiedBlock>>;
49
50    /// Reads the last commit.
51    fn read_last_commit(&self) -> ConsensusResult<Option<TrustedCommit>>;
52
53    /// Reads all commits from start (inclusive) until end (inclusive).
54    fn scan_commits(&self, range: CommitRange) -> ConsensusResult<Vec<TrustedCommit>>;
55
56    /// Reads all blocks voting on a particular commit.
57    fn read_commit_votes(&self, commit_index: CommitIndex) -> ConsensusResult<Vec<BlockRef>>;
58
59    /// Reads the last commit info, written atomically with the last commit.
60    fn read_last_commit_info(&self) -> ConsensusResult<Option<(CommitRef, CommitInfo)>>;
61
62    /// Reads the last finalized commit.
63    fn read_last_finalized_commit(&self) -> ConsensusResult<Option<CommitRef>>;
64
65    // Reads rejected transactions by block for a given commit.
66    fn read_rejected_transactions(
67        &self,
68        commit_ref: CommitRef,
69    ) -> ConsensusResult<Option<BTreeMap<BlockRef, Vec<TransactionIndex>>>>;
70}
71
72/// Represents data to be written to the store together atomically.
73#[derive(Debug, Default)]
74pub struct WriteBatch {
75    pub blocks: Vec<VerifiedBlock>,
76    pub commits: Vec<TrustedCommit>,
77    pub commit_info: Vec<(CommitRef, CommitInfo)>,
78    pub finalized_commits: Vec<(CommitRef, BTreeMap<BlockRef, Vec<TransactionIndex>>)>,
79}
80
81impl WriteBatch {
82    pub fn new(
83        blocks: Vec<VerifiedBlock>,
84        commits: Vec<TrustedCommit>,
85        commit_info: Vec<(CommitRef, CommitInfo)>,
86        finalized_commits: Vec<(CommitRef, BTreeMap<BlockRef, Vec<TransactionIndex>>)>,
87    ) -> Self {
88        WriteBatch {
89            blocks,
90            commits,
91            commit_info,
92            finalized_commits,
93        }
94    }
95
96    // Test setters.
97
98    #[cfg(test)]
99    pub fn blocks(mut self, blocks: Vec<VerifiedBlock>) -> Self {
100        self.blocks = blocks;
101        self
102    }
103
104    #[cfg(test)]
105    pub fn commits(mut self, commits: Vec<TrustedCommit>) -> Self {
106        self.commits = commits;
107        self
108    }
109
110    #[cfg(test)]
111    pub fn commit_info(mut self, commit_info: Vec<(CommitRef, CommitInfo)>) -> Self {
112        self.commit_info = commit_info;
113        self
114    }
115}