b61adf7ca4 Introduce TxOutExt trait (Tobin C. Harding)
8b089ffe40 Run the formatter (Tobin C. Harding)
02c7d504fa Add tmp module around TxOut impl block (Tobin C. Harding)
fa946796eb Create a separate TxOut impl block (Tobin C. Harding)

Pull request description:

  This one is pretty easy. Leaves the actual move of `TxOut` for later.

ACKs for top commit:
  Kixunil:
    ACK b61adf7ca4
  apoelstra:
    ACK b61adf7ca4 successfully ran local tests

Tree-SHA512: 99c23f1cebdade0f4298bf46b6af66c02594f79096b82550a7d17d5a7d058afba3738c676deac2712cfec43798813a6e49ac53e39760677777c6910ccedf5bc8
This commit is contained in:
merge-script 2024-08-27 19:46:30 +00:00
commit 02f0111c4e
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 49 additions and 43 deletions

View File

@ -352,53 +352,59 @@ 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 this output.
///
/// Keep in mind that when adding a [`TxOut`] to a [`Transaction`] the total weight of the
/// transaction might increase more than `TxOut::weight`. This happens when the new output added
/// causes the output length `VarInt` to increase its encoding length.
///
/// # Panics
///
/// If output size * 4 overflows, this should never happen under normal conditions. Use
/// `Weght::from_vb_checked(self.size().to_u64())` if you are concerned.
pub fn weight(&self) -> Weight {
// Size is equivalent to virtual size since all bytes of a TxOut are non-witness bytes.
Weight::from_vb(self.size().to_u64()).expect("should never happen under normal conditions")
}
crate::internal_macros::define_extension_trait! {
/// Extension functionality for the [`TxOut`] type.
pub trait TxOutExt impl for TxOut {
/// The weight of this output.
///
/// Keep in mind that when adding a [`TxOut`] to a [`Transaction`] the total weight of the
/// transaction might increase more than `TxOut::weight`. This happens when the new output added
/// causes the output length `VarInt` to increase its encoding length.
///
/// # Panics
///
/// If output size * 4 overflows, this should never happen under normal conditions. Use
/// `Weght::from_vb_checked(self.size().to_u64())` if you are concerned.
fn weight(&self) -> Weight {
// Size is equivalent to virtual size since all bytes of a TxOut are non-witness bytes.
Weight::from_vb(self.size().to_u64())
.expect("should never happen under normal conditions")
}
/// Returns the total number of bytes that this output contributes to a transaction.
///
/// There is no difference between base size vs total size for outputs.
pub fn size(&self) -> usize { size_from_script_pubkey(&self.script_pubkey) }
/// Returns the total number of bytes that this output contributes to a transaction.
///
/// There is no difference between base size vs total size for outputs.
fn size(&self) -> usize { size_from_script_pubkey(&self.script_pubkey) }
/// Creates a `TxOut` with given script and the smallest possible `value` that is **not** dust
/// per current Core policy.
///
/// Dust depends on the -dustrelayfee value of the Bitcoin Core node you are broadcasting to.
/// This function uses the default value of 0.00003 BTC/kB (3 sat/vByte).
///
/// To use a custom value, use [`minimal_non_dust_custom`].
///
/// [`minimal_non_dust_custom`]: TxOut::minimal_non_dust_custom
pub fn minimal_non_dust(script_pubkey: ScriptBuf) -> Self {
TxOut { value: script_pubkey.minimal_non_dust(), script_pubkey }
}
/// Creates a `TxOut` with given script and the smallest possible `value` that is **not** dust
/// per current Core policy.
///
/// Dust depends on the -dustrelayfee value of the Bitcoin Core node you are broadcasting to.
/// This function uses the default value of 0.00003 BTC/kB (3 sat/vByte).
///
/// To use a custom value, use [`minimal_non_dust_custom`].
///
/// [`minimal_non_dust_custom`]: TxOut::minimal_non_dust_custom
fn minimal_non_dust(script_pubkey: ScriptBuf) -> Self {
TxOut { value: script_pubkey.minimal_non_dust(), script_pubkey }
}
/// Creates a `TxOut` with given script and the smallest possible `value` that is **not** dust
/// per current Core policy.
///
/// Dust depends on the -dustrelayfee value of the Bitcoin Core node you are broadcasting to.
/// This function lets you set the fee rate used in dust calculation.
///
/// The current default value in Bitcoin Core (as of v26) is 3 sat/vByte.
///
/// To use the default Bitcoin Core value, use [`minimal_non_dust`].
///
/// [`minimal_non_dust`]: TxOut::minimal_non_dust
pub fn minimal_non_dust_custom(script_pubkey: ScriptBuf, dust_relay_fee: FeeRate) -> Self {
TxOut { value: script_pubkey.minimal_non_dust_custom(dust_relay_fee), script_pubkey }
/// Creates a `TxOut` with given script and the smallest possible `value` that is **not** dust
/// per current Core policy.
///
/// Dust depends on the -dustrelayfee value of the Bitcoin Core node you are broadcasting to.
/// This function lets you set the fee rate used in dust calculation.
///
/// The current default value in Bitcoin Core (as of v26) is 3 sat/vByte.
///
/// To use the default Bitcoin Core value, use [`minimal_non_dust`].
///
/// [`minimal_non_dust`]: TxOut::minimal_non_dust
fn minimal_non_dust_custom(script_pubkey: ScriptBuf, dust_relay_fee: FeeRate) -> Self {
TxOut { value: script_pubkey.minimal_non_dust_custom(dust_relay_fee), script_pubkey }
}
}
}