This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Double signing detection and logging improvements #348
Merged
tarcieri
merged 1 commit into
master
from
double-sign-detection-and-logging-improvements
Aug 13, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,10 @@ use crate::{ | |
}; | ||
use std::{fmt::Debug, os::unix::net::UnixStream, time::Instant}; | ||
use tendermint::{ | ||
amino_types::{PingRequest, PingResponse, PubKeyRequest, PubKeyResponse, RemoteError}, | ||
consensus::state::NIL_PLACEHOLDER, | ||
net, | ||
amino_types::{ | ||
PingRequest, PingResponse, PubKeyRequest, PubKeyResponse, RemoteError, SignedMsgType, | ||
}, | ||
consensus, net, | ||
}; | ||
|
||
/// Encrypted session with a validator node | ||
|
@@ -119,25 +120,26 @@ impl Session { | |
} | ||
|
||
/// Perform a digital signature operation | ||
fn sign<T: TendermintRequest + Debug>(&mut self, mut request: T) -> Result<Response, Error> { | ||
fn sign<R>(&mut self, mut request: R) -> Result<Response, Error> | ||
where | ||
R: TendermintRequest + Debug, | ||
{ | ||
request.validate()?; | ||
|
||
let registry = chain::REGISTRY.get(); | ||
let chain = registry.get_chain(&self.config.chain_id).unwrap(); | ||
|
||
if let Some(request_state) = &request.consensus_state() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like this skips the double signing check if the requested message's consensus state parses as The new logic will return an error in that case, rather than what the old logic did, which is unwrap a |
||
let mut chain_state = chain.state.lock().unwrap(); | ||
|
||
if let Err(e) = chain_state.update_consensus_state(request_state.clone()) { | ||
// Report double signing error back to the validator | ||
if e.kind() == StateErrorKind::DoubleSign { | ||
return self.handle_double_signing( | ||
request, | ||
&chain_state.consensus_state().block_id_prefix(), | ||
); | ||
} else { | ||
return Err(e.into()); | ||
} | ||
let (_, request_state) = parse_request(&request)?; | ||
let mut chain_state = chain.state.lock().unwrap(); | ||
|
||
if let Err(e) = chain_state.update_consensus_state(request_state) { | ||
// Report double signing error back to the validator | ||
if e.kind() == StateErrorKind::DoubleSign { | ||
return self.handle_double_signing( | ||
request, | ||
&chain_state.consensus_state().block_id_prefix(), | ||
); | ||
} else { | ||
return Err(e.into()); | ||
} | ||
} | ||
|
||
|
@@ -161,7 +163,7 @@ impl Session { | |
let started_at = Instant::now(); | ||
let signature = chain.keyring.sign_ed25519(None, &to_sign)?; | ||
|
||
self.log_signing_request(&request, started_at); | ||
self.log_signing_request(&request, started_at).unwrap(); | ||
request.set_signature(&signature); | ||
|
||
Ok(request.build_response(None)) | ||
|
@@ -184,43 +186,38 @@ impl Session { | |
} | ||
|
||
/// Write an INFO logline about a signing request | ||
fn log_signing_request<T: TendermintRequest + Debug>(&self, request: &T, started_at: Instant) { | ||
let (consensus_state, block_id) = request | ||
.consensus_state() | ||
.map(|state| (state.to_string(), state.block_id_prefix())) | ||
.unwrap_or_else(|| (NIL_PLACEHOLDER.to_owned(), NIL_PLACEHOLDER.to_owned())); | ||
|
||
let msg_type = request | ||
.msg_type() | ||
.map(|t| format!("{:?}", t)) | ||
.unwrap_or_else(|| "Unknown".to_owned()); | ||
fn log_signing_request<R>(&self, request: &R, started_at: Instant) -> Result<(), Error> | ||
where | ||
R: TendermintRequest + Debug, | ||
{ | ||
let (msg_type, request_state) = parse_request(request)?; | ||
|
||
info!( | ||
"[{}@{}] signed {}:{} at h/r/s {} ({} ms)", | ||
"[{}@{}] signed {:?}:{} at h/r/s {} ({} ms)", | ||
&self.config.chain_id, | ||
&self.config.addr, | ||
msg_type, | ||
block_id, | ||
consensus_state, | ||
request_state.block_id_prefix(), | ||
request_state, | ||
started_at.elapsed().as_millis(), | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Handle attempted double signing | ||
fn handle_double_signing<T: TendermintRequest + Debug>( | ||
fn handle_double_signing<R>( | ||
&self, | ||
request: T, | ||
request: R, | ||
original_block_id: &str, | ||
) -> Result<Response, Error> { | ||
let msg_type = request | ||
.msg_type() | ||
.map(|t| format!("{:?}", t)) | ||
.unwrap_or_else(|| "Unknown".to_owned()); | ||
|
||
let request_state = request.consensus_state().unwrap(); | ||
) -> Result<Response, Error> | ||
where | ||
R: TendermintRequest + Debug, | ||
{ | ||
let (msg_type, request_state) = parse_request(&request)?; | ||
|
||
error!( | ||
"[{}:{}] attempted double sign for {} at h/r/s: {} ({} != {})", | ||
"[{}:{}] attempted double sign {:?} at h/r/s: {} ({} != {})", | ||
&self.config.chain_id, | ||
&self.config.addr, | ||
msg_type, | ||
|
@@ -233,3 +230,28 @@ impl Session { | |
Ok(request.build_response(Some(remote_err))) | ||
} | ||
} | ||
|
||
/// Parse the consensus state from an incoming request | ||
// TODO(tarcieri): fix the upstream Amino parser to do this correctly for us | ||
fn parse_request<R>(request: &R) -> Result<(SignedMsgType, consensus::State), Error> | ||
where | ||
R: TendermintRequest + Debug, | ||
{ | ||
let msg_type = match request.msg_type() { | ||
Some(ty) => ty, | ||
None => Err(err!(ProtocolError, "no message type for this request"))?, | ||
}; | ||
|
||
let mut consensus_state = match request.consensus_state() { | ||
Some(state) => state, | ||
None => Err(err!(ProtocolError, "no consensus state in request"))?, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this relies on every message we sign including both of these, but I think that's the case anyway? |
||
|
||
consensus_state.step = match msg_type { | ||
SignedMsgType::Proposal => 0, | ||
SignedMsgType::PreVote => 1, | ||
SignedMsgType::PreCommit => 2, | ||
}; | ||
|
||
Ok((msg_type, consensus_state)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened an upstream issue about how I think this should get fixed upstream in tendermint-rs to better reflect the Tendermint spec:
informalsystems/tendermint-rs#9