Skip to content

Commit

Permalink
wip: move to effective_value from rust-bitcoin
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Feb 20, 2024
1 parent 228824b commit 9d39147
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,14 +45,16 @@ pub struct WeightedUtxo {
pub utxo: TxOut,
}

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

Expand Down

0 comments on commit 9d39147

Please sign in to comment.