-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsession.rs
215 lines (197 loc) · 8.51 KB
/
session.rs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Copyright (c) 2018-2021 Iqlusion Inc. (licensed under the Apache License, Version 2.0)
//! Modifications Copyright (c) 2021-present Crypto.com (licensed under the Apache License, Version 2.0)
use crate::{
chain::state::{PersistStateSync, State, StateError, StateErrorDetail},
config::validator::ValidatorConfig,
connection::Connection,
error::Error,
rpc::{ChainIdErrorType, DoubleSignErrorType, Request, Response},
};
use ed25519_dalek::{Keypair, Signer};
use std::time::Instant;
use tendermint_proto::privval::PingResponse;
use tracing::{debug, error, info};
/// Encrypted or plain session with a validator node
pub struct Session<S: PersistStateSync> {
/// Validator configuration options
config: ValidatorConfig,
/// connection to a validator node
connection: Box<dyn Connection>,
/// consensus signing key
signing_key: Keypair,
/// consensus state
state: State,
/// consensus state persistence
state_syncer: S,
}
impl<S: PersistStateSync> Session<S> {
pub fn reset_connection(&mut self, connection: Box<dyn Connection>) {
self.connection = connection;
}
pub fn new(
config: ValidatorConfig,
connection: Box<dyn Connection>,
signing_key: Keypair,
state: State,
state_syncer: S,
) -> Self {
Self {
config,
connection,
signing_key,
state,
state_syncer,
}
}
/// Check chain id matches the configured one
fn check_chain_id(&self, chain_id: &tendermint::chain::Id) -> Result<(), Error> {
if chain_id == &self.config.chain_id {
Ok(())
} else {
Err(Error::chain_id_error(chain_id.to_string()))
}
}
/// If a max block height is configured, ensure the block we're signing
/// doesn't exceed it
fn check_max_height(&self, request_height: i64) -> Result<(), Error> {
if let Some(max_height) = self.config.max_height {
if request_height > max_height.value() as i64 {
return Err(Error::exceed_max_height(request_height, max_height.into()));
}
}
Ok(())
}
/// Main request loop
pub fn request_loop(&mut self) -> Result<(), Error> {
while self.handle_request()? {}
Ok(())
}
/// Handle an incoming request from the validator
fn handle_request(&mut self) -> Result<bool, Error> {
let request = Request::read(&mut self.connection)?;
debug!(
"[{}] received request: {:?}",
&self.config.chain_id, &request
);
let response = match request {
Request::SignProposal(req) => {
if self.check_chain_id(&req.chain_id).is_err() {
Response::invalid_chain_id(ChainIdErrorType::Proposal, &req.chain_id)
} else {
self.check_max_height(req.proposal.height.into())?;
let request_state = State::from(req.clone());
let req_cs = request_state.consensus_state();
match self
.state
.check_update_consensus_state(req_cs.clone(), &mut self.state_syncer)
{
Ok(_) => {
let signable_bytes = req.to_signable_vec().map_err(|e| {
Error::signing_tendermint_error(
"can't get proposal signable bytes".into(),
e,
)
})?;
let started_at = Instant::now();
let signature = self.signing_key.sign(&signable_bytes);
info!(
"[{}] signed:{} at h/r/s {} ({} ms)",
&self.config.chain_id,
req_cs.block_id_prefix(),
req_cs,
started_at.elapsed().as_millis(),
);
Response::proposal_response(req, signature)
}
Err(StateError(StateErrorDetail::DoubleSignError(_), _)) => {
// Report double signing error back to the validator
let original_block_id = self.state.consensus_state().block_id_prefix();
error!(
"[{}] attempted double sign at h/r/s: {} ({} != {})",
&self.config.chain_id,
req_cs,
original_block_id,
req_cs.block_id_prefix()
);
Response::double_sign(
DoubleSignErrorType::Proposal,
req_cs.height.into(),
)
}
Err(e) => {
return Err(Error::signing_state_error(
"failed signing proposal".into(),
e,
))
}
}
}
}
Request::SignVote(req) => {
if self.check_chain_id(&req.chain_id).is_err() {
Response::invalid_chain_id(ChainIdErrorType::Vote, &req.chain_id)
} else {
self.check_max_height(req.vote.height.into())?;
let request_state = State::from(req.clone());
let req_cs = request_state.consensus_state();
match self
.state
.check_update_consensus_state(req_cs.clone(), &mut self.state_syncer)
{
Ok(_) => {
let signable_bytes = req.to_signable_vec().map_err(|e| {
Error::signing_tendermint_error(
"cannot get vote signable bytes".into(),
e,
)
})?;
let started_at = Instant::now();
let signature = self.signing_key.sign(&signable_bytes);
info!(
"[{}] signed:{} at h/r/s {} ({} ms)",
&self.config.chain_id,
req_cs.block_id_prefix(),
req_cs,
started_at.elapsed().as_millis(),
);
Response::vote_response(req, signature)
}
Err(StateError(StateErrorDetail::DoubleSignError(_), _)) => {
// Report double signing error back to the validator
let original_block_id = self.state.consensus_state().block_id_prefix();
error!(
"[{}] attempted double sign at h/r/s: {} ({} != {})",
&self.config.chain_id,
req_cs,
original_block_id,
req_cs.block_id_prefix()
);
Response::double_sign(DoubleSignErrorType::Vote, req_cs.height.into())
}
Err(e) => {
return Err(Error::signing_state_error("failed signing vote".into(), e))
}
}
}
}
// non-signable requests:
Request::ReplyPing(_) => Response::Ping(PingResponse {}),
Request::ShowPublicKey(ref req) => {
if self.check_chain_id(&req.chain_id).is_err() {
Response::invalid_chain_id(ChainIdErrorType::Pubkey, &req.chain_id)
} else {
Response::PublicKey(self.signing_key.public.into())
}
}
};
debug!(
"[{}] sending response: {:?}",
&self.config.chain_id, &response
);
let response_bytes = response.encode()?;
self.connection
.write_all(&response_bytes)
.map_err(|e| Error::io_error("write response failed".into(), e))?;
Ok(true)
}
}