consensus_types/
timeout.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::common::Round;
9use diem_crypto_derive::{BCSCryptoHash, CryptoHasher};
10use diem_types::{
11    validator_config::ConsensusSignature, validator_signer::ValidatorSigner,
12};
13use serde::{Deserialize, Serialize};
14use std::fmt::{Display, Formatter};
15
16/// This structure contains all the information necessary to construct a
17/// signature on the equivalent of a timeout message
18#[derive(
19    Clone,
20    Debug,
21    Deserialize,
22    Eq,
23    PartialEq,
24    Serialize,
25    CryptoHasher,
26    BCSCryptoHash,
27)]
28pub struct Timeout {
29    /// Epoch number corresponds to the set of validators that are active for
30    /// this round.
31    epoch: u64,
32    /// The consensus protocol executes proposals (blocks) in rounds, which
33    /// monotically increase per epoch.
34    round: Round,
35}
36
37impl Timeout {
38    pub fn new(epoch: u64, round: Round) -> Self { Self { epoch, round } }
39
40    pub fn epoch(&self) -> u64 { self.epoch }
41
42    pub fn round(&self) -> Round { self.round }
43
44    pub fn sign(&self, signer: &ValidatorSigner) -> ConsensusSignature {
45        signer.sign(self)
46    }
47}
48
49impl Display for Timeout {
50    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
51        write!(f, "Timeout: [epoch: {}, round: {}]", self.epoch, self.round,)
52    }
53}