safety_rules/t_safety_rules.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::{ConsensusState, Error};
9use consensus_types::{
10 block::Block, block_data::BlockData, timeout::Timeout, vote::Vote,
11 vote_proposal::MaybeSignedVoteProposal,
12};
13use diem_types::{
14 epoch_change::EpochChangeProof, validator_config::ConsensusSignature,
15};
16
17/// Interface for SafetyRules
18pub trait TSafetyRules {
19 /// Provides the internal state of SafetyRules for monitoring / debugging
20 /// purposes. This does not include sensitive data like private keys.
21 fn consensus_state(&mut self) -> Result<ConsensusState, Error>;
22
23 /// Initialize SafetyRules using an Epoch ending LedgerInfo, this should map
24 /// to what was provided in consensus_state. It will be used to
25 /// initialize the ValidatorSet. This uses a EpochChangeProof because
26 /// there's a possibility that consensus migrated to a new epoch but
27 /// SafetyRules did not.
28 fn initialize(&mut self, proof: &EpochChangeProof) -> Result<(), Error>;
29
30 /// Attempts to vote for a given proposal following the voting rules.
31 fn construct_and_sign_vote(
32 &mut self, vote_proposal: &MaybeSignedVoteProposal,
33 ) -> Result<Vote, Error>;
34
35 /// As the holder of the private key, SafetyRules also signs proposals or
36 /// blocks. A Block is a signed BlockData along with some additional
37 /// metadata.
38 fn sign_proposal(&mut self, block_data: BlockData) -> Result<Block, Error>;
39
40 /// As the holder of the private key, SafetyRules also signs what is
41 /// effectively a timeout message. This returns the signature for that
42 /// timeout message.
43 fn sign_timeout(
44 &mut self, timeout: &Timeout,
45 ) -> Result<ConsensusSignature, Error>;
46
47 /// Allow the safety rule to start voting with saved secure data from
48 /// another node.
49 fn start_voting(&mut self, _initialize: bool) -> Result<(), Error> {
50 Err(Error::SecureStorageUnexpectedError(
51 "unsupported safety rule type".to_string(),
52 ))
53 }
54
55 /// Stop the safety rule from voting and save secure data.
56 fn stop_voting(&mut self) -> Result<(), Error> {
57 Err(Error::SecureStorageUnexpectedError(
58 "unsupported safety rule type".to_string(),
59 ))
60 }
61}