From f4e058fb70eb724da79d4b640db27f0c738ec72b Mon Sep 17 00:00:00 2001 From: yancy Date: Wed, 6 Dec 2023 12:28:24 +0100 Subject: [PATCH] wip --- src/single_random_draw.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/single_random_draw.rs b/src/single_random_draw.rs index 0086ca3..ba6c723 100644 --- a/src/single_random_draw.rs +++ b/src/single_random_draw.rs @@ -3,10 +3,9 @@ use crate::WeightedUtxo; use crate::CHANGE_LOWER; +use bitcoin::blockdata::effective_value; use bitcoin::Amount; use bitcoin::FeeRate; -use bitcoin::SignedAmount; -use bitcoin::blockdata::effective_value; use rand::seq::SliceRandom; /// Randomly select coins for the given target by shuffling the UTXO pool and @@ -44,11 +43,11 @@ pub fn select_coins_srd( for w_utxo in weighted_utxos { let utxo_value = w_utxo.utxo.value; - let effective_value: SignedAmount = effective_value(fee_rate, w_utxo.satisfaction_weight, utxo_value)?; + let effective_value = effective_value(fee_rate, w_utxo.satisfaction_weight, utxo_value)?; value += match effective_value.to_unsigned() { Ok(amt) => amt, - Err(_) => Amount::ZERO, + Err(_) => continue, }; result.push(w_utxo.clone()); @@ -146,9 +145,10 @@ mod tests { #[test] fn select_coins_skip_negative_effective_value() { - let target: Amount = Amount::from_str("1 cBTC").unwrap() - CHANGE_LOWER; + let target: Amount = Amount::from_str("2 cBTC").unwrap() - CHANGE_LOWER; - let mut weighted_utxos: Vec = vec![WeightedUtxo { + let mut weighted_utxos = create_weighted_utxos(); + let mut negative_eff_value = vec![WeightedUtxo { satisfaction_weight: Weight::ZERO, utxo: TxOut { value: Amount::from_str("1 sat").unwrap(), @@ -156,10 +156,16 @@ mod tests { }, }]; - let result = select_coins_srd(target, FEE_RATE, &mut weighted_utxos, &mut get_rng()) + weighted_utxos.append(&mut negative_eff_value); + assert_eq!(weighted_utxos.len(), 3); + + let mut rng = get_rng(); + let result = select_coins_srd(target, FEE_RATE, &mut weighted_utxos, &mut rng) .expect("unexpected error"); - assert!(result.is_empty()); + let mut expected_utxos = create_weighted_utxos(); + expected_utxos.shuffle(&mut rng); + assert_eq!(result, expected_utxos); } #[test]