From b7d7df0c717e3923a307365c0aac8536fce2108d Mon Sep 17 00:00:00 2001 From: yancy Date: Sat, 29 Jun 2024 17:35:58 -0500 Subject: [PATCH] Use latest bitcoin version --- Cargo.toml | 8 +------- src/branch_and_bound.rs | 23 +++++++++++++---------- src/lib.rs | 12 ++++++++++-- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2738661..b11f13e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ keywords = ["crypto", "bitcoin", "coinselection"] readme = "README.md" [dependencies] -bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", rev="2f109442e30d74fb7502e7fd1ce2075a67262cd5" } +bitcoin = "0.32.2" rand = {version = "0.8.5", default-features = false, optional = true} [dev-dependencies] @@ -22,12 +22,6 @@ criterion = "0.3" 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" } - [[bench]] name = "coin_selection" harness = false diff --git a/src/branch_and_bound.rs b/src/branch_and_bound.rs index 2dc6254..6390ee9 100644 --- a/src/branch_and_bound.rs +++ b/src/branch_and_bound.rs @@ -367,52 +367,55 @@ mod tests { #[test] fn select_coins_bnb_one() { - assert_coin_select("1 cBTC", &["0.01 BTC"]); + assert_coin_select("1 cBTC", &["0.01000000 BTC"]); } #[test] fn select_coins_bnb_two() { - assert_coin_select("2 cBTC", &["0.02 BTC"]); + assert_coin_select("2 cBTC", &["0.02000000 BTC"]); } #[test] fn select_coins_bnb_three() { - assert_coin_select("3 cBTC", &["0.02 BTC", "0.01 BTC"]); + assert_coin_select("3 cBTC", &["0.02000000 BTC", "0.01000000 BTC"]); } #[test] fn select_coins_bnb_four() { - assert_coin_select("4 cBTC", &["0.03 BTC", "0.01 BTC"]); + assert_coin_select("4 cBTC", &["0.03000000 BTC", "0.01000000 BTC"]); } #[test] fn select_coins_bnb_five() { - assert_coin_select("5 cBTC", &["0.03 BTC", "0.02 BTC"]); + assert_coin_select("5 cBTC", &["0.03000000 BTC", "0.02000000 BTC"]); } #[test] fn select_coins_bnb_six() { - assert_coin_select("6 cBTC", &["0.03 BTC", "0.02 BTC", "0.01 BTC"]); + assert_coin_select("6 cBTC", &["0.03000000 BTC", "0.02000000 BTC", "0.01000000 BTC"]); } #[test] fn select_coins_bnb_seven() { - assert_coin_select("7 cBTC", &["0.04 BTC", "0.02 BTC", "0.01 BTC"]); + assert_coin_select("7 cBTC", &["0.04000000 BTC", "0.02000000 BTC", "0.01000000 BTC"]); } #[test] fn select_coins_bnb_eight() { - assert_coin_select("8 cBTC", &["0.04 BTC", "0.03 BTC", "0.01 BTC"]); + assert_coin_select("8 cBTC", &["0.04000000 BTC", "0.03000000 BTC", "0.01000000 BTC"]); } #[test] fn select_coins_bnb_nine() { - assert_coin_select("9 cBTC", &["0.04 BTC", "0.03 BTC", "0.02 BTC"]); + assert_coin_select("9 cBTC", &["0.04000000 BTC", "0.03000000 BTC", "0.02000000 BTC"]); } #[test] fn select_coins_bnb_ten() { - assert_coin_select("10 cBTC", &["0.04 BTC", "0.03 BTC", "0.02 BTC", "0.01 BTC"]); + assert_coin_select( + "10 cBTC", + &["0.04000000 BTC", "0.03000000 BTC", "0.02000000 BTC", "0.01000000 BTC"], + ); } #[test] diff --git a/src/lib.rs b/src/lib.rs index e0ae215..ece3155 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,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; /// Trait that a UTXO struct must implement to be used as part of the coin selection @@ -36,6 +35,15 @@ pub trait Utxo: Clone { // https://github.com/bitcoin/bitcoin/blob/f722a9bd132222d9d5cd503b5af25c905b205cdb/src/wallet/coinselection.h#L20 const CHANGE_LOWER: Amount = Amount::from_sat(50_000); +// https://github.com/rust-bitcoin/rust-bitcoin/blob/35202ba51bef3236e6ed1007a0d2111265b6498c/bitcoin/src/blockdata/transaction.rs#L357 +const SEQUENCE_SIZE: u64 = 4; + +// https://github.com/rust-bitcoin/rust-bitcoin/blob/35202ba51bef3236e6ed1007a0d2111265b6498c/bitcoin/src/blockdata/transaction.rs#L92 +const OUT_POINT_SIZE: u64 = 32 + 4; + +// https://github.com/rust-bitcoin/rust-bitcoin/blob/35202ba51bef3236e6ed1007a0d2111265b6498c/bitcoin/src/blockdata/transaction.rs#L249 +const BASE_WEIGHT: Weight = Weight::from_vb_unwrap(OUT_POINT_SIZE + SEQUENCE_SIZE); + /// This struct contains the weight of all params needed to satisfy the UTXO. /// /// The idea of using a WeightUtxo type was inspired by the BDK implementation: @@ -56,7 +64,7 @@ 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(BASE_WEIGHT)?; fee_rate.checked_mul_by_weight(weight) }