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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::{counters, logging::LogEntry, ConsensusState, Error, SafetyRules, TSafetyRules};
use consensus_types::{
block_data::BlockData,
timeout::Timeout,
timeout_2chain::{TwoChainTimeout, TwoChainTimeoutCertificate},
vote::Vote,
vote_proposal::MaybeSignedVoteProposal,
};
use diem_crypto::ed25519::Ed25519Signature;
use diem_infallible::RwLock;
use diem_types::{
epoch_change::EpochChangeProof,
ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
};
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>),
SignTimeoutWithQC(
Box<TwoChainTimeout>,
Box<Option<TwoChainTimeoutCertificate>>,
),
ConstructAndSignVoteTwoChain(
Box<MaybeSignedVoteProposal>,
Box<Option<TwoChainTimeoutCertificate>>,
),
SignCommitVote(Box<LedgerInfoWithSignatures>, Box<LedgerInfo>),
}
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 = serde_json::from_slice(&input_message)?;
let output = match input {
SafetyRulesInput::ConsensusState => {
serde_json::to_vec(&self.internal.consensus_state())
}
SafetyRulesInput::Initialize(li) => serde_json::to_vec(&self.internal.initialize(&li)),
SafetyRulesInput::ConstructAndSignVote(vote_proposal) => {
serde_json::to_vec(&self.internal.construct_and_sign_vote(&vote_proposal))
}
SafetyRulesInput::SignProposal(block_data) => {
serde_json::to_vec(&self.internal.sign_proposal(&block_data))
}
SafetyRulesInput::SignTimeout(timeout) => {
serde_json::to_vec(&self.internal.sign_timeout(&timeout))
}
SafetyRulesInput::SignTimeoutWithQC(timeout, maybe_tc) => serde_json::to_vec(
&self
.internal
.sign_timeout_with_qc(&timeout, maybe_tc.as_ref().as_ref()),
),
SafetyRulesInput::ConstructAndSignVoteTwoChain(vote_proposal, maybe_tc) => {
serde_json::to_vec(
&self.internal.construct_and_sign_vote_two_chain(
&vote_proposal,
maybe_tc.as_ref().as_ref(),
),
)
}
SafetyRulesInput::SignCommitVote(ledger_info, new_ledger_info) => serde_json::to_vec(
&self
.internal
.sign_commit_vote(*ledger_info, *new_ledger_info),
),
};
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)?;
serde_json::from_slice(&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())))?;
serde_json::from_slice(&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(),
)))?;
serde_json::from_slice(&response)?
}
fn sign_proposal(&mut self, block_data: &BlockData) -> Result<Ed25519Signature, Error> {
let _timer = counters::start_timer("external", LogEntry::SignProposal.as_str());
let response =
self.request(SafetyRulesInput::SignProposal(Box::new(block_data.clone())))?;
serde_json::from_slice(&response)?
}
fn sign_timeout(&mut self, timeout: &Timeout) -> Result<Ed25519Signature, Error> {
let _timer = counters::start_timer("external", LogEntry::SignTimeout.as_str());
let response = self.request(SafetyRulesInput::SignTimeout(Box::new(timeout.clone())))?;
serde_json::from_slice(&response)?
}
fn sign_timeout_with_qc(
&mut self,
timeout: &TwoChainTimeout,
timeout_cert: Option<&TwoChainTimeoutCertificate>,
) -> Result<Ed25519Signature, Error> {
let _timer = counters::start_timer("external", LogEntry::SignTimeoutWithQC.as_str());
let response = self.request(SafetyRulesInput::SignTimeoutWithQC(
Box::new(timeout.clone()),
Box::new(timeout_cert.cloned()),
))?;
serde_json::from_slice(&response)?
}
fn construct_and_sign_vote_two_chain(
&mut self,
vote_proposal: &MaybeSignedVoteProposal,
timeout_cert: Option<&TwoChainTimeoutCertificate>,
) -> Result<Vote, Error> {
let _timer =
counters::start_timer("external", LogEntry::ConstructAndSignVoteTwoChain.as_str());
let response = self.request(SafetyRulesInput::ConstructAndSignVoteTwoChain(
Box::new(vote_proposal.clone()),
Box::new(timeout_cert.cloned()),
))?;
serde_json::from_slice(&response)?
}
fn sign_commit_vote(
&mut self,
ledger_info: LedgerInfoWithSignatures,
new_ledger_info: LedgerInfo,
) -> Result<Ed25519Signature, Error> {
let _timer = counters::start_timer("external", LogEntry::SignCommitVote.as_str());
let response = self.request(SafetyRulesInput::SignCommitVote(
Box::new(ledger_info),
Box::new(new_ledger_info),
))?;
serde_json::from_slice(&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 = serde_json::to_vec(&input)?;
self.serializer_service
.write()
.handle_message(input_message)
}
}