-
Notifications
You must be signed in to change notification settings - Fork 122
/
tx_signer.rs
397 lines (326 loc) · 12.9 KB
/
tx_signer.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//! Transaction signer.
//!
//! Connects to a remote service to obtain transactions to sign, and if they
//! meet a prescribed policy, signs them.
pub mod jsonrpc;
pub mod last_tx;
pub mod sequence_file;
pub mod sign_msg;
pub mod tx_request;
pub use tx_request::TxSigningRequest;
use self::{last_tx::LastTx, sign_msg::SignMsg};
use crate::{
chain,
config::tx_signer::{PollInterval, TxAcl, TxSignerConfig, TxSource},
error::{Error, ErrorKind},
prelude::*,
};
use abscissa_tokio::tokio;
use sequence_file::SequenceFile;
use std::process;
use stdtx::amino;
use subtle_encoding::hex;
use tendermint_rpc::{endpoint::status, Client};
use tokio::time;
/// Frequency at which to retry after failures
// TODO(tarcieri): make this configurable?
pub const RETRY_DELAY: time::Duration = time::Duration::from_secs(5);
/// RPC polling interval
// TODO(tarcieri): use websocket instead of polling? make this configurable?
pub const RPC_POLL_INTERVAL: time::Duration = time::Duration::from_millis(500);
/// Transaction signer
pub struct TxSigner {
/// Chain ID of the Tendermint network this validator is part of
chain_id: tendermint::chain::Id,
/// Transaction builder
tx_builder: amino::Builder,
/// Account address
address: stdtx::Address,
/// Arbitrary context string to pass to transaction source
context: String,
/// Access Control List for authorized transaaction types to sign
acl: TxAcl,
/// Polling interval
poll_interval: PollInterval,
/// Transaction source (JSONRPC)
// TODO(tarcieri): gRPC
source: jsonrpc::Client,
/// Tendermint RPC client
rpc_client: tendermint_rpc::HttpClient,
/// Sequence file
seq_file: SequenceFile,
/// State of the last broadcasted transaction
last_tx: LastTx,
}
impl TxSigner {
/// Create a new transaction signer
pub fn new(config: &TxSignerConfig) -> Result<Self, Error> {
let schema = amino::Schema::load_toml(&config.schema).unwrap_or_else(|e| {
status_err!(
"couldn't read TX schema from `{}`: {}",
config.schema.display(),
e
);
process::exit(1);
});
let tx_builder =
amino::Builder::new(schema, config.chain_id.to_string(), config.account_number);
let source = match &config.source {
TxSource::JsonRpc { uri } => jsonrpc::Client::new(uri.clone()),
};
let tendermint_rpc = tendermint_rpc::HttpClient::new(config.rpc.addr.clone())?;
let seq_file = SequenceFile::open(&config.seq_file)?;
Ok(Self {
chain_id: config.chain_id.clone(),
tx_builder,
address: config.account_address,
context: config.context.clone(),
acl: config.acl.clone(),
poll_interval: config.poll_interval.clone(),
source,
rpc_client: tendermint_rpc,
seq_file,
last_tx: LastTx::default(),
})
}
/// Run the transaction signer
pub async fn run(&mut self) {
// Fetch the block height via RPC and use that to synchronize the
// block interval to the block height count
let mut next_block = loop {
match self.rpc_client.status().await {
Ok(status) => {
break self.next_block_after(status.sync_info.latest_block_height.value())
}
Err(e) => {
warn!(
"[{}] error getting initial block height: {}",
self.chain_id, e
);
time::sleep(RETRY_DELAY).await
}
}
};
loop {
info!(
"[{}] waiting until block height: {}",
&self.chain_id, next_block
);
let status = match self.wait_until_block_height(next_block).await {
Ok(status) => status,
Err(e) => {
error!(
"[{}] couldn't get current block height via RPC: {}",
&self.chain_id, e
);
time::sleep(RETRY_DELAY).await;
continue;
}
};
let current_block_height = status.sync_info.latest_block_height.value();
next_block = self.next_block_after(current_block_height);
if let Err(e) = self.request_and_sign_tx(status).await {
error!("[{}] {} - {}", &self.chain_id, self.source.uri(), e);
}
}
}
/// Wait until the chain is at the given block height
async fn wait_until_block_height(
&mut self,
target_height: u64,
) -> Result<status::Response, Error> {
let (block_interval, min_secs) = match self.poll_interval {
PollInterval::Block { blocks, min_secs } => (blocks, min_secs),
};
let min_deadline = time::Instant::now() + time::Duration::from_secs(min_secs);
loop {
let status = self.rpc_client.status().await?;
let current_height = status.sync_info.latest_block_height.value();
debug!(
"[{}] current block height is: {}",
&self.chain_id, current_height
);
if current_height >= target_height {
if time::Instant::now() < min_deadline {
warn!(
"[{}] target height {} reached before min_secs deadline ({}s)! \
sleeping... (is node catching up?)",
&self.chain_id, target_height, min_secs
);
time::sleep_until(min_deadline).await;
}
return Ok(status);
} else if target_height.checked_sub(current_height).unwrap() > block_interval {
warn!(
"block wait sanity check failed: current={} target={} interval={}",
current_height, target_height, block_interval
);
// Hopefully returning the current status will sync us back up if this ever happens
return Ok(status);
}
time::sleep(RPC_POLL_INTERVAL).await
}
}
/// Get the next block we should wait for after the provided block height
/// according ot the internally configured block interval
fn next_block_after(&self, block_height: u64) -> u64 {
let block_interval = match self.poll_interval {
PollInterval::Block { blocks, .. } => blocks,
};
block_height
.checked_sub(block_height % block_interval)
.unwrap()
.checked_add(block_interval)
.unwrap()
}
/// Request a transaction to be signed from the transaction source
async fn request_and_sign_tx(&mut self, status: status::Response) -> Result<(), Error> {
let params = jsonrpc::Request {
network: self.chain_id.clone(),
context: self.context.clone(),
status: status.sync_info,
last_tx_response: Option::from(&self.last_tx),
};
let tx_req = match self.source.request(params).await? {
Some(req) => req,
None => return Ok(()),
};
if self.chain_id.as_str() != tx_req.chain_id {
fail!(
ErrorKind::ChainIdError,
"expected `{}`, got `{}`",
self.chain_id,
tx_req.chain_id
);
}
let seq = self.seq_file.sequence();
let sign_msg = SignMsg::new(&tx_req, &self.tx_builder, seq)?;
// If no transaction broadcasted successfully before, retry with a
// higher sequence number to see if we're out-of-sync
// TODO(tarcieri): handle these errors by querying sequence number via RPC
let retry_on_failure = !self.last_tx.is_response();
if let Err(e) = self.broadcast_tx(sign_msg, seq).await {
error!("[{}] {} - {}", &self.chain_id, self.source.uri(), e);
// If the last transaction errored, speculatively try the next
// sequence number, as the previous transaction may have been
// successfully broadcast but we never got a response.
// TODO(tarcieri): replace this by resynchronizing the sequence number
if retry_on_failure {
let seq = seq.checked_add(1).unwrap();
warn!(
"[{}] {} - retrying transaction at sequence {}",
&self.chain_id,
self.source.uri(),
seq
);
let sign_msg = SignMsg::new(&tx_req, &self.tx_builder, seq)?;
if let Err(e) = self.broadcast_tx(sign_msg, seq).await {
error!("[{}] {} - {}", &self.chain_id, self.source.uri(), e);
// Try a third time for good measure
// If we wanted to generalize this, it could use a loop,
// but instead of that it'd be better to implement
// self-healing by consulting the sequence from the chain
// itself once we can use e.g. gRPC to do to that.
let seq = seq.checked_add(1).unwrap();
warn!(
"[{}] {} - retrying transaction at sequence {}",
&self.chain_id,
self.source.uri(),
seq
);
let sign_msg = SignMsg::new(&tx_req, &self.tx_builder, seq)?;
self.broadcast_tx(sign_msg, seq).await?;
}
}
}
Ok(())
}
/// Broadcast signed transaction to the Tendermint P2P network via RPC
async fn broadcast_tx(&mut self, sign_msg: SignMsg, sequence: u64) -> Result<(), Error> {
let tx = self.sign_tx(&sign_msg)?;
let amino_tx = tendermint::abci::Transaction::from(
tx.to_amino_bytes(self.tx_builder.schema().namespace()),
);
let amino_tx_hex =
String::from_utf8(hex::encode(amino_tx.as_ref())).expect("hex should always be UTF-8");
info!(
"[{}] broadcasting TX: {}",
self.chain_id,
amino_tx_hex.to_ascii_uppercase()
);
let response = match self.rpc_client.broadcast_tx_commit(amino_tx).await {
Ok(resp) => {
self.last_tx = LastTx::Response(Box::new(resp.clone()));
resp
}
Err(e) => {
self.last_tx = LastTx::Error(e.clone());
return Err(e.into());
}
};
if response.check_tx.code.is_err() {
fail!(
ErrorKind::TendermintError,
"TX broadcast failed: {} (CheckTx code={})",
response.check_tx.log,
response.check_tx.code.value(),
);
}
// If CheckTx succeeds the sequence number always needs to be
// incremented, even if DeliverTx subsequently fails
self.seq_file.persist(sequence.checked_add(1).unwrap())?;
if response.deliver_tx.code.is_err() {
fail!(
ErrorKind::TendermintError,
"TX broadcast failed: {} (DeliverTx code={}, hash={})",
response.deliver_tx.log,
response.deliver_tx.code.value(),
response.hash
);
}
info!(
"[{}] successfully broadcast TX {} (shash={})",
self.chain_id,
self.seq_file.sequence(),
response.hash
);
Ok(())
}
fn sign_tx(&self, sign_msg: &SignMsg) -> Result<amino::StdTx, Error> {
sign_msg.authorize(&self.acl)?;
let registry = chain::REGISTRY.get();
let chain = registry.get_chain(&self.chain_id).unwrap_or_else(|| {
panic!("chain '{}' missing from registry!", &self.chain_id);
});
debug!("[{}] performing signature", &self.chain_id);
let account_id = tendermint::account::Id::new(self.address.0);
let mut signature = amino::StdSignature::from(
chain
.keyring
.sign_ecdsa(account_id, sign_msg.sign_bytes())?,
);
signature.pub_key = chain
.keyring
.get_account_pubkey(account_id)
.expect("missing account key")
.to_bytes();
let msg_type_info = sign_msg
.msg_types()
.iter()
.map(|ty| ty.to_string())
.collect::<Vec<_>>()
.join(", ");
let address = self
.address
.to_bech32(self.tx_builder.schema().acc_prefix());
info!(
"[{}] signed TX {} for {} ({} msgs total; types: {})",
self.chain_id,
self.seq_file.sequence(),
address,
sign_msg.msgs().len(),
msg_type_info,
);
Ok(sign_msg.to_stdtx(signature))
}
}