3232//! # use bdk::*;
3333//! # use bdk::wallet::coin_selection::decide_change;
3434//! # use anyhow::Error;
35- //! # const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4) * 4;
3635//! #[derive(Debug)]
3736//! struct AlwaysSpendEverything;
3837//!
5554//! |(selected_amount, additional_weight), weighted_utxo| {
5655//! **selected_amount += weighted_utxo.utxo.txout().value;
5756//! **additional_weight += Weight::from_wu(
58- //! (TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as u64,
57+ //! (TxIn::default().segwit_weight() + weighted_utxo.satisfaction_weight)
58+ //! as u64,
5959//! );
6060//! Some(weighted_utxo.utxo)
6161//! },
@@ -109,6 +109,7 @@ use crate::WeightedUtxo;
109109use alloc:: vec:: Vec ;
110110use bitcoin:: consensus:: encode:: serialize;
111111use bitcoin:: OutPoint ;
112+ use bitcoin:: TxIn ;
112113use bitcoin:: { Script , Weight } ;
113114
114115use core:: convert:: TryInto ;
@@ -119,10 +120,6 @@ use rand::seq::SliceRandom;
119120/// overridden
120121pub type DefaultCoinSelectionAlgorithm = BranchAndBoundCoinSelection ;
121122
122- // Base weight of a Txin, not counting the weight needed for satisfying it.
123- // prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes)
124- pub ( crate ) const TXIN_BASE_WEIGHT : usize = ( 32 + 4 + 4 ) * 4 ;
125-
126123/// Errors that can be thrown by the [`coin_selection`](crate::wallet::coin_selection) module
127124#[ derive( Debug ) ]
128125pub enum Error {
@@ -345,7 +342,8 @@ fn select_sorted_utxos(
345342 |( selected_amount, fee_amount) , ( must_use, weighted_utxo) | {
346343 if must_use || * * selected_amount < target_amount + * * fee_amount {
347344 * * fee_amount += fee_rate. fee_wu ( Weight :: from_wu (
348- ( TXIN_BASE_WEIGHT + weighted_utxo. satisfaction_weight ) as u64 ,
345+ ( TxIn :: default ( ) . segwit_weight ( ) + weighted_utxo. satisfaction_weight )
346+ as u64 ,
349347 ) ) ;
350348 * * selected_amount += weighted_utxo. utxo . txout ( ) . value ;
351349 Some ( weighted_utxo. utxo )
@@ -388,7 +386,7 @@ struct OutputGroup {
388386impl OutputGroup {
389387 fn new ( weighted_utxo : WeightedUtxo , fee_rate : FeeRate ) -> Self {
390388 let fee = fee_rate. fee_wu ( Weight :: from_wu (
391- ( TXIN_BASE_WEIGHT + weighted_utxo. satisfaction_weight ) as u64 ,
389+ ( TxIn :: default ( ) . segwit_weight ( ) + weighted_utxo. satisfaction_weight ) as u64 ,
392390 ) ) ;
393391 let effective_value = weighted_utxo. utxo . txout ( ) . value as i64 - fee as i64 ;
394392 OutputGroup {
@@ -738,7 +736,7 @@ mod test {
738736 use core:: str:: FromStr ;
739737
740738 use bdk_chain:: ConfirmationTime ;
741- use bitcoin:: { OutPoint , ScriptBuf , TxOut } ;
739+ use bitcoin:: { OutPoint , ScriptBuf , TxIn , TxOut } ;
742740
743741 use super :: * ;
744742 use crate :: types:: * ;
@@ -749,9 +747,9 @@ mod test {
749747 use rand:: seq:: SliceRandom ;
750748 use rand:: { Rng , RngCore , SeedableRng } ;
751749
752- // n. of items on witness (1WU) + signature len (1WU) + signature and sighash (72WU)
753- // + pubkey len (1WU) + pubkey (33WU) + script sig len (1 byte, 4WU)
754- const P2WPKH_SATISFACTION_SIZE : usize = 1 + 1 + 72 + 1 + 33 + 4 ;
750+ // n. of items on witness (1WU) signature and sighash (72WU)
751+ // + pubkey len (1WU) + pubkey (33WU)
752+ const P2WPKH_SATISFACTION_SIZE : usize = 1 + 72 + 1 + 33 ;
755753
756754 const FEE_AMOUNT : u64 = 50 ;
757755
@@ -1240,7 +1238,7 @@ mod test {
12401238
12411239 assert_eq ! ( result. selected. len( ) , 1 ) ;
12421240 assert_eq ! ( result. selected_amount( ) , 100_000 ) ;
1243- let input_size = ( TXIN_BASE_WEIGHT + P2WPKH_SATISFACTION_SIZE ) . vbytes ( ) ;
1241+ let input_size = ( TxIn :: default ( ) . segwit_weight ( ) + P2WPKH_SATISFACTION_SIZE ) . vbytes ( ) ;
12441242 // the final fee rate should be exactly the same as the fee rate given
12451243 assert ! ( ( 1.0 - ( result. fee_amount as f32 / input_size as f32 ) ) . abs( ) < f32 :: EPSILON ) ;
12461244 }
0 commit comments