From 9d39147e3068ac9f49a290c5c92eb2860004ec76 Mon Sep 17 00:00:00 2001 From: yancy Date: Tue, 20 Feb 2024 11:04:40 +0100 Subject: [PATCH] wip: move to effective_value from rust-bitcoin --- Cargo.toml | 10 +++++----- src/branch_and_bound.rs | 3 ++- src/lib.rs | 15 ++++++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab7e9af..23c175f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["crypto", "bitcoin"] readme = "README.md" [dependencies] -bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" } +bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" } rand = {version = "0.8.5", default-features = false, optional = true} [dev-dependencies] @@ -24,10 +24,10 @@ rust-bitcoin-coin-selection = {path = ".", features = ["rand"]} rand = "0.8.5" [patch.crates-io] -bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" } -bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" } -bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" } -bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" } +bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" } +bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" } +bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" } +bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="14e3bc" } [[bench]] name = "coin_selection" diff --git a/src/branch_and_bound.rs b/src/branch_and_bound.rs index f9a9ffb..8923c65 100644 --- a/src/branch_and_bound.rs +++ b/src/branch_and_bound.rs @@ -9,6 +9,7 @@ use bitcoin::amount::CheckedSum; use bitcoin::Amount; use bitcoin::FeeRate; use bitcoin::SignedAmount; +use bitcoin::blockdata::transaction::effective_value; /// Select coins bnb performs a depth first branch and bound search. The search traverses a /// binary tree with a maximum depth n where n is the size of the UTXO pool to be searched. @@ -159,7 +160,7 @@ pub fn select_coins_bnb( let mut w_utxos: Vec<(Amount, SignedAmount, &WeightedUtxo)> = weighted_utxos .iter() // calculate effective_value and waste for each w_utxo. - .map(|wu| (wu.effective_value(fee_rate), wu.waste(fee_rate, long_term_fee_rate), wu)) + .map(|wu| (effective_value(fee_rate, wu.satisfaction_weight, wu.utxo.value), wu.waste(fee_rate, long_term_fee_rate), wu)) // remove utxos that either had an error in the effective_value or waste calculation. .filter(|(eff_val, waste, _)| !eff_val.is_none() && !waste.is_none()) // unwrap the option type since we know they are not None (see previous step). diff --git a/src/lib.rs b/src/lib.rs index ac78061..40f3822 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,6 @@ use bitcoin::Weight; pub use crate::branch_and_bound::select_coins_bnb; use crate::single_random_draw::select_coins_srd; -use bitcoin::blockdata::transaction::TxIn; use rand::thread_rng; // https://github.com/bitcoin/bitcoin/blob/f722a9bd132222d9d5cd503b5af25c905b205cdb/src/wallet/coinselection.h#L20 @@ -46,14 +45,16 @@ pub struct WeightedUtxo { pub utxo: TxOut, } -impl WeightedUtxo { - fn effective_value(&self, fee_rate: FeeRate) -> Option { - let signed_input_fee = self.calculate_fee(fee_rate)?.to_signed().ok()?; - self.utxo.value.to_signed().ok()?.checked_sub(signed_input_fee) - } +// Serialized length of a u32. +const SEQUENCE_SIZE: u64 = 4; +// The serialized lengths of txid and vout. +const OUTPOINT_SIZE: u64 = 32 + 4; +const TX_IN_BASE_WEIGHT: Weight = + Weight::from_vb_unwrap(OUTPOINT_SIZE + SEQUENCE_SIZE); +impl WeightedUtxo { fn calculate_fee(&self, fee_rate: FeeRate) -> Option { - let weight = self.satisfaction_weight.checked_add(TxIn::BASE_WEIGHT)?; + let weight = self.satisfaction_weight.checked_add(TX_IN_BASE_WEIGHT)?; fee_rate.checked_mul_by_weight(weight) }