From fa946796ebd9c0d42afaac9b8b2169cb6f851af0 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 27 Aug 2024 16:34:36 +1000 Subject: [PATCH 1/4] Create a separate TxOut impl block Everything except the associated const is going to stay here in `bitcoin` when we move `TxOut` to `primitives`. --- bitcoin/src/blockdata/transaction.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index a2a8e671a..c4a4600dd 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -352,7 +352,9 @@ 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() }; +} +impl TxOut { /// The weight of this output. /// /// Keep in mind that when adding a [`TxOut`] to a [`Transaction`] the total weight of the From 02c7d504fadfc31fed588393d57dfd89f3a0c2b6 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 27 Aug 2024 16:25:35 +1000 Subject: [PATCH 2/4] Add tmp module around TxOut impl block The two `TxOut` fields are public and there are no construtors or getters to move, only the associated const `NULL`. Add a tmp module around the big impl block so we can trick the formatter into indenting before we add the extension trait. --- bitcoin/src/blockdata/transaction.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index c4a4600dd..18be1d144 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -354,6 +354,8 @@ impl TxOut { TxOut { value: Amount::from_sat(0xffffffffffffffff), script_pubkey: ScriptBuf::new() }; } +mod tmp { +use super::*; impl TxOut { /// The weight of this output. /// @@ -403,6 +405,7 @@ impl TxOut { TxOut { value: script_pubkey.minimal_non_dust_custom(dust_relay_fee), script_pubkey } } } +} #[cfg(feature = "arbitrary")] impl<'a> Arbitrary<'a> for TxOut { From 8b089ffe4058015eb0fc135c658b2334f6166d83 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 27 Aug 2024 16:27:41 +1000 Subject: [PATCH 3/4] Run the formatter Run `cargo +nightly fmt`, no manual changes. --- bitcoin/src/blockdata/transaction.rs | 93 ++++++++++++++-------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 18be1d144..5284e8988 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -355,57 +355,58 @@ impl TxOut { } mod tmp { -use super::*; -impl 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. - 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") - } + use super::*; + impl 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. + 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") + } - /// 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. + pub 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 + 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 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 + 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 } + } } } -} #[cfg(feature = "arbitrary")] impl<'a> Arbitrary<'a> for TxOut { From b61adf7ca4fefd6c5fe3879de7baecf1ffde9d9b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 27 Aug 2024 16:32:27 +1000 Subject: [PATCH 4/4] Introduce TxOutExt trait In preparation for moving the `TxOut` type over to `primitives` ad an extension trait as is becoming customary. --- bitcoin/src/blockdata/transaction.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 5284e8988..5221f4a38 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -354,9 +354,9 @@ impl TxOut { TxOut { value: Amount::from_sat(0xffffffffffffffff), script_pubkey: ScriptBuf::new() }; } -mod tmp { - use super::*; - impl TxOut { +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 @@ -367,7 +367,7 @@ mod tmp { /// /// 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 { + 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") @@ -376,7 +376,7 @@ mod tmp { /// 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) } + 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. @@ -387,7 +387,7 @@ mod tmp { /// 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 { + fn minimal_non_dust(script_pubkey: ScriptBuf) -> Self { TxOut { value: script_pubkey.minimal_non_dust(), script_pubkey } } @@ -402,7 +402,7 @@ mod tmp { /// 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 { + 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 } } }