Skip to content

Commit

Permalink
Add coin struct
Browse files Browse the repository at this point in the history
The coin struct pre-processes the attributes needed for selection
(effective_value and waste)
  • Loading branch information
yancyribbens committed Mar 20, 2024
1 parent cbdb656 commit d71f6a9
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 514 deletions.
33 changes: 21 additions & 12 deletions benches/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use bitcoin::Amount;
use bitcoin::FeeRate;
use bitcoin::ScriptBuf;
use bitcoin::SignedAmount;
use bitcoin::TxOut;
use bitcoin::Weight;
use rust_bitcoin_coin_selection::select_coins_bnb;
use rust_bitcoin_coin_selection::WeightedUtxo;
use rust_bitcoin_coin_selection::Coin;

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 no_op = TxOut { value: Amount::ZERO, script_pubkey: ScriptBuf::new() };
let fee_rate = FeeRate::ZERO;
let long_term_fee_rate = FeeRate::ZERO;
let waste = SignedAmount::ZERO;

let one = Coin {
utxo: no_op.clone(),
effective_value: Amount::from_sat(1_000),
waste,
fee_rate,
long_term_fee_rate,
};

let two = WeightedUtxo {
satisfaction_weight: Weight::ZERO,
utxo: TxOut { value: Amount::from_sat(3), script_pubkey: ScriptBuf::new() },
let two = Coin {
utxo: no_op.clone(),
effective_value: Amount::from_sat(3),
waste,
fee_rate,
long_term_fee_rate,
};

let target = Amount::from_sat(1_003);
Expand All @@ -31,16 +42,14 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let result: Vec<_> = select_coins_bnb(
black_box(target),
black_box(cost_of_change),
black_box(FeeRate::ZERO),
black_box(FeeRate::ZERO),
black_box(&utxo_pool),
)
.unwrap()
.collect();

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);
assert_eq!(Amount::from_sat(1_000), result[0].effective_value);
assert_eq!(Amount::from_sat(3), result[1].effective_value);
})
});
}
Expand Down
Loading

0 comments on commit d71f6a9

Please sign in to comment.