consensus_types/
safety_data.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::vote::Vote;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12/// Data structure for safety rules to ensure consensus safety.
13#[derive(Debug, Deserialize, Eq, PartialEq, Serialize, Clone, Default)]
14pub struct SafetyData {
15    pub epoch: u64,
16    pub last_voted_round: u64,
17    pub preferred_round: u64,
18    pub last_vote: Option<Vote>,
19}
20
21impl SafetyData {
22    pub fn new(
23        epoch: u64, last_voted_round: u64, preferred_round: u64,
24        last_vote: Option<Vote>,
25    ) -> Self {
26        Self {
27            epoch,
28            last_voted_round,
29            preferred_round,
30            last_vote,
31        }
32    }
33}
34
35impl fmt::Display for SafetyData {
36    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
37        write!(
38            f,
39            "SafetyData: [epoch: {}, last_voted_round: {}, preferred_round: {}]",
40            self.epoch, self.last_voted_round, self.preferred_round
41        )
42    }
43}