consensus_types/
vote_proposal.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::block::Block;
9use diem_crypto::hash::TransactionAccumulatorHasher;
10use diem_crypto_derive::{BCSCryptoHash, CryptoHasher};
11use diem_types::{
12    block_info::PivotBlockDecision, epoch_state::EpochState,
13    proof::AccumulatorExtensionProof, validator_config::ConsensusSignature,
14};
15use serde::{Deserialize, Serialize};
16use std::{
17    fmt::{Display, Formatter},
18    ops::Deref,
19};
20
21/// This structure contains all the information needed by safety rules to
22/// evaluate a proposal / block for correctness / safety and to produce a Vote.
23#[derive(Clone, Debug, CryptoHasher, Deserialize, BCSCryptoHash, Serialize)]
24pub struct VoteProposal {
25    /// Contains the data necessary to construct the parent's execution output
26    /// state and the childs in a verifiable way
27    accumulator_extension_proof:
28        AccumulatorExtensionProof<TransactionAccumulatorHasher>,
29    /// The block / proposal to evaluate
30    #[serde(bound(deserialize = "Block: Deserialize<'de>"))]
31    block: Block,
32    /// An optional field containing the next epoch info.
33    next_epoch_state: Option<EpochState>,
34    /// The pivot decision after `block` is executed.
35    pivot_decision: Option<PivotBlockDecision>,
36}
37
38impl VoteProposal {
39    pub fn new(
40        accumulator_extension_proof: AccumulatorExtensionProof<
41            TransactionAccumulatorHasher,
42        >,
43        block: Block, next_epoch_state: Option<EpochState>,
44        pivot_decision: Option<PivotBlockDecision>,
45    ) -> Self {
46        Self {
47            accumulator_extension_proof,
48            block,
49            next_epoch_state,
50            pivot_decision,
51        }
52    }
53
54    pub fn accumulator_extension_proof(
55        &self,
56    ) -> &AccumulatorExtensionProof<TransactionAccumulatorHasher> {
57        &self.accumulator_extension_proof
58    }
59
60    pub fn block(&self) -> &Block { &self.block }
61
62    pub fn next_epoch_state(&self) -> Option<&EpochState> {
63        self.next_epoch_state.as_ref()
64    }
65
66    pub fn pivot_decision(&self) -> &Option<PivotBlockDecision> {
67        &self.pivot_decision
68    }
69}
70
71impl Display for VoteProposal {
72    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
73        write!(f, "VoteProposal[block: {}]", self.block,)
74    }
75}
76
77/// Wraps a vote_proposal and its signature.
78#[derive(Clone, Debug, Deserialize, Serialize)]
79pub struct MaybeSignedVoteProposal {
80    /// The vote proposal to be signed.
81    pub vote_proposal: VoteProposal,
82
83    /// The signature of this proposal's hash from Diem Execution Correctness
84    /// service. It is an `Option` because the LEC can be configured to not
85    /// sign the vote hash.
86    pub signature: Option<ConsensusSignature>,
87}
88
89impl Deref for MaybeSignedVoteProposal {
90    type Target = VoteProposal;
91
92    fn deref(&self) -> &VoteProposal { &self.vote_proposal }
93}