diff --git a/Cargo.lock b/Cargo.lock index d312db92..ae7f7982 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,9 +135,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.5" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +checksum = "4cd2405b3ac1faab2990b74d728624cd9fd115651fcecc7c2d8daf01376275ba" dependencies = [ "anstyle", "anstyle-parse", @@ -244,9 +244,9 @@ checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" [[package]] name = "base64" -version = "0.21.6" +version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c79fed4cdb43e993fcdadc7e58a09fd0e3e649c4436fa11da71c9f1f3ee7feb9" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" [[package]] name = "base64ct" @@ -453,9 +453,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.14" +version = "4.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33e92c5c1a78c62968ec57dbc2440366a2d6e5a23faf829970ff1585dc6b18e2" +checksum = "58e54881c004cec7895b0068a0a954cd5d62da01aef83fa35b1e594497bf5445" dependencies = [ "clap_builder", "clap_derive", @@ -463,9 +463,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.14" +version = "4.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4323769dc8a61e2c39ad7dc26f6f2800524691a44d74fe3d1071a5c24db6370" +checksum = "59cb82d7f531603d2fd1f507441cdd35184fa81beff7bd489570de7f773460bb" dependencies = [ "anstream", "anstyle", @@ -1126,9 +1126,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.22" +version = "0.3.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d6250322ef6e60f93f9a2162799302cd6f68f79f6e5d85c8c16f14d1d958178" +checksum = "b553656127a00601c8ae5590fcfdc118e4083a7924b6cf4ffc1ea4b99dc429d7" dependencies = [ "bytes", "fnv", @@ -1155,7 +1155,7 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270" dependencies = [ - "base64 0.21.6", + "base64 0.21.7", "bytes", "headers-core", "http", @@ -1385,9 +1385,9 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "js-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +checksum = "9a1d36f1235bc969acba30b7f5990b864423a6068a10f7c90ae8f0112e3a59d1" dependencies = [ "wasm-bindgen", ] @@ -1408,9 +1408,9 @@ dependencies = [ [[package]] name = "keccak" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940" +checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654" dependencies = [ "cpufeatures 0.2.12", ] @@ -2197,9 +2197,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.28" +version = "0.38.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" dependencies = [ "bitflags 2.4.1", "errno", @@ -2707,9 +2707,9 @@ checksum = "e6324de19005740c3a6acf046fb17a8e69157415c7a9a4b679802d5edb78cb60" [[package]] name = "termcolor" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" dependencies = [ "winapi-util", ] @@ -3188,9 +3188,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" dependencies = [ "cfg-if 1.0.0", "wasm-bindgen-macro", @@ -3198,9 +3198,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd" dependencies = [ "bumpalo", "log", @@ -3213,9 +3213,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -3223,9 +3223,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7" dependencies = [ "proc-macro2", "quote", @@ -3236,15 +3236,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" [[package]] name = "web-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f" +checksum = "58cd2333b6e0be7a39605f0e255892fd7418a682d8da8fe042fe25128794d2ed" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/src/privval.rs b/src/privval.rs index 479573df..5f8a7f09 100644 --- a/src/privval.rs +++ b/src/privval.rs @@ -129,6 +129,38 @@ impl SignableMsg { }, } } + + /// Add a consensus signature to this message. + pub fn add_consensus_signature(&mut self, signature: impl Into) { + match self { + SignableMsg::Proposal(proposal) => { + proposal.signature = Some(signature.into()); + } + SignableMsg::Vote(vote) => { + vote.signature = Some(signature.into()); + } + } + } + + /// Add an extension signature to this message. + pub fn add_extension_signature( + &mut self, + signature: impl Into, + ) -> Result<(), Error> { + match self { + SignableMsg::Vote(vote) => { + if vote.extension.is_empty() { + return Err(Error::protocol( + "can't add signature to empty extension".into(), + )); + } + + vote.extension_signature = Some(signature.into()); + Ok(()) + } + _ => Err(Error::invalid_message_type()), + } + } } impl From for SignableMsg { diff --git a/src/rpc.rs b/src/rpc.rs index f9331366..428669ff 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -3,7 +3,7 @@ // TODO: docs for everything #![allow(missing_docs)] -use crate::{keyring::Signature, privval::SignableMsg}; +use crate::privval::SignableMsg; use prost::Message as _; use std::io::Read; use tendermint::{chain, Proposal, Vote}; @@ -128,28 +128,21 @@ impl Response { }), } } +} - /// Construct a signed response from a [`SignableMsg`] and a [`Signature`]. - pub fn sign(msg: SignableMsg, sig: Signature) -> Result { +impl From for Response { + fn from(msg: SignableMsg) -> Response { match msg { SignableMsg::Proposal(proposal) => { - let mut proposal = proto::types::Proposal::from(proposal); - proposal.signature = sig.to_vec(); - Ok(Response::SignedProposal( - proto::privval::SignedProposalResponse { - proposal: Some(proposal), - error: None, - }, - )) - } - SignableMsg::Vote(vote) => { - let mut vote = proto::types::Vote::from(vote); - vote.signature = sig.to_vec(); - Ok(Response::SignedVote(proto::privval::SignedVoteResponse { - vote: Some(vote), + Response::SignedProposal(proto::privval::SignedProposalResponse { + proposal: Some(proposal.into()), error: None, - })) + }) } + SignableMsg::Vote(vote) => Response::SignedVote(proto::privval::SignedVoteResponse { + vote: Some(vote.into()), + error: None, + }), } } } diff --git a/src/session.rs b/src/session.rs index 09509a38..b88d9238 100644 --- a/src/session.rs +++ b/src/session.rs @@ -142,23 +142,28 @@ impl Session { // TODO(tarcieri): support for non-default public keys let public_key = None; let chain_id = self.config.chain_id.clone(); - let canonical_msg = signable_msg.canonical_bytes(chain_id.clone())?; + let started_at = Instant::now(); - let signature = chain.keyring.sign(public_key, &canonical_msg)?; + let consensus_sig = chain.keyring.sign(public_key, &canonical_msg)?; + signable_msg.add_consensus_signature(consensus_sig); self.log_signing_request(&signable_msg, started_at).unwrap(); // Add extension signature if there are any extensions defined if let Some(extension_msg) = signable_msg.extension_bytes(chain_id)? { + let started_at = Instant::now(); let extension_sig = chain.keyring.sign(public_key, &extension_msg)?; - - match &mut signable_msg { - SignableMsg::Vote(vote) => vote.extension_signature = Some(extension_sig.into()), - other => fail!(InvalidMessageError, "expected a vote type: {:?}", other), - } + signable_msg.add_extension_signature(extension_sig)?; + + info!( + "[{}@{}] signed vote extension ({} ms)", + &self.config.chain_id, + &self.config.addr, + started_at.elapsed().as_millis(), + ); } - Response::sign(signable_msg, signature) + Ok(signable_msg.into()) } /// If a max block height is configured, ensure the block we're signing