Move and rename TxOut default trait to a const called NULL

This commit is contained in:
yancy 2023-05-05 11:02:06 +02:00
parent 64540b9b93
commit 75b3f19b96
4 changed files with 14 additions and 25 deletions

View File

@ -29,7 +29,7 @@ pub struct ScriptBuf(pub(in crate::blockdata::script) Vec<u8>);
impl ScriptBuf { impl ScriptBuf {
/// Creates a new empty script. /// Creates a new empty script.
pub fn new() -> Self { ScriptBuf(Vec::new()) } pub const fn new() -> Self { ScriptBuf(Vec::new()) }
/// Creates a new empty script with pre-allocated capacity. /// Creates a new empty script with pre-allocated capacity.
pub fn with_capacity(capacity: usize) -> Self { ScriptBuf(Vec::with_capacity(capacity)) } pub fn with_capacity(capacity: usize) -> Self { ScriptBuf(Vec::with_capacity(capacity)) }

View File

@ -484,6 +484,10 @@ pub struct TxOut {
} }
impl TxOut { impl TxOut {
/// This is used as a "null txout" in consensus signing code.
pub const NULL: Self =
TxOut { value: Amount::from_sat(0xffffffffffffffff), script_pubkey: ScriptBuf::new() };
/// The weight of the txout in witness units /// The weight of the txout in witness units
/// ///
/// Keep in mind that when adding a TxOut to a transaction, the total weight of the transaction /// Keep in mind that when adding a TxOut to a transaction, the total weight of the transaction
@ -518,13 +522,6 @@ impl TxOut {
} }
} }
// This is used as a "null txout" in consensus signing code.
impl Default for TxOut {
fn default() -> TxOut {
TxOut { value: Amount::from_sat(0xffffffffffffffff), script_pubkey: ScriptBuf::new() }
}
}
/// Result of [`Transaction::encode_signing_data_to`]. /// Result of [`Transaction::encode_signing_data_to`].
/// ///
/// This type forces the caller to handle SIGHASH_SINGLE bug case. /// This type forces the caller to handle SIGHASH_SINGLE bug case.

View File

@ -929,15 +929,7 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
.iter() .iter()
.take(input_index + 1) // sign all outputs up to and including this one, but erase .take(input_index + 1) // sign all outputs up to and including this one, but erase
.enumerate() // all of them except for this one .enumerate() // all of them except for this one
.map( .map(|(n, out)| if n == input_index { out.clone() } else { TxOut::NULL });
|(n, out)| {
if n == input_index {
out.clone()
} else {
TxOut::default()
}
},
);
output_iter.collect() output_iter.collect()
} }
EcdsaSighashType::None => vec![], EcdsaSighashType::None => vec![],
@ -1142,7 +1134,7 @@ mod tests {
version: 1, version: 1,
lock_time: absolute::LockTime::ZERO, lock_time: absolute::LockTime::ZERO,
input: vec![TxIn::default(), TxIn::default()], input: vec![TxIn::default(), TxIn::default()],
output: vec![TxOut::default()], output: vec![TxOut::NULL],
}; };
let script = ScriptBuf::new(); let script = ScriptBuf::new();
let cache = SighashCache::new(&tx); let cache = SighashCache::new(&tx);
@ -1340,13 +1332,13 @@ mod tests {
c.taproot_signature_hash(0, &empty_prevouts, None, None, TapSighashType::All), c.taproot_signature_hash(0, &empty_prevouts, None, None, TapSighashType::All),
Err(Error::PrevoutsSize) Err(Error::PrevoutsSize)
); );
let two = vec![TxOut::default(), TxOut::default()]; let two = vec![TxOut::NULL, TxOut::NULL];
let too_many_prevouts = Prevouts::All(&two); let too_many_prevouts = Prevouts::All(&two);
assert_eq!( assert_eq!(
c.taproot_signature_hash(0, &too_many_prevouts, None, None, TapSighashType::All), c.taproot_signature_hash(0, &too_many_prevouts, None, None, TapSighashType::All),
Err(Error::PrevoutsSize) Err(Error::PrevoutsSize)
); );
let tx_out = TxOut::default(); let tx_out = TxOut::NULL;
let prevout = Prevouts::One(1, &tx_out); let prevout = Prevouts::One(1, &tx_out);
assert_eq!( assert_eq!(
c.taproot_signature_hash(0, &prevout, None, None, TapSighashType::All), c.taproot_signature_hash(0, &prevout, None, None, TapSighashType::All),

View File

@ -1691,11 +1691,11 @@ mod tests {
output: vec![ output: vec![
TxOut { TxOut {
value: output_0_val, value: output_0_val,
..Default::default() script_pubkey: ScriptBuf::new()
}, },
TxOut { TxOut {
value: output_1_val, value: output_1_val,
..Default::default() script_pubkey: ScriptBuf::new()
}, },
], ],
}, },
@ -1730,11 +1730,11 @@ mod tests {
output: vec![ output: vec![
TxOut { TxOut {
value: prev_output_val, value: prev_output_val,
..Default::default() script_pubkey: ScriptBuf::new()
}, },
TxOut { TxOut {
value: Amount::from_sat(190_303_501_938), value: Amount::from_sat(190_303_501_938),
..Default::default() script_pubkey: ScriptBuf::new()
}, },
], ],
}), }),
@ -1788,7 +1788,7 @@ mod tests {
version: 2, version: 2,
lock_time: absolute::LockTime::ZERO, lock_time: absolute::LockTime::ZERO,
input: vec![TxIn::default(), TxIn::default()], input: vec![TxIn::default(), TxIn::default()],
output: vec![TxOut::default()], output: vec![TxOut::NULL],
}; };
let mut psbt = PartiallySignedTransaction::from_unsigned_tx(unsigned_tx).unwrap(); let mut psbt = PartiallySignedTransaction::from_unsigned_tx(unsigned_tx).unwrap();