From 085f7b8316e77fad8009d4a919dfc8863d9eb7d3 Mon Sep 17 00:00:00 2001 From: Shoaib Ahmed Date: Mon, 14 Feb 2022 16:28:20 +0530 Subject: [PATCH 1/4] Make encode_vec() infallible --- abci/src/application/kvstore/main.rs | 50 +++++++++++++++++++--------- proto/src/lib.rs | 6 ++-- proto/tests/unit.rs | 2 +- tendermint/src/block/header.rs | 34 ++++++++----------- tendermint/src/public_key.rs | 2 +- tendermint/src/validator.rs | 2 +- tendermint/src/vote/sign_vote.rs | 6 ++-- 7 files changed, 58 insertions(+), 44 deletions(-) diff --git a/abci/src/application/kvstore/main.rs b/abci/src/application/kvstore/main.rs index 5eba12d75..dee45aa8c 100644 --- a/abci/src/application/kvstore/main.rs +++ b/abci/src/application/kvstore/main.rs @@ -29,20 +29,38 @@ struct Opt { } fn main() { - let opt: Opt = Opt::from_args(); - let log_level = if opt.quiet { - LevelFilter::OFF - } else if opt.verbose { - LevelFilter::DEBUG - } else { - LevelFilter::INFO - }; - tracing_subscriber::fmt().with_max_level(log_level).init(); - - let (app, driver) = KeyValueStoreApp::new(); - let server = ServerBuilder::new(opt.read_buf_size) - .bind(format!("{}:{}", opt.host, opt.port), app) - .unwrap(); - std::thread::spawn(move || driver.run()); - server.listen().unwrap(); + // let opt: Opt = Opt::from_args(); + // let log_level = if opt.quiet { + // LevelFilter::OFF + // } else if opt.verbose { + // LevelFilter::DEBUG + // } else { + // LevelFilter::INFO + // }; + // tracing_subscriber::fmt().with_max_level(log_level).init(); + // + // let (app, driver) = KeyValueStoreApp::new(); + // let server = ServerBuilder::new(opt.read_buf_sizeSubscription) + // .bind(format!("{}:{}", opt.host, opt.port), app) + // .unwrap(); + // std::thread::spawn(move || driver.run()); + // server.listen().unwrap(); + use futures::StreamExt; + use tendermint_rpc::Subscription; + + /// Prints `count` events from the given subscription. + async fn print_events(subs: &mut Subscription, count: usize) { + let mut counter = 0_usize; + while let Some(res) = subs.next().await { + // Technically, a subscription produces `Result` + // instances. Errors can be produced by the remote endpoint at any + // time and need to be handled here. + let ev = res.unwrap(); + println!("Got incoming event: {:?}", ev); + counter += 1; + if counter >= count { + break; + } + } + } } diff --git a/proto/src/lib.rs b/proto/src/lib.rs index 1d21e821b..bbdeefa3a 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -185,9 +185,11 @@ where } /// Encodes into a Protobuf-encoded `Vec`. - fn encode_vec(&self) -> Result, Error> { + fn encode_vec(&self) -> Vec { let mut wire = Vec::with_capacity(self.encoded_len()); - self.encode(&mut wire).map(|_| wire) + self.encode(&mut wire) + .map(|_| wire) + .expect("buffer size too small") } /// Constructor that attempts to decode a Protobuf-encoded instance from a diff --git a/proto/tests/unit.rs b/proto/tests/unit.rs index 03efe33bb..a975cbd46 100644 --- a/proto/tests/unit.rs +++ b/proto/tests/unit.rs @@ -93,7 +93,7 @@ pub fn protobuf_struct_conveniences_example() { part_set_header_exists: false, }; - let wire = my_domain_type.encode_vec().unwrap(); + let wire = my_domain_type.encode_vec(); assert_eq!( wire, vec![10, 12, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33] diff --git a/tendermint/src/block/header.rs b/tendermint/src/block/header.rs index ddb51de90..d8f966bcf 100644 --- a/tendermint/src/block/header.rs +++ b/tendermint/src/block/header.rs @@ -169,26 +169,20 @@ impl Header { // https://github.com/tendermint/tendermint/blob/134fe2896275bb926b49743c1e25493f6b24cc31/types/encoding_helper.go#L9:6 let fields_bytes = vec![ - self.version.encode_vec().unwrap(), - self.chain_id.encode_vec().unwrap(), - self.height.encode_vec().unwrap(), - self.time.encode_vec().unwrap(), - self.last_block_id.unwrap_or_default().encode_vec().unwrap(), - self.last_commit_hash - .unwrap_or_default() - .encode_vec() - .unwrap(), - self.data_hash.unwrap_or_default().encode_vec().unwrap(), - self.validators_hash.encode_vec().unwrap(), - self.next_validators_hash.encode_vec().unwrap(), - self.consensus_hash.encode_vec().unwrap(), - self.app_hash.encode_vec().unwrap(), - self.last_results_hash - .unwrap_or_default() - .encode_vec() - .unwrap(), - self.evidence_hash.unwrap_or_default().encode_vec().unwrap(), - self.proposer_address.encode_vec().unwrap(), + self.version.encode_vec(), + self.chain_id.encode_vec(), + self.height.encode_vec(), + self.time.encode_vec(), + self.last_block_id.unwrap_or_default().encode_vec(), + self.last_commit_hash.unwrap_or_default().encode_vec(), + self.data_hash.unwrap_or_default().encode_vec(), + self.validators_hash.encode_vec(), + self.next_validators_hash.encode_vec(), + self.consensus_hash.encode_vec(), + self.app_hash.encode_vec(), + self.last_results_hash.unwrap_or_default().encode_vec(), + self.evidence_hash.unwrap_or_default().encode_vec(), + self.proposer_address.encode_vec(), ]; Hash::Sha256(simple_hash_from_byte_vectors(fields_bytes)) diff --git a/tendermint/src/public_key.rs b/tendermint/src/public_key.rs index 0a7565af4..575841c1b 100644 --- a/tendermint/src/public_key.rs +++ b/tendermint/src/public_key.rs @@ -553,7 +553,7 @@ mod tests { ), error: None, }; - let got = msg.encode_vec().unwrap(); + let got = msg.encode_vec(); assert_eq!(got, encoded); assert_eq!(PubKeyResponse::decode_vec(&encoded).unwrap(), msg); diff --git a/tendermint/src/validator.rs b/tendermint/src/validator.rs index 58da81376..fcd28d5e7 100644 --- a/tendermint/src/validator.rs +++ b/tendermint/src/validator.rs @@ -286,7 +286,7 @@ impl Info { /// Returns the bytes to be hashed into the Merkle tree - /// the leaves of the tree. pub fn hash_bytes(&self) -> Vec { - SimpleValidator::from(self).encode_vec().unwrap() + SimpleValidator::from(self).encode_vec() } } diff --git a/tendermint/src/vote/sign_vote.rs b/tendermint/src/vote/sign_vote.rs index 15470d964..56c978de9 100644 --- a/tendermint/src/vote/sign_vote.rs +++ b/tendermint/src/vote/sign_vote.rs @@ -295,7 +295,7 @@ mod tests { }; println!("{:?}", vt_precommit); let cv_precommit = CanonicalVote::new(vt_precommit, ChainId::try_from("A").unwrap()); - let got = cv_precommit.encode_vec().unwrap(); + let got = cv_precommit.encode_vec(); let want = vec![ 0x8, // (field_number << 3) | wire_type 0x2, // PrecommitType @@ -322,7 +322,7 @@ mod tests { let cv_prevote = CanonicalVote::new(vt_prevote, ChainId::try_from("A").unwrap()); - let got = cv_prevote.encode_vec().unwrap(); + let got = cv_prevote.encode_vec(); let want = vec![ 0x8, // (field_number << 3) | wire_type @@ -375,7 +375,7 @@ mod tests { ]) .unwrap(), }; - let got = vote.encode_vec().unwrap(); + let got = vote.encode_vec(); let v = Vote::decode_vec(&got).unwrap(); assert_eq!(v, vote); From 478fa1121b82a5ec0c302152fcad9209c6dda709 Mon Sep 17 00:00:00 2001 From: Shoaib Ahmed Date: Mon, 14 Feb 2022 17:01:10 +0530 Subject: [PATCH 2/4] Add .changelog entry --- .../unreleased/breaking-changes/1064-infallible-encode_vec.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md diff --git a/.changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md b/.changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md new file mode 100644 index 000000000..110d96672 --- /dev/null +++ b/.changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md @@ -0,0 +1,2 @@ +- Make encode_vec() infallible + ([#1064](https://github.com/informalsystems/tendermint-rs/issues/1064)) \ No newline at end of file From 0e7895347e7c4cb7e7812ff08c9912376ed2295b Mon Sep 17 00:00:00 2001 From: Shoaib Ahmed Date: Mon, 14 Feb 2022 17:24:58 +0530 Subject: [PATCH 3/4] Undo changes to kvstore --- abci/src/application/kvstore/main.rs | 50 +++++++++------------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/abci/src/application/kvstore/main.rs b/abci/src/application/kvstore/main.rs index dee45aa8c..5eba12d75 100644 --- a/abci/src/application/kvstore/main.rs +++ b/abci/src/application/kvstore/main.rs @@ -29,38 +29,20 @@ struct Opt { } fn main() { - // let opt: Opt = Opt::from_args(); - // let log_level = if opt.quiet { - // LevelFilter::OFF - // } else if opt.verbose { - // LevelFilter::DEBUG - // } else { - // LevelFilter::INFO - // }; - // tracing_subscriber::fmt().with_max_level(log_level).init(); - // - // let (app, driver) = KeyValueStoreApp::new(); - // let server = ServerBuilder::new(opt.read_buf_sizeSubscription) - // .bind(format!("{}:{}", opt.host, opt.port), app) - // .unwrap(); - // std::thread::spawn(move || driver.run()); - // server.listen().unwrap(); - use futures::StreamExt; - use tendermint_rpc::Subscription; - - /// Prints `count` events from the given subscription. - async fn print_events(subs: &mut Subscription, count: usize) { - let mut counter = 0_usize; - while let Some(res) = subs.next().await { - // Technically, a subscription produces `Result` - // instances. Errors can be produced by the remote endpoint at any - // time and need to be handled here. - let ev = res.unwrap(); - println!("Got incoming event: {:?}", ev); - counter += 1; - if counter >= count { - break; - } - } - } + let opt: Opt = Opt::from_args(); + let log_level = if opt.quiet { + LevelFilter::OFF + } else if opt.verbose { + LevelFilter::DEBUG + } else { + LevelFilter::INFO + }; + tracing_subscriber::fmt().with_max_level(log_level).init(); + + let (app, driver) = KeyValueStoreApp::new(); + let server = ServerBuilder::new(opt.read_buf_size) + .bind(format!("{}:{}", opt.host, opt.port), app) + .unwrap(); + std::thread::spawn(move || driver.run()); + server.listen().unwrap(); } From 5010ee27ad749bb5256e4010ecc5ea8d81a5db9e Mon Sep 17 00:00:00 2001 From: Thane Thomson Date: Mon, 14 Feb 2022 07:30:19 -0500 Subject: [PATCH 4/4] Expand on changelog entry --- .../unreleased/breaking-changes/1064-infallible-encode_vec.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md b/.changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md index 110d96672..f403b9ab6 100644 --- a/.changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md +++ b/.changelog/unreleased/breaking-changes/1064-infallible-encode_vec.md @@ -1,2 +1,2 @@ -- Make encode_vec() infallible +- `[tendermint-proto]` Make `Protobuf::encode_vec()` infallible ([#1064](https://github.com/informalsystems/tendermint-rs/issues/1064)) \ No newline at end of file