@@ -161,18 +161,18 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
161
161
let mut index = 0 ;
162
162
let mut backtrack;
163
163
164
- let mut value = Amount :: ZERO ;
164
+ let mut value = 0 ;
165
165
166
- let mut current_waste: SignedAmount = SignedAmount :: ZERO ;
167
- let mut best_waste = SignedAmount :: MAX_MONEY ;
166
+ let mut current_waste: i64 = 0 ;
167
+ let mut best_waste: i64 = Amount :: MAX_MONEY . to_sat ( ) as i64 ;
168
168
169
169
let mut index_selection: Vec < usize > = vec ! [ ] ;
170
170
let mut best_selection: Vec < usize > = vec ! [ ] ;
171
171
172
172
let upper_bound = target. checked_add ( cost_of_change) ?;
173
173
174
174
// Creates a tuple of (effective_value, waste, weighted_utxo)
175
- let mut w_utxos: Vec < ( Amount , SignedAmount , & Utxo ) > = weighted_utxos
175
+ let mut w_utxos: Vec < ( u64 , i64 , & Utxo ) > = weighted_utxos
176
176
. iter ( )
177
177
// calculate effective_value and waste for each w_utxo.
178
178
. map ( |wu| ( wu. effective_value ( fee_rate) , wu. waste ( fee_rate, long_term_fee_rate) , wu) )
@@ -183,17 +183,17 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
183
183
// filter out all effective_values that are negative.
184
184
. filter ( |( eff_val, _, _) | eff_val. is_positive ( ) )
185
185
// all utxo effective_values are now positive (see previous step) - cast to unsigned.
186
- . map ( |( eff_val, waste, wu) | ( eff_val. to_unsigned ( ) . unwrap ( ) , waste, wu) )
186
+ . map ( |( eff_val, waste, wu) | ( eff_val. to_sat ( ) as u64 , waste. to_sat ( ) , wu) )
187
187
. collect ( ) ;
188
188
189
189
// descending sort by effective_value using satisfaction weight as tie breaker.
190
190
w_utxos. sort_by ( |a, b| {
191
191
b. 0 . cmp ( & a. 0 ) . then ( b. 2 . satisfaction_weight ( ) . cmp ( & a. 2 . satisfaction_weight ( ) ) )
192
192
} ) ;
193
193
194
- let mut available_value = w_utxos. clone ( ) . into_iter ( ) . map ( |( ev, _, _) | ev) . checked_sum ( ) ? ;
194
+ let mut available_value: u64 = w_utxos. clone ( ) . into_iter ( ) . map ( |( ev, _, _) | ev) . sum ( ) ;
195
195
196
- if available_value < target || target == Amount :: ZERO {
196
+ if available_value < target. to_sat ( ) || target == Amount :: ZERO {
197
197
return None ;
198
198
}
199
199
@@ -205,7 +205,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
205
205
// unchecked_add is used here for performance. Before entering the search loop, all
206
206
// utxos are summed and checked for overflow. Since there was no overflow then, any
207
207
// subset of addition will not overflow.
208
- if available_value. unchecked_add ( value) < target
208
+ if available_value + value < target. to_sat ( )
209
209
// Provides an upper bound on the excess value that is permissible.
210
210
// Since value is lost when we create a change output due to increasing the size of the
211
211
// transaction by an output (the change output), we accept solutions that may be
@@ -217,7 +217,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
217
217
//
218
218
// That is, the range includes solutions that exactly equal the target up to but not
219
219
// including values greater than target + cost_of_change.
220
- || value > upper_bound
220
+ || value > upper_bound. to_sat ( )
221
221
// if current_waste > best_waste, then backtrack. However, only backtrack if
222
222
// it's high fee_rate environment. During low fee environments, a utxo may
223
223
// have negative waste, therefore adding more utxos in such an environment
@@ -228,12 +228,11 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
228
228
}
229
229
// * value meets or exceeds the target.
230
230
// Record the solution and the waste then continue.
231
- else if value >= target {
231
+ else if value >= target. to_sat ( ) {
232
232
backtrack = true ;
233
233
234
- let v = value. to_signed ( ) . ok ( ) ?;
235
- let t = target. to_signed ( ) . ok ( ) ?;
236
- let waste: SignedAmount = v. checked_sub ( t) ?;
234
+ // TODO checked
235
+ let waste: i64 = value as i64 - ( target. to_sat ( ) as i64 ) ;
237
236
current_waste = current_waste. checked_add ( waste) ?;
238
237
239
238
// Check if index_selection is better than the previous known best, and
@@ -265,7 +264,9 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
265
264
assert_eq ! ( index, * index_selection. last( ) . unwrap( ) ) ;
266
265
let ( eff_value, utxo_waste, _) = w_utxos[ index] ;
267
266
current_waste = current_waste. checked_sub ( utxo_waste) ?;
268
- value = value. checked_sub ( eff_value) ?;
267
+
268
+ // TODO this checked
269
+ value = value - eff_value;
269
270
index_selection. pop ( ) . unwrap ( ) ;
270
271
}
271
272
// * Add next node to the inclusion branch.
@@ -275,7 +276,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
275
276
// unchecked sub is used her for performance.
276
277
// The bounds for available_value are at most the sum of utxos
277
278
// and at least zero.
278
- available_value = available_value. unchecked_sub ( eff_value) ;
279
+ available_value = available_value - eff_value;
279
280
280
281
// Check if we can omit the currently selected depending on if the last
281
282
// was omitted. Therefore, check if index_selection has a previous one.
@@ -290,7 +291,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
290
291
291
292
// unchecked add is used here for performance. Since the sum of all utxo values
292
293
// did not overflow, then any positive subset of the sum will not overflow.
293
- value = value. unchecked_add ( eff_value) ;
294
+ value = value - eff_value;
294
295
}
295
296
}
296
297
@@ -305,7 +306,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
305
306
fn index_to_utxo_list < Utxo : WeightedUtxo > (
306
307
iterations : u32 ,
307
308
index_list : Vec < usize > ,
308
- wu : Vec < ( Amount , SignedAmount , & Utxo ) > ,
309
+ wu : Vec < ( u64 , i64 , & Utxo ) > ,
309
310
) -> Return < Utxo > {
310
311
let mut result: Vec < _ > = Vec :: new ( ) ;
311
312
let list = index_list;
0 commit comments