Use expect for concensus_encode on Vec

Calls to `unwrap` outside of tests are typically unfavourable.

In memory writers (`Vec`) do not error. We can use `expect` with a
descriptive message string to indicate this.
This commit is contained in:
Tobin Harding 2021-11-25 10:07:25 +11:00
parent 4031fbf4ba
commit e7b84e20d3
3 changed files with 6 additions and 6 deletions

View File

@ -138,7 +138,7 @@ impl From<psbt::Error> for Error {
/// Encode an object into a vector /// Encode an object into a vector
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> { pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
let mut encoder = Vec::new(); let mut encoder = Vec::new();
let len = data.consensus_encode(&mut encoder).unwrap(); let len = data.consensus_encode(&mut encoder).expect("in-memory writers don't error");
debug_assert_eq!(len, encoder.len()); debug_assert_eq!(len, encoder.len());
encoder encoder
} }

View File

@ -365,7 +365,7 @@ impl<'a> GCSFilterWriter<'a> {
// write number of elements as varint // write number of elements as varint
let mut encoder = Vec::new(); let mut encoder = Vec::new();
VarInt(mapped.len() as u64).consensus_encode(&mut encoder).unwrap(); VarInt(mapped.len() as u64).consensus_encode(&mut encoder).expect("in-memory writers don't error");
let mut wrote = self.writer.write(encoder.as_slice())?; let mut wrote = self.writer.write(encoder.as_slice())?;
// write out deltas of sorted values into a Golonb-Rice coded bit stream // write out deltas of sorted values into a Golonb-Rice coded bit stream

View File

@ -45,8 +45,8 @@ pub fn bitcoin_merkle_root_inline<T>(data: &mut [T]) -> T
let idx1 = 2 * idx; let idx1 = 2 * idx;
let idx2 = min(idx1 + 1, data.len() - 1); let idx2 = min(idx1 + 1, data.len() - 1);
let mut encoder = T::engine(); let mut encoder = T::engine();
data[idx1].consensus_encode(&mut encoder).unwrap(); data[idx1].consensus_encode(&mut encoder).expect("in-memory writers don't error");
data[idx2].consensus_encode(&mut encoder).unwrap(); data[idx2].consensus_encode(&mut encoder).expect("in-memory writers don't error");
data[idx] = T::from_engine(encoder); data[idx] = T::from_engine(encoder);
} }
let half_len = data.len() / 2 + data.len() % 2; let half_len = data.len() / 2 + data.len() % 2;
@ -73,8 +73,8 @@ pub fn bitcoin_merkle_root<T, I>(mut iter: I) -> T
// If the size is odd, use the last element twice. // If the size is odd, use the last element twice.
let hash2 = iter.next().unwrap_or(hash1); let hash2 = iter.next().unwrap_or(hash1);
let mut encoder = T::engine(); let mut encoder = T::engine();
hash1.consensus_encode(&mut encoder).unwrap(); hash1.consensus_encode(&mut encoder).expect("in-memory writers don't error");
hash2.consensus_encode(&mut encoder).unwrap(); hash2.consensus_encode(&mut encoder).expect("in-memory writers don't error");
alloc.push(T::from_engine(encoder)); alloc.push(T::from_engine(encoder));
} }
bitcoin_merkle_root_inline(&mut alloc) bitcoin_merkle_root_inline(&mut alloc)