consensus_types/vote_msg.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::{sync_info::SyncInfo, vote::Vote};
9use anyhow::ensure;
10use diem_crypto::HashValue;
11use diem_types::validator_verifier::ValidatorVerifier;
12use serde::{Deserialize, Serialize};
13use std::fmt::{Display, Formatter};
14
15/// VoteMsg is the struct that is ultimately sent by the voter in response for
16/// receiving a proposal.
17/// VoteMsg carries the `LedgerInfo` of a block that is going to be committed in
18/// case this vote is gathers QuorumCertificate (see the detailed explanation in
19/// the comments of `LedgerInfo`).
20#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
21pub struct VoteMsg {
22 /// The container for the vote (VoteData, LedgerInfo, Signature)
23 vote: Vote,
24 /// Sync info carries information about highest QC, TC and LedgerInfo
25 sync_info: SyncInfo,
26}
27
28impl Display for VoteMsg {
29 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
30 write!(f, "VoteMsg: [{}]", self.vote,)
31 }
32}
33
34impl VoteMsg {
35 pub fn new(vote: Vote, sync_info: SyncInfo) -> Self {
36 Self { vote, sync_info }
37 }
38
39 /// Container for actual voting material
40 pub fn vote(&self) -> &Vote { &self.vote }
41
42 /// SyncInfo of the given vote message
43 pub fn sync_info(&self) -> &SyncInfo { &self.sync_info }
44
45 pub fn epoch(&self) -> u64 { self.vote.epoch() }
46
47 pub fn proposed_block_id(&self) -> HashValue {
48 self.vote.vote_data().proposed().id()
49 }
50
51 pub fn verify(&self, validator: &ValidatorVerifier) -> anyhow::Result<()> {
52 ensure!(
53 self.vote().epoch() == self.sync_info.epoch(),
54 "VoteMsg has different epoch"
55 );
56 // We're not verifying SyncInfo here yet: we are going to verify it only
57 // in case we need it. This way we avoid verifying O(n) SyncInfo
58 // messages while aggregating the votes (O(n^2) signature
59 // verifications).
60 self.vote().verify(validator)
61 }
62}