Skip to content

Commit

Permalink
Implement BnB search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Feb 6, 2024
1 parent dbc77f3 commit 5c38bb5
Show file tree
Hide file tree
Showing 5 changed files with 767 additions and 158 deletions.
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ keywords = ["crypto", "bitcoin"]
readme = "README.md"

[dependencies]
bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", branch = "add-effective-value-calculation" }
bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin", rev="254fafdf71b30ae9156b43d57b336cf4a251ed33" }
rand = {version = "0.8.5", default-features = false, optional = true}

[dev-dependencies]
rust-bitcoin-coin-selection = {path = ".", features = ["rand"]}
rand = "0.8.5"

[patch.crates-io]
bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", branch = "add-effective-value-calculation" }
bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", branch = "add-effective-value-calculation" }
bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", branch = "add-effective-value-calculation" }
bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", branch = "add-effective-value-calculation" }
bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="254fafdf71b30ae9156b43d57b336cf4a251ed33" }
bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="254fafdf71b30ae9156b43d57b336cf4a251ed33" }
bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="254fafdf71b30ae9156b43d57b336cf4a251ed33" }
bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="254fafdf71b30ae9156b43d57b336cf4a251ed33" }

[[bench]]
name = "coin_selection"
harness = false
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ As discussed in the literature above, ideally we want to choose a selection from

## Benchmarks

<<<<<<< HEAD
To run the benchmarks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.
=======
To run the benchmarks use: `cargo bench`.

### performance comparison

A basic performance comparison between this current [Rust BnB](https://github.com/p2pderivatives/rust-bitcoin-coin-selection/pull/28/files#diff-9098d62be93e83524a8371395c973d761a95000d1c295f600a8c808e917c16d9R122) implementation and the [Bitcoin Core](https://github.com/bitcoin/bitcoin/blob/4b1196a9855dcd188a24f393aa2fa21e2d61f061/src/wallet/coinselection.cpp#L76) version using commodity hardware (My rather old laptop).

|implementation|pool size|ns/iter|
|-------------:|---------|-------|
| Rust BnB| 1,000|823,680|
| C++ Core BnB| 1,000|816,374|
>>>>>>> 4ede8d5 (Add cost of change param)
## Minimum Supported Rust Version (MSRV)

Expand Down
47 changes: 47 additions & 0 deletions benches/coin_selection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use bitcoin::Amount;
use bitcoin::FeeRate;
use bitcoin::ScriptBuf;
use bitcoin::TxOut;
use bitcoin::Weight;
use rust_bitcoin_coin_selection::select_coins_bnb;
use rust_bitcoin_coin_selection::WeightedUtxo;

pub fn criterion_benchmark(c: &mut Criterion) {
// https://github.com/bitcoin/bitcoin/blob/f3bc1a72825fe2b51f4bc20e004cef464f05b965/src/wallet/coinselection.h#L18
let cost_of_change = Amount::from_sat(50_000);

let one = WeightedUtxo {
satisfaction_weight: Weight::ZERO,
utxo: TxOut { value: Amount::from_sat(1_000), script_pubkey: ScriptBuf::new() },
};

let two = WeightedUtxo {
satisfaction_weight: Weight::ZERO,
utxo: TxOut { value: Amount::from_sat(3), script_pubkey: ScriptBuf::new() },
};

let target = Amount::from_sat(1_003);
let mut utxo_pool = vec![one; 1000];
utxo_pool.push(two);

c.bench_function("bnb 1000", |b| {
b.iter(|| {
let result = select_coins_bnb(
black_box(target),
black_box(cost_of_change),
black_box(FeeRate::ZERO),
black_box(FeeRate::ZERO),
black_box(&mut utxo_pool),
)
.unwrap();
assert_eq!(2, result.len());
assert_eq!(Amount::from_sat(1_000), result[0].utxo.value);
assert_eq!(Amount::from_sat(3), result[1].utxo.value);
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading

0 comments on commit 5c38bb5

Please sign in to comment.