@@ -39,7 +39,6 @@ pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo>(
39
39
weighted_utxos : & ' a [ Utxo ] ,
40
40
rng : & mut R ,
41
41
) -> Return < ' a , Utxo > {
42
- println ! ( "start" ) ;
43
42
let mut result: Vec < _ > = weighted_utxos. iter ( ) . collect ( ) ;
44
43
let mut origin = result. to_owned ( ) ;
45
44
origin. shuffle ( rng) ;
@@ -50,32 +49,29 @@ pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo>(
50
49
let mut value = Amount :: ZERO ;
51
50
52
51
let mut iteration = 0 ;
53
- println ! ( "start loop" ) ;
54
52
for w_utxo in origin {
55
53
iteration += 1 ;
56
54
let utxo_value = w_utxo. value ( ) ;
57
55
let utxo_weight = w_utxo. satisfaction_weight ( ) ;
58
56
let effective_value = effective_value ( fee_rate, utxo_weight, utxo_value) ;
59
57
58
+ // if effective_value has no overflow
60
59
if let Some ( e) = effective_value {
60
+ // if effective_value is positive
61
61
if let Ok ( v) = e. to_unsigned ( ) {
62
- println ! ( "hi" ) ;
63
- println ! ( "{} + {} or {} {} " , value. to_sat( ) , v. to_sat( ) , value, v) ;
64
- println ! ( "{:?} {}" , Amount :: MAX , Amount :: MAX ) ;
65
- value = value. checked_add ( v) . unwrap ( ) ;
66
-
67
- result. push ( w_utxo) ;
68
-
69
- println ! ( "value {} threshold {} is >=: {}" , value, threshold, value >= threshold) ;
70
- if value >= threshold {
71
- println ! ( "return the result" ) ;
72
- return Some ( ( iteration, result) ) ;
62
+ // if addition does not overflow
63
+ if let Some ( new_val) = value. checked_add ( v) {
64
+ value = new_val;
65
+ result. push ( w_utxo) ;
66
+
67
+ if value >= threshold {
68
+ return Some ( ( iteration, result) ) ;
69
+ }
73
70
}
74
71
}
75
72
}
76
73
}
77
74
78
- println ! ( "none found" ) ;
79
75
None
80
76
}
81
77
@@ -109,6 +105,7 @@ mod tests {
109
105
let pool: UtxoPool = UtxoPool :: new ( self . weighted_utxos , fee_rate) ;
110
106
111
107
let result = select_coins_srd ( target, fee_rate, & pool. utxos , & mut get_rng ( ) ) ;
108
+ println ! ( "result {:?}" , result) ;
112
109
113
110
if let Some ( ( iterations, inputs) ) = result {
114
111
assert_eq ! ( iterations, self . expected_iterations) ;
@@ -272,6 +269,22 @@ mod tests {
272
269
. assert ( ) ;
273
270
}
274
271
272
+ #[ test]
273
+ fn select_coins_srd_pool_overflow ( ) {
274
+ TestSRD {
275
+ target : "18315535.91666658 BTC" ,
276
+ fee_rate : "0" ,
277
+ weighted_utxos : & [
278
+ "e(1550078614956004 sats)/68 vB" ,
279
+ "e(1831540706090689 sats)/18446744073692774400 wu" ,
280
+ "e(12885625970 sats)/68 vB" ,
281
+ ] ,
282
+ expected_utxos : Some ( & [ "e(1831540706090689 sats)/18446744073692774400 wu" , "e(12885625970 sats)/68 vB" ] ) ,
283
+ expected_iterations : 2 ,
284
+ }
285
+ . assert ( ) ;
286
+ }
287
+
275
288
#[ test]
276
289
fn select_srd_match_proptest ( ) {
277
290
arbtest ( |u| {
0 commit comments