diff --git a/src/branch_and_bound.rs b/src/branch_and_bound.rs index 256ac5e..954e44e 100644 --- a/src/branch_and_bound.rs +++ b/src/branch_and_bound.rs @@ -142,6 +142,8 @@ pub fn select_coins_bnb( let mut index_selection: Vec = vec![]; let mut best_selection: Option> = 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 @@ -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 @@ -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()); + } }