Use get_or_insert_with in taproot_cache

This commit is contained in:
Martin Habovstiak 2021-08-10 10:02:33 +02:00
parent 6e06a32ccc
commit ca80a5a030
1 changed files with 4 additions and 6 deletions

View File

@ -594,7 +594,7 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
} }
fn taproot_cache(&mut self, prevouts: &[TxOut]) -> &TaprootCache { fn taproot_cache(&mut self, prevouts: &[TxOut]) -> &TaprootCache {
if self.taproot_cache.is_none() { self.taproot_cache.get_or_insert_with(|| {
let mut enc_amounts = sha256::Hash::engine(); let mut enc_amounts = sha256::Hash::engine();
let mut enc_script_pubkeys = sha256::Hash::engine(); let mut enc_script_pubkeys = sha256::Hash::engine();
for prevout in prevouts { for prevout in prevouts {
@ -604,13 +604,11 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
.consensus_encode(&mut enc_script_pubkeys) .consensus_encode(&mut enc_script_pubkeys)
.unwrap(); .unwrap();
} }
let cache = TaprootCache { TaprootCache {
amounts: sha256::Hash::from_engine(enc_amounts), amounts: sha256::Hash::from_engine(enc_amounts),
script_pubkeys: sha256::Hash::from_engine(enc_script_pubkeys), script_pubkeys: sha256::Hash::from_engine(enc_script_pubkeys),
};
self.taproot_cache = Some(cache);
} }
self.taproot_cache.as_ref().unwrap() // safe to unwrap because we checked is_none() })
} }
} }