diff --git a/src/rpc.rs b/src/rpc.rs index 90ebb43..81bfbb5 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -20,6 +20,7 @@ use tendermint::amino_types::*; pub const MAX_MSG_LEN: usize = 1024; /// Requests to the KMS +#[derive(Debug)] pub enum Request { /// Sign the given message SignProposal(SignProposalRequest), @@ -31,6 +32,7 @@ pub enum Request { } /// Responses from the KMS +#[derive(Debug)] pub enum Response { /// Signature response SignedVote(SignedVoteResponse), diff --git a/src/session.rs b/src/session.rs index 9f3fef2..6176c25 100644 --- a/src/session.rs +++ b/src/session.rs @@ -33,7 +33,7 @@ pub struct Session { /// Chain ID for this session chain_id: chain::Id, - // Do not sign blocks greates than this height + /// Do not sign blocks greater than this height max_height: Option, /// TCP connection to a validator node @@ -129,8 +129,11 @@ where info!("terminate signal received"); return Ok(false); } - debug!("started handling request ... "); - let response = match Request::read(&mut self.connection)? { + + let request = Request::read(&mut self.connection)?; + debug!("received request: {:?}", &request); + + let response = match request { Request::SignProposal(req) => self.sign(req)?, Request::SignVote(req) => self.sign(req)?, // non-signable requests: @@ -138,6 +141,8 @@ where Request::ShowPublicKey(ref req) => self.get_public_key(req)?, }; + debug!("sending response: {:?}", &response); + let mut buf = vec![]; match response { @@ -148,7 +153,7 @@ where } self.connection.write_all(&buf)?; - debug!("... success handling request"); + Ok(true) } @@ -185,8 +190,9 @@ where // from keyring here: let sig = chain.keyring.sign_ed25519(None, &to_sign)?; + self.log_signing_request(&request); request.set_signature(&sig); - debug!("successfully signed request:\n {:?}", request); + Ok(request.build_response()) } @@ -205,4 +211,22 @@ where *chain.keyring.default_pubkey()?, ))) } + + /// Write an INFO logline about a signing request + fn log_signing_request(&self, request: &T) { + let height = request + .height() + .map(|h| h.to_string()) + .unwrap_or_else(|| "none".to_owned()); + + let msg_type = request + .msg_type() + .map(|t| format!("{:?}", t)) + .unwrap_or_else(|| "Unknown".to_owned()); + + info!( + "[{}] signed {:?} at height: {}", + self.chain_id, msg_type, height + ); + } } diff --git a/tendermint-rs/src/amino_types/proposal.rs b/tendermint-rs/src/amino_types/proposal.rs index a77f239..88354b1 100644 --- a/tendermint-rs/src/amino_types/proposal.rs +++ b/tendermint-rs/src/amino_types/proposal.rs @@ -159,6 +159,10 @@ impl SignableMsg for SignProposalRequest { fn height(&self) -> Option { self.proposal.as_ref().map(|proposal| proposal.height) } + + fn msg_type(&self) -> Option { + Some(SignedMsgType::Proposal) + } } impl ConsensusMessage for Proposal { diff --git a/tendermint-rs/src/amino_types/signature.rs b/tendermint-rs/src/amino_types/signature.rs index 08dd289..97932c0 100644 --- a/tendermint-rs/src/amino_types/signature.rs +++ b/tendermint-rs/src/amino_types/signature.rs @@ -18,6 +18,7 @@ pub trait SignableMsg { fn validate(&self) -> Result<(), ValidationError>; fn consensus_state(&self) -> Option; fn height(&self) -> Option; + fn msg_type(&self) -> Option; } /// Signed message types. This follows: diff --git a/tendermint-rs/src/amino_types/vote.rs b/tendermint-rs/src/amino_types/vote.rs index d743d79..cd5bc33 100644 --- a/tendermint-rs/src/amino_types/vote.rs +++ b/tendermint-rs/src/amino_types/vote.rs @@ -38,9 +38,14 @@ pub struct Vote { } impl Vote { - fn is_valid_vote_type(&self) -> bool { - self.vote_type == SignedMsgType::PreVote.to_u32() - || self.vote_type == SignedMsgType::PreCommit.to_u32() + fn msg_type(&self) -> Option { + if self.vote_type == SignedMsgType::PreVote.to_u32() { + Some(SignedMsgType::PreVote) + } else if self.vote_type == SignedMsgType::PreCommit.to_u32() { + Some(SignedMsgType::PreCommit) + } else { + None + } } } @@ -179,11 +184,14 @@ impl SignableMsg for SignVoteRequest { fn height(&self) -> Option { self.vote.as_ref().map(|vote| vote.height) } + fn msg_type(&self) -> Option { + self.vote.as_ref().and_then(|vote| vote.msg_type()) + } } impl ConsensusMessage for Vote { fn validate_basic(&self) -> Result<(), ValidationError> { - if !self.is_valid_vote_type() { + if self.msg_type().is_none() { return Err(InvalidMessageType.into()); } if self.height < 0 {