Skip to content

Commit

Permalink
wip: add backtrack conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Dec 7, 2023
1 parent d256cb1 commit c98543c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ pub fn select_coins_bnb(
_fee_rate: FeeRate,
weighted_utxos: &mut [WeightedUtxo],
) -> Option<Vec<WeightedUtxo>> {
let _value = Amount::ZERO;
let selection: Vec<WeightedUtxo> = vec![];
let mut value = Amount::ZERO;
let mut selection: Vec<WeightedUtxo> = vec![];
let available_value: Amount = weighted_utxos.iter().map(|u| u.utxo.value).sum();

if available_value < target {
Expand All @@ -161,14 +161,26 @@ pub fn select_coins_bnb(

weighted_utxos.sort_by(|a, b| b.utxo.value.cmp(&a.utxo.value));

for (_i, _utxo) in weighted_utxos.iter().enumerate() {
for (_i, utxo) in weighted_utxos.iter().enumerate() {
// backtrack
//
// There are three conditions for backtracking:
//
// * value meets or exceeded target
// * leaf node (nothing left to explorer)
// * not enough value to make it to target
if value >= target
|| available_value + value < target
|| utxo == weighted_utxos.last().unwrap()
{
// backtrack
}

// proceed with depth first search.
else {
selection.push(utxo.clone());
value += utxo.utxo.value;
}
}

Some(selection)
Expand Down

0 comments on commit c98543c

Please sign in to comment.