safety_rules/
local_client.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, SafetyRules, TSafetyRules};
9use consensus_types::{
10    block::Block, block_data::BlockData, timeout::Timeout, vote::Vote,
11    vote_proposal::MaybeSignedVoteProposal,
12};
13use diem_infallible::RwLock;
14use diem_types::{
15    epoch_change::EpochChangeProof, validator_config::ConsensusSignature,
16};
17use std::sync::Arc;
18
19/// A local interface into SafetyRules. Constructed in such a way that the
20/// container / caller cannot distinguish this API from an actual client/server
21/// process without being exposed to the actual container instead the caller can
22/// access a Box<dyn TSafetyRules>.
23pub struct LocalClient {
24    internal: Arc<RwLock<SafetyRules>>,
25}
26
27impl LocalClient {
28    pub fn new(internal: Arc<RwLock<SafetyRules>>) -> Self { Self { internal } }
29}
30
31impl TSafetyRules for LocalClient {
32    fn consensus_state(&mut self) -> Result<ConsensusState, Error> {
33        self.internal.write().consensus_state()
34    }
35
36    fn initialize(&mut self, proof: &EpochChangeProof) -> Result<(), Error> {
37        self.internal.write().initialize(proof)
38    }
39
40    fn construct_and_sign_vote(
41        &mut self, vote_proposal: &MaybeSignedVoteProposal,
42    ) -> Result<Vote, Error> {
43        self.internal.write().construct_and_sign_vote(vote_proposal)
44    }
45
46    fn sign_proposal(&mut self, block_data: BlockData) -> Result<Block, Error> {
47        self.internal.write().sign_proposal(block_data)
48    }
49
50    fn sign_timeout(
51        &mut self, timeout: &Timeout,
52    ) -> Result<ConsensusSignature, Error> {
53        self.internal.write().sign_timeout(timeout)
54    }
55
56    fn start_voting(&mut self, initialize: bool) -> Result<(), Error> {
57        self.internal.write().start_voting(initialize)
58    }
59
60    fn stop_voting(&mut self) -> Result<(), Error> {
61        self.internal.write().stop_voting()
62    }
63}