Use expect for concensus_encode on engines

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

Hash engines do not error when calling `consensus_encode`. Instead of
the current usage of `unwrap` we can use `expect` with a descriptive
string as is done in other parts of the codebase.
This commit is contained in:
Tobin Harding 2021-11-25 10:01:41 +11:00
parent a2efafcf9a
commit fa513bb5b5
5 changed files with 22 additions and 22 deletions

View File

@ -215,7 +215,7 @@ impl Block {
/// compute witness commitment for the transaction list
pub fn compute_witness_commitment (witness_root: &WitnessMerkleNode, witness_reserved_value: &[u8]) -> WitnessCommitment {
let mut encoder = WitnessCommitment::engine();
witness_root.consensus_encode(&mut encoder).unwrap();
witness_root.consensus_encode(&mut encoder).expect("engines don't error");
encoder.input(witness_reserved_value);
WitnessCommitment::from_engine(encoder)
}

View File

@ -292,10 +292,10 @@ impl Transaction {
/// will also hash witnesses.
pub fn txid(&self) -> Txid {
let mut enc = Txid::engine();
self.version.consensus_encode(&mut enc).unwrap();
self.input.consensus_encode(&mut enc).unwrap();
self.output.consensus_encode(&mut enc).unwrap();
self.lock_time.consensus_encode(&mut enc).unwrap();
self.version.consensus_encode(&mut enc).expect("engines don't error");
self.input.consensus_encode(&mut enc).expect("engines don't error");
self.output.consensus_encode(&mut enc).expect("engines don't error");
self.lock_time.consensus_encode(&mut enc).expect("engines don't error");
Txid::from_engine(enc)
}
@ -304,7 +304,7 @@ impl Transaction {
/// value returned by txid() function.
pub fn wtxid(&self) -> Wtxid {
let mut enc = Wtxid::engine();
self.consensus_encode(&mut enc).unwrap();
self.consensus_encode(&mut enc).expect("engines don't error");
Wtxid::from_engine(enc)
}

View File

@ -56,7 +56,7 @@ impl SighashComponents {
let hash_prevouts = {
let mut enc = SigHash::engine();
for txin in &tx.input {
txin.previous_output.consensus_encode(&mut enc).unwrap();
txin.previous_output.consensus_encode(&mut enc).expect("engines don't error");
}
SigHash::from_engine(enc)
};
@ -64,7 +64,7 @@ impl SighashComponents {
let hash_sequence = {
let mut enc = SigHash::engine();
for txin in &tx.input {
txin.sequence.consensus_encode(&mut enc).unwrap();
txin.sequence.consensus_encode(&mut enc).expect("engines don't error");
}
SigHash::from_engine(enc)
};
@ -72,7 +72,7 @@ impl SighashComponents {
let hash_outputs = {
let mut enc = SigHash::engine();
for txout in &tx.output {
txout.consensus_encode(&mut enc).unwrap();
txout.consensus_encode(&mut enc).expect("engines don't error");
}
SigHash::from_engine(enc)
};
@ -90,19 +90,19 @@ impl SighashComponents {
/// input.
pub fn sighash_all(&self, txin: &TxIn, script_code: &Script, value: u64) -> SigHash {
let mut enc = SigHash::engine();
self.tx_version.consensus_encode(&mut enc).unwrap();
self.hash_prevouts.consensus_encode(&mut enc).unwrap();
self.hash_sequence.consensus_encode(&mut enc).unwrap();
self.tx_version.consensus_encode(&mut enc).expect("engines don't error");
self.hash_prevouts.consensus_encode(&mut enc).expect("engines don't error");
self.hash_sequence.consensus_encode(&mut enc).expect("engines don't error");
txin
.previous_output
.consensus_encode(&mut enc)
.unwrap();
script_code.consensus_encode(&mut enc).unwrap();
value.consensus_encode(&mut enc).unwrap();
txin.sequence.consensus_encode(&mut enc).unwrap();
self.hash_outputs.consensus_encode(&mut enc).unwrap();
self.tx_locktime.consensus_encode(&mut enc).unwrap();
1u32.consensus_encode(&mut enc).unwrap(); // hashtype
.expect("engines don't error");
script_code.consensus_encode(&mut enc).expect("engines don't error");
value.consensus_encode(&mut enc).expect("engines don't error");
txin.sequence.consensus_encode(&mut enc).expect("engines don't error");
self.hash_outputs.consensus_encode(&mut enc).expect("engines don't error");
self.tx_locktime.consensus_encode(&mut enc).expect("engines don't error");
1u32.consensus_encode(&mut enc).expect("engines don't error"); // hashtype
SigHash::from_engine(enc)
}
}

View File

@ -341,8 +341,8 @@ impl PartialMerkleTree {
/// Helper method to produce SHA256D(left + right)
fn parent_hash(left: TxMerkleNode, right: TxMerkleNode) -> TxMerkleNode {
let mut encoder = TxMerkleNode::engine();
left.consensus_encode(&mut encoder).unwrap();
right.consensus_encode(&mut encoder).unwrap();
left.consensus_encode(&mut encoder).expect("engines don't error");
right.consensus_encode(&mut encoder).expect("engines don't error");
TxMerkleNode::from_engine(encoder)
}
}

View File

@ -252,7 +252,7 @@ pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
let mut engine = sha256d::Hash::engine();
engine.input(BITCOIN_SIGNED_MSG_PREFIX);
let msg_len = encode::VarInt(msg.len() as u64);
msg_len.consensus_encode(&mut engine).unwrap();
msg_len.consensus_encode(&mut engine).expect("engines don't error");
engine.input(msg.as_bytes());
sha256d::Hash::from_engine(engine)
}