From 75f99d2b8fb016dbfd1564799d68e766d0e27399 Mon Sep 17 00:00:00 2001 From: yancy Date: Wed, 20 Nov 2024 11:47:41 -0600 Subject: [PATCH] Add weight to weighted_utxo --- src/branch_and_bound.rs | 10 ++++----- src/coin_grinder.rs | 44 +++++++++++++++++---------------------- src/lib.rs | 12 ++++++++--- src/single_random_draw.rs | 4 ++-- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/branch_and_bound.rs b/src/branch_and_bound.rs index b445f2be..e7e9f4f9 100644 --- a/src/branch_and_bound.rs +++ b/src/branch_and_bound.rs @@ -346,7 +346,7 @@ mod tests { let mut pool = vec![]; for a in amts { - let utxo = build_utxo(a, Weight::ZERO); + let utxo = build_utxo(a, Weight::ZERO, Weight::ZERO); pool.push(utxo); } @@ -400,7 +400,7 @@ mod tests { .weighted_utxos .iter() .map(|s| Amount::from_str(s).unwrap()) - .map(|a| build_utxo(a, Weight::ZERO)) + .map(|a| build_utxo(a, Weight::ZERO, Weight::ZERO)) .collect(); let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &w_utxos); @@ -639,7 +639,7 @@ mod tests { .map(|a| Amount::from_sat(a as u64)) .collect(); - let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO)).collect(); + let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO, Weight::ZERO)).collect(); let list = select_coins_bnb(target, Amount::ONE_SAT, FeeRate::ZERO, FeeRate::ZERO, &pool); @@ -658,7 +658,7 @@ mod tests { }); let amts: Vec<_> = vals.map(Amount::from_sat).collect(); - let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO)).collect(); + let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO, Weight::ZERO)).collect(); let list = select_coins_bnb( Amount::from_sat(target), @@ -687,7 +687,7 @@ mod tests { // Add a value that will match the target before iteration exhaustion occurs. amts.push(Amount::from_sat(target)); - let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO)).collect(); + let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO, Weight::ZERO)).collect(); let mut list = select_coins_bnb( Amount::from_sat(target), diff --git a/src/coin_grinder.rs b/src/coin_grinder.rs index 7b027b57..5cf911e9 100644 --- a/src/coin_grinder.rs +++ b/src/coin_grinder.rs @@ -124,8 +124,8 @@ mod tests { let mut pool = Vec::new(); for i in 0..10 { - let one_cbtc = build_utxo(Amount::from_str("1 cBTC").unwrap(), Weight::ZERO); - let two_cbtc = build_utxo(Amount::from_str("2 cBTC").unwrap(), Weight::ZERO); + let one_cbtc = build_utxo(Amount::from_str("1 cBTC").unwrap(), Weight::ZERO, Weight::ZERO); + let two_cbtc = build_utxo(Amount::from_str("2 cBTC").unwrap(), Weight::ZERO, Weight::ZERO); pool.push(one_cbtc); pool.push(two_cbtc); } @@ -138,7 +138,7 @@ mod tests { //from_str like Amount::from_str() let target = Amount::from_str(p.target).unwrap(); let change_target = Amount::from_str(p.change_target).unwrap(); - let fee_rate = FeeRate::from_sat_per_kwu(fee_rate); + let fee_rate = FeeRate::from_sat_per_vb(fee_rate).unwrap(); let max_weight = Weight::from_str(p.max_weight).unwrap(); let w_utxos: Vec<_> = p @@ -159,7 +159,7 @@ mod tests { _ => panic!(), } }) - .map(|(a, w)| build_utxo(a, w)) + .map(|(a, w)| build_utxo(a, w, w - Weight::from_wu(40))) .collect(); let c = coin_grinder(target, change_target, max_weight, fee_rate, &w_utxos); @@ -192,33 +192,27 @@ mod tests { // 110 * segwit multiplyer + input_base_weight = // 110 * 4 + 160 = // 150 - let target = Amount::from_str("30 BTC").unwrap(); - let max_weight = Weight::from_wu(400_000); - let change_target = Amount::from_str("1 BTC").unwrap(); - - let fee_rate = FeeRate::from_sat_per_vb(5).unwrap(); - let params = ParamsStr { target: "30 BTC", change_target: "1 BTC", max_weight: "400000", fee_rate: "5", //from sat per vb weighted_utxos: vec![ - "3 BTC/310", - "6 BTC/310", - "9 BTC/310", - "12 BTC/310", - "15 BTC/310", - "2 BTC/210", - "5 BTC/210", - "8 BTC/210", - "11 BTC/210", - "14 BTC/210", - "1 BTC/110", - "4 BTC/110", - "7 BTC/110", - "10 BTC/110", - "13 BTC/110", + "3 BTC/350", + "6 BTC/350", + "9 BTC/350", + "12 BTC/350", + "15 BTC/350", + "2 BTC/250", + "5 BTC/250", + "8 BTC/250", + "11 BTC/250", + "14 BTC/250", + "1 BTC/150", + "4 BTC/150", + "7 BTC/150", + "10 BTC/150", + "13 BTC/150", ] }; } diff --git a/src/lib.rs b/src/lib.rs index 6ae59146..112d2a8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,9 @@ pub trait WeightedUtxo { /// fn satisfaction_weight(&self) -> Weight; + /// The weight + fn weight(&self) -> Weight; + /// The UTXO value. fn value(&self) -> Amount; @@ -119,7 +122,8 @@ mod tests { .map(|a| { let amt = Amount::from_sat(*a); let weight = Weight::ZERO; - build_utxo(amt, weight) + let satisfaction_weight = Weight::ZERO; + build_utxo(amt, weight, satisfaction_weight) }) .collect(); @@ -129,16 +133,18 @@ mod tests { #[derive(Debug)] pub struct Utxo { pub output: TxOut, + pub weight: Weight, pub satisfaction_weight: Weight, } - pub fn build_utxo(amt: Amount, satisfaction_weight: Weight) -> Utxo { + pub fn build_utxo(amt: Amount, weight: Weight, satisfaction_weight: Weight) -> Utxo { let output = TxOut { value: amt, script_pubkey: ScriptBuf::new() }; - Utxo { output, satisfaction_weight } + Utxo { output, weight, satisfaction_weight } } impl WeightedUtxo for Utxo { fn satisfaction_weight(&self) -> Weight { self.satisfaction_weight } + fn weight(&self) -> Weight { self.weight } fn value(&self) -> Amount { self.output.value } } diff --git a/src/single_random_draw.rs b/src/single_random_draw.rs index 95b07031..c6a3dac6 100644 --- a/src/single_random_draw.rs +++ b/src/single_random_draw.rs @@ -108,7 +108,7 @@ mod tests { let mut pool = vec![]; for a in amts { - let utxo = build_utxo(a, SATISFACTION_WEIGHT); + let utxo = build_utxo(a, Weight::ZERO, SATISFACTION_WEIGHT); pool.push(utxo); } @@ -161,7 +161,7 @@ mod tests { _ => panic!(), } }) - .map(|(a, w)| build_utxo(a, w)) + .map(|(a, w)| build_utxo(a, Weight::ZERO, w)) .collect(); let result = select_coins_srd(target, fee_rate, &w_utxos, &mut get_rng());