Skip to content

Commit 1717c0a

Browse files
committed
wip
1 parent 8b2e1bb commit 1717c0a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/branch_and_bound.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
172172
let upper_bound = target.checked_add(cost_of_change)?;
173173

174174
// Creates a tuple of (effective_value, waste, weighted_utxo)
175-
let mut w_utxos: Vec<(u64, i64, &Utxo)> = weighted_utxos
175+
let w_utxos = weighted_utxos
176176
.iter()
177177
// calculate effective_value and waste for each w_utxo.
178178
.map(|wu| (wu.effective_value(fee_rate), wu.waste(fee_rate, long_term_fee_rate), wu))
@@ -183,20 +183,24 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
183183
// filter out all effective_values that are negative.
184184
.filter(|(eff_val, _, _)| eff_val.is_positive())
185185
// all utxo effective_values are now positive (see previous step) - cast to unsigned.
186-
.map(|(eff_val, waste, wu)| (eff_val.to_sat() as u64, waste.to_sat(), wu))
187-
.collect();
186+
.map(|(eff_val, waste, wu)| (eff_val.to_unsigned().unwrap(), waste, wu));
187+
188+
let available_value = w_utxos.clone().map(|(ev, _, _)| ev).checked_sum()?;
189+
190+
if available_value < target || target == Amount::ZERO {
191+
return None;
192+
}
193+
194+
let mut available_value = available_value.to_sat();
195+
196+
// cast from Amount/SignedAmount to u64/i64 for more perfomant operations.
197+
let mut w_utxos: Vec<(u64, i64, &Utxo)> = w_utxos.map(|(e, w, u)| (e.to_sat(), w.to_sat(), u)).collect();
188198

189199
// descending sort by effective_value using satisfaction weight as tie breaker.
190200
w_utxos.sort_by(|a, b| {
191201
b.0.cmp(&a.0).then(b.2.satisfaction_weight().cmp(&a.2.satisfaction_weight()))
192202
});
193203

194-
let mut available_value: u64 = w_utxos.clone().into_iter().map(|(ev, _, _)| ev).sum();
195-
196-
if available_value < target.to_sat() || target == Amount::ZERO {
197-
return None;
198-
}
199-
200204
while iteration < ITERATION_LIMIT {
201205
backtrack = false;
202206

0 commit comments

Comments
 (0)