Skip to content

Commit

Permalink
Use latest bitcoin version
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Jun 29, 2024
1 parent 84c08bc commit b7d7df0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ 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]
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
23 changes: 13 additions & 10 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -56,7 +64,7 @@ impl WeightedUtxo {
}

fn calculate_fee(&self, fee_rate: FeeRate) -> Option<Amount> {
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)
}

Expand Down

0 comments on commit b7d7df0

Please sign in to comment.