1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use consensus_types::{
    executed_block::ExecutedBlock, quorum_cert::QuorumCert,
    timeout_certificate::TimeoutCertificate,
};
use diem_crypto::HashValue;
use std::sync::Arc;

mod block_store;
mod block_tree;
pub mod tracing;

pub use block_store::{sync_manager::BlockRetriever, BlockStore};
use consensus_types::{block::Block, sync_info::SyncInfo};

pub trait BlockReader: Send + Sync {
    /// Check if a block with the block_id exist in the BlockTree.
    fn block_exists(&self, block_id: HashValue) -> bool;

    /// Try to get a block with the block_id, return an Arc of it if found.
    fn get_block(&self, block_id: HashValue) -> Option<Arc<ExecutedBlock>>;

    fn get_ledger_block(
        &self, block_id: &HashValue,
    ) -> anyhow::Result<Option<Block>>;

    /// Get the current root block of the BlockTree.
    fn root(&self) -> Arc<ExecutedBlock>;

    fn get_quorum_cert_for_block(
        &self, block_id: HashValue,
    ) -> Option<Arc<QuorumCert>>;

    /// Returns all the blocks between the root and the given block, including
    /// the given block but excluding the root.
    /// In case a given block is not the successor of the root, return None.
    /// For example if a tree is b0 <- b1 <- b2 <- b3, then
    /// path_from_root(b2) -> Some([b2, b1])
    /// path_from_root(b0) -> Some([])
    /// path_from_root(a) -> None
    fn path_from_root(
        &self, block_id: HashValue,
    ) -> Option<Vec<Arc<ExecutedBlock>>>;

    /// Return the certified block with the highest round.
    #[allow(dead_code)]
    fn highest_certified_block(&self) -> Arc<ExecutedBlock>;

    /// Return the quorum certificate with the highest round
    fn highest_quorum_cert(&self) -> Arc<QuorumCert>;

    /// Return the quorum certificate that carries ledger info with the highest
    /// round
    fn highest_commit_cert(&self) -> Arc<QuorumCert>;

    /// Return the highest timeout certificate if available.
    fn highest_timeout_cert(&self) -> Option<Arc<TimeoutCertificate>>;

    /// Return the combination of highest quorum cert, timeout cert and commit
    /// cert.
    fn sync_info(&self) -> SyncInfo;
}