Skip to content

Commit

Permalink
Add upper_bound
Browse files Browse the repository at this point in the history
The upper_bound is calculated as target + cost_of_change.
  • Loading branch information
yancyribbens committed Feb 3, 2024
1 parent 56e0e5d commit 512f7b7
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ pub fn select_coins_bnb(
let mut index_selection: Vec<usize> = vec![];
let mut best_selection: Option<Vec<usize>> = None;

let upper_bound = target.checked_add(cost_of_change)?;

// Create a tuple of (effective_value, waste, weighted_utxo)
// * filter out effective values and wastes that are None
// * filter out negative effective values
Expand Down Expand Up @@ -237,7 +239,7 @@ pub fn select_coins_bnb(
// it's high fee_rate environment. During low fee environments, a utxo may
// have negative waste, therefore adding more utxos in such an environment
// may still result in reduced waste.
else if value > target.unchecked_add(cost_of_change) //TODO we can check this for overflow
else if value > upper_bound //TODO we can check this for overflow
//before the loop begins by adding
//target to utxo_pool sum.
|| current_waste > best_waste && fee_rate > long_term_fee_rate
Expand Down Expand Up @@ -707,4 +709,29 @@ mod tests {
);
assert!(list.is_none());
}

#[test]
fn upper_bound_overflow() {
// the upper_bound is target + cost_of_change.
// adding these two together returns NONE on overflow.
let target = Amount::MAX;
let cost_of_change = Amount::MAX;

let satisfaction_weight = Weight::from_wu(204);
let mut weighted_utxos = vec![
WeightedUtxo {
satisfaction_weight,
utxo: TxOut { value: target, script_pubkey: ScriptBuf::new() },
},
];

let list = select_coins_bnb(
target,
cost_of_change,
FeeRate::ZERO,
FeeRate::ZERO,
&mut weighted_utxos,
);
assert!(list.is_none());
}
}

0 comments on commit 512f7b7

Please sign in to comment.