@@ -13,7 +13,7 @@ use rand::seq::SliceRandom;
1313
1414use crate :: OverflowError :: Addition ;
1515use crate :: SelectionError :: { InsufficentFunds , MaxWeightExceeded , Overflow , ProgramError } ;
16- use crate :: { Return , WeightedUtxo , CHANGE_LOWER } ;
16+ use crate :: { Return , WeightedUtxo } ;
1717
1818/// Select coins by Single Random Draw (SRD).
1919///
@@ -59,8 +59,7 @@ pub fn single_random_draw<
5959 . try_fold ( Amount :: ZERO , Amount :: checked_add)
6060 . ok_or ( Overflow ( Addition ) ) ?;
6161
62- let threshold = target. checked_add ( CHANGE_LOWER ) . ok_or ( Overflow ( Addition ) ) ?;
63- if available_value < threshold {
62+ if available_value < target {
6463 return Err ( InsufficentFunds ) ;
6564 }
6665
@@ -69,7 +68,6 @@ pub fn single_random_draw<
6968 let mut heap: BinaryHeap < & WeightedUtxo > = BinaryHeap :: new ( ) ;
7069
7170 let mut value = Amount :: ZERO ;
72-
7371 let mut iteration = 0 ;
7472 let mut max_tx_weight_exceeded = false ;
7573 let mut weight_total = Weight :: ZERO ;
@@ -93,7 +91,7 @@ pub fn single_random_draw<
9391 } ;
9492 }
9593
96- if value >= threshold {
94+ if value >= target {
9795 let result: Vec < _ > = heap. into_sorted_vec ( ) ;
9896 return Ok ( ( iteration, result) ) ;
9997 }
@@ -233,55 +231,21 @@ mod tests {
233231 . assert ( ) ;
234232 }
235233
236- #[ test]
237- fn select_coins_srd_change_output_too_small ( ) {
238- // The resulting change must be greater than CHANGE_LOWER
239- // therefore, an exact match will fail.
240- TestSRD {
241- target : "3 cBTC" ,
242- fee_rate : "10 sat/kwu" ,
243- max_weight : "40000 wu" ,
244- weighted_utxos : & [ "e(1 cBTC)/68 vB" , "e(2 cBTC)/68 vB" ] ,
245- expected_utxos : & [ ] ,
246- expected_error : Some ( InsufficentFunds ) ,
247- expected_iterations : 0 ,
248- }
249- . assert ( ) ;
250- }
251-
252234 #[ test]
253235 fn select_coins_srd_with_high_fee ( ) {
254- // Although the first selected UTXO valued at 2050000 meets the
255- // target and meets the threshold of target + CHANGE, the value
256- // is not enough since when the effective value is calculated,
257- // it falls bellow the threshold. Therefore multiple UTXOs are
258- // selected.
236+ // Both UTXOs are selected since neither has enough effective_value individually
259237 TestSRD {
260238 target : "2 cBTC" ,
261239 fee_rate : "10 sat/kwu" ,
262240 max_weight : "40000 wu" ,
263- weighted_utxos : & [ "1 cBTC/68 vB" , "2050000 sats /68 vB" ] ,
264- expected_utxos : & [ "2050000 sats /68 vB" , "1 cBTC/68 vB" ] ,
241+ weighted_utxos : & [ "1 cBTC/68 vB" , "2 cBTC /68 vB" ] ,
242+ expected_utxos : & [ "2 cBTC /68 vB" , "1 cBTC/68 vB" ] ,
265243 expected_error : None ,
266244 expected_iterations : 2 ,
267245 }
268246 . assert ( ) ;
269247 }
270248
271- #[ test]
272- fn select_coins_srd_threshold_overflow ( ) {
273- TestSRD {
274- target : "2100000000000000 sat" , // Amount::MAX
275- fee_rate : "10 sat/kwu" ,
276- max_weight : "40000 wu" ,
277- weighted_utxos : & [ "1 cBTC/68 vB" ] ,
278- expected_utxos : & [ ] ,
279- expected_error : Some ( Overflow ( Addition ) ) ,
280- expected_iterations : 0 ,
281- }
282- . assert ( ) ;
283- }
284-
285249 #[ test]
286250 fn select_coins_srd_utxo_pool_sum_overflow ( ) {
287251 TestSRD {
@@ -328,7 +292,7 @@ mod tests {
328292 #[ test]
329293 fn select_coins_srd_max_weight_eff_value ( ) {
330294 TestSRD {
331- target : "10000 sats" , //threshold = 60k sats
295+ target : "60000 sats" ,
332296 fee_rate : "10 sat/kwu" ,
333297 max_weight : "1000 wu" ,
334298 // after rand: [30k sats/500 wu, 29,999 sats/700 wu, 30k sats/500 wu]
@@ -347,7 +311,7 @@ mod tests {
347311 #[ test]
348312 fn select_coins_srd_max_weight_eff_value_tie ( ) {
349313 TestSRD {
350- target : "10000 sats" , // threshold = 60k sats
314+ target : "60000 sats" ,
351315 fee_rate : "10 sat/kwu" ,
352316 max_weight : "1000 wu" ,
353317 // after rand: [30k sats/500 wu, 30k sats/700 wu, 30k sats/500 wu]
@@ -381,7 +345,7 @@ mod tests {
381345 }
382346 Err ( InsufficentFunds ) => {
383347 let available_value = candidate. available_value ( ) . unwrap ( ) ;
384- assert ! ( available_value < ( target + CHANGE_LOWER ) . unwrap ( ) ) ;
348+ assert ! ( available_value < target) ;
385349 }
386350 Err ( crate :: SelectionError :: IterationLimitReached ) => panic ! ( "un-expected result" ) ,
387351 Err ( MaxWeightExceeded ) => {
@@ -392,9 +356,7 @@ mod tests {
392356 let available_value = candidate. available_value ( ) ;
393357 let weight_total = candidate. weight_total ( ) ;
394358 assert ! (
395- available_value. is_none( )
396- || weight_total. is_none( )
397- || target. checked_add( CHANGE_LOWER ) . is_none( )
359+ available_value. is_none( ) || weight_total. is_none( )
398360 ) ;
399361 }
400362 Err ( ProgramError ) => panic ! ( "un-expected program error" ) ,
0 commit comments