Skip to content

Commit c70f6ee

Browse files
Make encode_vec() infallible (#1093)
* Make encode_vec() infallible * Add .changelog entry * Undo changes to kvstore * Expand on changelog entry Co-authored-by: Thane Thomson <[email protected]>
1 parent 904862c commit c70f6ee

File tree

7 files changed

+26
-28
lines changed

7 files changed

+26
-28
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `[tendermint-proto]` Make `Protobuf::encode_vec()` infallible
2+
([#1064](https://github.com/informalsystems/tendermint-rs/issues/1064))

proto/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,11 @@ where
185185
}
186186

187187
/// Encodes into a Protobuf-encoded `Vec<u8>`.
188-
fn encode_vec(&self) -> Result<Vec<u8>, Error> {
188+
fn encode_vec(&self) -> Vec<u8> {
189189
let mut wire = Vec::with_capacity(self.encoded_len());
190-
self.encode(&mut wire).map(|_| wire)
190+
self.encode(&mut wire)
191+
.map(|_| wire)
192+
.expect("buffer size too small")
191193
}
192194

193195
/// Constructor that attempts to decode a Protobuf-encoded instance from a

proto/tests/unit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn protobuf_struct_conveniences_example() {
9393
part_set_header_exists: false,
9494
};
9595

96-
let wire = my_domain_type.encode_vec().unwrap();
96+
let wire = my_domain_type.encode_vec();
9797
assert_eq!(
9898
wire,
9999
vec![10, 12, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]

tendermint/src/block/header.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,20 @@ impl Header {
169169
// https://github.com/tendermint/tendermint/blob/134fe2896275bb926b49743c1e25493f6b24cc31/types/encoding_helper.go#L9:6
170170

171171
let fields_bytes = vec![
172-
self.version.encode_vec().unwrap(),
173-
self.chain_id.encode_vec().unwrap(),
174-
self.height.encode_vec().unwrap(),
175-
self.time.encode_vec().unwrap(),
176-
self.last_block_id.unwrap_or_default().encode_vec().unwrap(),
177-
self.last_commit_hash
178-
.unwrap_or_default()
179-
.encode_vec()
180-
.unwrap(),
181-
self.data_hash.unwrap_or_default().encode_vec().unwrap(),
182-
self.validators_hash.encode_vec().unwrap(),
183-
self.next_validators_hash.encode_vec().unwrap(),
184-
self.consensus_hash.encode_vec().unwrap(),
185-
self.app_hash.encode_vec().unwrap(),
186-
self.last_results_hash
187-
.unwrap_or_default()
188-
.encode_vec()
189-
.unwrap(),
190-
self.evidence_hash.unwrap_or_default().encode_vec().unwrap(),
191-
self.proposer_address.encode_vec().unwrap(),
172+
self.version.encode_vec(),
173+
self.chain_id.encode_vec(),
174+
self.height.encode_vec(),
175+
self.time.encode_vec(),
176+
self.last_block_id.unwrap_or_default().encode_vec(),
177+
self.last_commit_hash.unwrap_or_default().encode_vec(),
178+
self.data_hash.unwrap_or_default().encode_vec(),
179+
self.validators_hash.encode_vec(),
180+
self.next_validators_hash.encode_vec(),
181+
self.consensus_hash.encode_vec(),
182+
self.app_hash.encode_vec(),
183+
self.last_results_hash.unwrap_or_default().encode_vec(),
184+
self.evidence_hash.unwrap_or_default().encode_vec(),
185+
self.proposer_address.encode_vec(),
192186
];
193187

194188
Hash::Sha256(simple_hash_from_byte_vectors(fields_bytes))

tendermint/src/public_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ mod tests {
553553
),
554554
error: None,
555555
};
556-
let got = msg.encode_vec().unwrap();
556+
let got = msg.encode_vec();
557557

558558
assert_eq!(got, encoded);
559559
assert_eq!(PubKeyResponse::decode_vec(&encoded).unwrap(), msg);

tendermint/src/validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl Info {
286286
/// Returns the bytes to be hashed into the Merkle tree -
287287
/// the leaves of the tree.
288288
pub fn hash_bytes(&self) -> Vec<u8> {
289-
SimpleValidator::from(self).encode_vec().unwrap()
289+
SimpleValidator::from(self).encode_vec()
290290
}
291291
}
292292

tendermint/src/vote/sign_vote.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ mod tests {
295295
};
296296
println!("{:?}", vt_precommit);
297297
let cv_precommit = CanonicalVote::new(vt_precommit, ChainId::try_from("A").unwrap());
298-
let got = cv_precommit.encode_vec().unwrap();
298+
let got = cv_precommit.encode_vec();
299299
let want = vec![
300300
0x8, // (field_number << 3) | wire_type
301301
0x2, // PrecommitType
@@ -322,7 +322,7 @@ mod tests {
322322

323323
let cv_prevote = CanonicalVote::new(vt_prevote, ChainId::try_from("A").unwrap());
324324

325-
let got = cv_prevote.encode_vec().unwrap();
325+
let got = cv_prevote.encode_vec();
326326

327327
let want = vec![
328328
0x8, // (field_number << 3) | wire_type
@@ -375,7 +375,7 @@ mod tests {
375375
])
376376
.unwrap(),
377377
};
378-
let got = vote.encode_vec().unwrap();
378+
let got = vote.encode_vec();
379379
let v = Vote::decode_vec(&got).unwrap();
380380

381381
assert_eq!(v, vote);

0 commit comments

Comments
 (0)