1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use crate::{
    counters, logging::LogEntry, ConsensusState, Error, SafetyRules,
    TSafetyRules,
};
use consensus_types::{
    block::Block, block_data::BlockData, timeout::Timeout, vote::Vote,
    vote_proposal::MaybeSignedVoteProposal,
};
use diem_infallible::RwLock;
use diem_types::{
    epoch_change::EpochChangeProof, validator_config::ConsensusSignature,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum SafetyRulesInput {
    ConsensusState,
    Initialize(Box<EpochChangeProof>),
    ConstructAndSignVote(Box<MaybeSignedVoteProposal>),
    SignProposal(Box<BlockData>),
    SignTimeout(Box<Timeout>),
}

pub struct SerializerService {
    internal: SafetyRules,
}

impl SerializerService {
    pub fn new(internal: SafetyRules) -> Self { Self { internal } }

    pub fn handle_message(
        &mut self, input_message: Vec<u8>,
    ) -> Result<Vec<u8>, Error> {
        let input = bcs::from_bytes(&input_message)?;

        let output = match input {
            SafetyRulesInput::ConsensusState => {
                bcs::to_bytes(&self.internal.consensus_state())
            }
            SafetyRulesInput::Initialize(li) => {
                bcs::to_bytes(&self.internal.initialize(&li))
            }
            SafetyRulesInput::ConstructAndSignVote(vote_proposal) => {
                bcs::to_bytes(
                    &self.internal.construct_and_sign_vote(&vote_proposal),
                )
            }
            SafetyRulesInput::SignProposal(block_data) => {
                bcs::to_bytes(&self.internal.sign_proposal(*block_data))
            }
            SafetyRulesInput::SignTimeout(timeout) => {
                bcs::to_bytes(&self.internal.sign_timeout(&timeout))
            }
        };

        Ok(output?)
    }
}

pub struct SerializerClient {
    service: Box<dyn TSerializerClient>,
}

impl SerializerClient {
    pub fn new(serializer_service: Arc<RwLock<SerializerService>>) -> Self {
        let service = Box::new(LocalService { serializer_service });
        Self { service }
    }

    pub fn new_client(service: Box<dyn TSerializerClient>) -> Self {
        Self { service }
    }

    fn request(&mut self, input: SafetyRulesInput) -> Result<Vec<u8>, Error> {
        self.service.request(input)
    }
}

impl TSafetyRules for SerializerClient {
    fn consensus_state(&mut self) -> Result<ConsensusState, Error> {
        let _timer = counters::start_timer(
            "external",
            LogEntry::ConsensusState.as_str(),
        );
        let response = self.request(SafetyRulesInput::ConsensusState)?;
        bcs::from_bytes(&response)?
    }

    fn initialize(&mut self, proof: &EpochChangeProof) -> Result<(), Error> {
        let _timer =
            counters::start_timer("external", LogEntry::Initialize.as_str());
        let response = self
            .request(SafetyRulesInput::Initialize(Box::new(proof.clone())))?;
        bcs::from_bytes(&response)?
    }

    fn construct_and_sign_vote(
        &mut self, vote_proposal: &MaybeSignedVoteProposal,
    ) -> Result<Vote, Error> {
        let _timer = counters::start_timer(
            "external",
            LogEntry::ConstructAndSignVote.as_str(),
        );
        let response = self.request(SafetyRulesInput::ConstructAndSignVote(
            Box::new(vote_proposal.clone()),
        ))?;
        bcs::from_bytes(&response)?
    }

    fn sign_proposal(&mut self, block_data: BlockData) -> Result<Block, Error> {
        let _timer =
            counters::start_timer("external", LogEntry::SignProposal.as_str());
        let response =
            self.request(SafetyRulesInput::SignProposal(Box::new(block_data)))?;
        bcs::from_bytes(&response)?
    }

    fn sign_timeout(
        &mut self, timeout: &Timeout,
    ) -> Result<ConsensusSignature, Error> {
        let _timer =
            counters::start_timer("external", LogEntry::SignTimeout.as_str());
        let response = self.request(SafetyRulesInput::SignTimeout(
            Box::new(timeout.clone()),
        ))?;
        bcs::from_bytes(&response)?
    }
}

pub trait TSerializerClient: Send + Sync {
    fn request(&mut self, input: SafetyRulesInput) -> Result<Vec<u8>, Error>;
}

struct LocalService {
    pub serializer_service: Arc<RwLock<SerializerService>>,
}

impl TSerializerClient for LocalService {
    fn request(&mut self, input: SafetyRulesInput) -> Result<Vec<u8>, Error> {
        let input_message = bcs::to_bytes(&input)?;
        self.serializer_service
            .write()
            .handle_message(input_message)
    }
}