[wallet] Use TXIN_DEFAULT_WEIGHT constant in coin selection

Replace all the occurences of `serialize(&txin)`
with TXIN_DEFAULT_WEIGHT.
This commit is contained in:
Daniela Brozzoni 2020-11-08 15:46:27 +01:00
parent 36c5a4dc0c
commit a86706d1a6
No known key found for this signature in database
GPG Key ID: B48B97D7E7590A25

View File

@ -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<D: Database> CoinSelectionAlgorithm<D> 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<D: Database> CoinSelectionAlgorithm<D> 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;