consensus_types/
executed_block.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::{
9    block::Block,
10    common::{Payload, Round},
11    quorum_cert::QuorumCert,
12    vote_proposal::{MaybeSignedVoteProposal, VoteProposal},
13};
14use diem_crypto::hash::HashValue;
15use diem_types::block_info::BlockInfo;
16use executor_types::StateComputeResult;
17use std::fmt::{Debug, Display, Formatter};
18
19/// ExecutedBlocks are managed in a speculative tree, the committed blocks form
20/// a chain. Besides block data, each executed block also has other derived meta
21/// data which could be regenerated from blocks.
22#[derive(Clone, Eq, PartialEq)]
23pub struct ExecutedBlock {
24    /// Block data that cannot be regenerated.
25    block: Block,
26    /// The state_compute_result is calculated for all the pending blocks prior
27    /// to insertion to the tree. The execution results are not persisted:
28    /// they're recalculated again for the pending blocks upon restart.
29    state_compute_result: StateComputeResult,
30}
31
32impl Debug for ExecutedBlock {
33    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
34        write!(f, "{}", self)
35    }
36}
37
38impl Display for ExecutedBlock {
39    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
40        write!(f, "{}", self.block())
41    }
42}
43
44impl ExecutedBlock {
45    pub fn new(block: Block, state_compute_result: StateComputeResult) -> Self {
46        Self {
47            block,
48            state_compute_result,
49        }
50    }
51
52    pub fn block(&self) -> &Block { &self.block }
53
54    pub fn id(&self) -> HashValue { self.block().id() }
55
56    pub fn epoch(&self) -> u64 { self.block.epoch() }
57
58    pub fn payload(&self) -> Option<&Payload> { self.block().payload() }
59
60    pub fn parent_id(&self) -> HashValue {
61        self.quorum_cert().certified_block().id()
62    }
63
64    pub fn quorum_cert(&self) -> &QuorumCert { self.block().quorum_cert() }
65
66    pub fn round(&self) -> Round { self.block().round() }
67
68    pub fn timestamp_usecs(&self) -> u64 { self.block().timestamp_usecs() }
69
70    pub fn compute_result(&self) -> &StateComputeResult {
71        &self.state_compute_result
72    }
73
74    pub fn block_info(&self) -> BlockInfo {
75        self.block().gen_block_info(
76            self.compute_result().root_hash(),
77            self.compute_result().version(),
78            self.compute_result().epoch_state().clone(),
79            self.compute_result().pivot_decision().clone(),
80        )
81    }
82
83    pub fn maybe_signed_vote_proposal(&self) -> MaybeSignedVoteProposal {
84        MaybeSignedVoteProposal {
85            vote_proposal: VoteProposal::new(
86                self.compute_result().extension_proof(),
87                self.block.clone(),
88                self.compute_result().epoch_state().clone(),
89                self.compute_result().pivot_decision().clone(),
90            ),
91            signature: self.compute_result().signature().clone(),
92        }
93    }
94}