From a86706d1a6d72e13657fabc8ea1c5f541ef91277 Mon Sep 17 00:00:00 2001 From: Daniela Brozzoni Date: Sun, 8 Nov 2020 15:46:27 +0100 Subject: [PATCH] [wallet] Use TXIN_DEFAULT_WEIGHT constant in coin selection Replace all the occurences of `serialize(&txin)` with TXIN_DEFAULT_WEIGHT. --- src/wallet/coin_selection.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wallet/coin_selection.rs b/src/wallet/coin_selection.rs index 4f927f6c..a77e21ad 100644 --- a/src/wallet/coin_selection.rs +++ b/src/wallet/coin_selection.rs @@ -42,7 +42,6 @@ //! ```no_run //! # use std::str::FromStr; //! # use bitcoin::*; -//! # use bitcoin::consensus::serialize; //! # use bdk::wallet::coin_selection::*; //! # use bdk::database::Database; //! # use bdk::*; @@ -70,7 +69,7 @@ //! }; //! //! **selected_amount += utxo.txout.value; -//! **additional_weight += serialize(&txin).len() * 4 + weight; +//! **additional_weight += TXIN_BASE_WEIGHT + weight; //! //! Some(( //! txin, @@ -106,7 +105,6 @@ //! # Ok::<(), bdk::Error>(()) //! ``` -use bitcoin::consensus::encode::serialize; use bitcoin::{Script, TxIn}; use crate::database::Database; @@ -209,7 +207,7 @@ impl CoinSelectionAlgorithm for LargestFirstCoinSelection { witness: vec![], }; - **fee_amount += calc_fee_bytes(serialize(&new_in).len() * 4 + weight); + **fee_amount += calc_fee_bytes(TXIN_BASE_WEIGHT + weight); **selected_amount += utxo.txout.value; log::debug!( @@ -238,6 +236,10 @@ impl CoinSelectionAlgorithm for LargestFirstCoinSelection { } } +// Base weight of a Txin, not counting the weight needed for satisfaying it. +// prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes) + script_len (1 bytes) +pub const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4 + 1) * 4; + #[cfg(test)] mod test { use std::str::FromStr;