Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Dec 6, 2023
1 parent 096176e commit f4e058f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/single_random_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,11 +43,11 @@ pub fn select_coins_srd<R: rand::Rng + ?Sized>(

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());
Expand Down Expand Up @@ -146,20 +145,27 @@ 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<WeightedUtxo> = 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(),
script_pubkey: ScriptBuf::new(),
},
}];

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]
Expand Down

0 comments on commit f4e058f

Please sign in to comment.