5
5
//! This module introduces the Branch and Bound Coin-Selection Algorithm.
6
6
7
7
use bitcoin:: amount:: CheckedSum ;
8
- use bitcoin:: { Amount , FeeRate , SignedAmount } ;
8
+ use bitcoin:: { Amount , FeeRate } ;
9
9
10
10
use crate :: { Return , WeightedUtxo } ;
11
11
@@ -193,8 +193,9 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
193
193
194
194
let mut available_value = available_value. to_sat ( ) ;
195
195
196
- // cast from Amount/SignedAmount to u64/i64 for more perfomant operations.
196
+ // cast from Amount/SignedAmount to u64/i64 for more performant operations.
197
197
let mut w_utxos: Vec < ( u64 , i64 , & Utxo ) > = w_utxos. map ( |( e, w, u) | ( e. to_sat ( ) , w. to_sat ( ) , u) ) . collect ( ) ;
198
+ let target = target. to_sat ( ) ;
198
199
199
200
// descending sort by effective_value using satisfaction weight as tie breaker.
200
201
w_utxos. sort_by ( |a, b| {
@@ -209,7 +210,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
209
210
// unchecked_add is used here for performance. Before entering the search loop, all
210
211
// utxos are summed and checked for overflow. Since there was no overflow then, any
211
212
// subset of addition will not overflow.
212
- if available_value + value < target. to_sat ( )
213
+ if available_value + value < target
213
214
// Provides an upper bound on the excess value that is permissible.
214
215
// Since value is lost when we create a change output due to increasing the size of the
215
216
// transaction by an output (the change output), we accept solutions that may be
@@ -232,11 +233,10 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
232
233
}
233
234
// * value meets or exceeds the target.
234
235
// Record the solution and the waste then continue.
235
- else if value >= target. to_sat ( ) {
236
+ else if value >= target {
236
237
backtrack = true ;
237
238
238
- // TODO checked
239
- let waste: i64 = value as i64 - ( target. to_sat ( ) as i64 ) ;
239
+ let waste: i64 = ( value as i64 ) . checked_sub ( target as i64 ) ?;
240
240
current_waste = current_waste. checked_add ( waste) ?;
241
241
242
242
// Check if index_selection is better than the previous known best, and
@@ -269,8 +269,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
269
269
let ( eff_value, utxo_waste, _) = w_utxos[ index] ;
270
270
current_waste = current_waste. checked_sub ( utxo_waste) ?;
271
271
272
- // TODO this checked
273
- value = value - eff_value;
272
+ value = value. checked_sub ( eff_value) ?;
274
273
index_selection. pop ( ) . unwrap ( ) ;
275
274
}
276
275
// * Add next node to the inclusion branch.
0 commit comments