Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make encode_vec() infallible #1093

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[tendermint-proto]` Make `Protobuf::encode_vec()` infallible
([#1064](https://github.com/informalsystems/tendermint-rs/issues/1064))
6 changes: 4 additions & 2 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ where
}

/// Encodes into a Protobuf-encoded `Vec<u8>`.
fn encode_vec(&self) -> Result<Vec<u8>, Error> {
fn encode_vec(&self) -> Vec<u8> {
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
Expand Down
2 changes: 1 addition & 1 deletion proto/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
34 changes: 14 additions & 20 deletions tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
SimpleValidator::from(self).encode_vec().unwrap()
SimpleValidator::from(self).encode_vec()
}
}

Expand Down
6 changes: 3 additions & 3 deletions tendermint/src/vote/sign_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down