Merge rust-bitcoin/rust-bitcoin#1838: Move and rename TxOut default trait to a const called NULL

75b3f19b96 Move and rename TxOut default trait to a const called NULL (yancy)

Pull request description:

  Create an associated constant `const TxOut::NULL` for consensus signing code and remove the default trait.  Note I tried to deprecate the `default()` fn instead of just removing it but it doesn't seem to be possible.  Also because `TxOut::NULL` is `const`, `ScriptBuf::new()` needed to be changed to `const fn`.

ACKs for top commit:
  apoelstra:
    ACK 75b3f19b96
  Kixunil:
    ACK 75b3f19b96

Tree-SHA512: ff61a2b1641a1ba32f183c27205af2d868dbc2eb47cf758c3d8315329d2c23e0b8a82ea0ab59d1de9add0d238f927165e2e4df014aab1ef066d74d4feda0700b
This commit is contained in:
Andrew Poelstra 2023-05-09 17:19:28 +00:00
commit d93e781148
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
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 {
/// 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.
pub fn with_capacity(capacity: usize) -> Self { ScriptBuf(Vec::with_capacity(capacity)) }

View File

@ -484,6 +484,10 @@ pub struct 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
///
/// 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`].
///
/// This type forces the caller to handle SIGHASH_SINGLE bug case.

View File

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

View File

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