Skip to content

Commit 32c09e2

Browse files
committed
Add constructors
Reduce code reuse by adding constructors
1 parent d152504 commit 32c09e2

File tree

3 files changed

+33
-57
lines changed

3 files changed

+33
-57
lines changed

src/branch_and_bound.rs

+12-34
Original file line numberDiff line numberDiff line change
@@ -341,39 +341,24 @@ mod tests {
341341
weighted_utxos: Vec<&'a str>,
342342
}
343343

344-
fn build_pool(fee: Amount) -> Vec<Utxo> {
345-
let amts = [
346-
Amount::from_str("1 cBTC").unwrap() + fee,
347-
Amount::from_str("2 cBTC").unwrap() + fee,
348-
Amount::from_str("3 cBTC").unwrap() + fee,
349-
Amount::from_str("4 cBTC").unwrap() + fee,
350-
];
351-
352-
let mut pool = vec![];
353-
354-
for a in amts {
355-
let utxo = Utxo::new(a, Weight::ZERO);
356-
pool.push(utxo);
357-
}
358-
359-
pool
344+
fn build_pool() -> UtxoPool {
345+
let utxo_str_list = vec!["1 cBTC", "2 cBTC", "3 cBTC", "4 cBTC"];
346+
UtxoPool::from_str_list(&utxo_str_list)
360347
}
361348

362349
fn assert_coin_select(target_str: &str, expected_inputs_str: &[&str]) {
363-
let fee = Amount::ZERO;
364350
let target = Amount::from_str(target_str).unwrap();
365-
let utxos = build_pool(fee);
351+
let pool = build_pool();
366352
let inputs: Vec<Utxo> =
367-
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &utxos)
353+
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &pool.utxos)
368354
.unwrap()
369355
.cloned()
370356
.collect();
371-
let expected_inputs: Vec<Utxo> =
372-
expected_inputs_str.iter().map(|s| Utxo::from_str(s).unwrap()).collect();
373-
assert_eq!(expected_inputs, inputs);
357+
let expected_inputs: UtxoPool = UtxoPool::from_str_list(expected_inputs_str);
358+
assert_eq!(expected_inputs.utxos, inputs);
374359
}
375360

376-
fn assert_coin_select_params(p: &ParamsStr, expected_inputs: Option<&[&str]>) {
361+
fn assert_coin_select_params(p: &ParamsStr, expected_inputs_str: Option<&[&str]>) {
377362
let fee_rate = p.fee_rate.parse::<u64>().unwrap(); // would be nice if FeeRate had
378363
// from_str like Amount::from_str()
379364
let lt_fee_rate = p.lt_fee_rate.parse::<u64>().unwrap();
@@ -383,20 +368,13 @@ mod tests {
383368
let fee_rate = FeeRate::from_sat_per_kwu(fee_rate);
384369
let lt_fee_rate = FeeRate::from_sat_per_kwu(lt_fee_rate);
385370

386-
let w_utxos: Vec<_> = p
387-
.weighted_utxos
388-
.iter()
389-
.map(|s| Amount::from_str(s).unwrap())
390-
.map(|a| Utxo::new(a, Weight::ZERO))
391-
.collect();
392-
393-
let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &w_utxos);
371+
let pool: UtxoPool = UtxoPool::from_str_list(&p.weighted_utxos);
372+
let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &pool.utxos);
394373

395374
if let Some(i) = iter {
396375
let inputs: Vec<Utxo> = i.cloned().collect();
397-
let expected_inputs: Vec<Utxo> =
398-
expected_inputs.unwrap().iter().map(|s| Utxo::from_str(s).unwrap()).collect();
399-
assert_eq!(expected_inputs, inputs);
376+
let expected_inputs: UtxoPool = UtxoPool::from_str_list(expected_inputs_str.unwrap());
377+
assert_eq!(expected_inputs.utxos, inputs);
400378
}
401379
}
402380

src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ mod tests {
161161
pub utxos: Vec<Utxo>,
162162
}
163163

164+
impl UtxoPool {
165+
pub fn new(utxos: Vec<Utxo>) -> UtxoPool { UtxoPool { utxos } }
166+
167+
pub fn from_str_list(list: &[&str]) -> UtxoPool {
168+
let utxos: Vec<Utxo> = list.iter().map(|s| Utxo::from_str(s).unwrap()).collect();
169+
Self::new(utxos)
170+
}
171+
}
172+
164173
impl WeightedUtxo for Utxo {
165174
fn satisfaction_weight(&self) -> Weight { self.satisfaction_weight }
166175
fn value(&self) -> Amount { self.output.value }

src/single_random_draw.rs

+12-23
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,14 @@ mod tests {
8686

8787
use arbitrary::Arbitrary;
8888
use arbtest::arbtest;
89-
use bitcoin::{Amount, Weight};
89+
use bitcoin::Amount;
9090
use rand::rngs::mock::StepRng;
9191

9292
use super::*;
9393
use crate::single_random_draw::select_coins_srd;
9494
use crate::tests::{assert_proptest_srd, Utxo, UtxoPool};
9595

9696
const FEE_RATE: FeeRate = FeeRate::from_sat_per_kwu(10);
97-
const SATISFACTION_WEIGHT: Weight = Weight::from_wu(204);
9897

9998
#[derive(Debug)]
10099
pub struct ParamsStr<'a> {
@@ -103,17 +102,9 @@ mod tests {
103102
weighted_utxos: Vec<&'a str>,
104103
}
105104

106-
fn build_pool() -> Vec<Utxo> {
107-
let amts = vec![Amount::from_str("1 cBTC").unwrap(), Amount::from_str("2 cBTC").unwrap()];
108-
109-
let mut pool = vec![];
110-
111-
for a in amts {
112-
let utxo = Utxo::new(a, SATISFACTION_WEIGHT);
113-
pool.push(utxo);
114-
}
115-
116-
pool
105+
fn build_pool() -> UtxoPool {
106+
let utxo_str_list = vec!["1 cBTC/204 wu", "2 cBTC/204 wu"];
107+
UtxoPool::from_str_list(&utxo_str_list)
117108
}
118109

119110
fn get_rng() -> StepRng {
@@ -135,30 +126,28 @@ mod tests {
135126
// from_str like Amount::from_str()
136127
let target = Amount::from_str(p.target).unwrap();
137128
let fee_rate = FeeRate::from_sat_per_kwu(fee_rate);
138-
let w_utxos: Vec<Utxo> =
139-
p.weighted_utxos.iter().map(|s| Utxo::from_str(s).unwrap()).collect();
140-
let result = select_coins_srd(target, fee_rate, &w_utxos, &mut get_rng());
129+
130+
let pool: UtxoPool = UtxoPool::from_str_list(&p.weighted_utxos);
131+
let result = select_coins_srd(target, fee_rate, &pool.utxos, &mut get_rng());
141132

142133
if let Some(r) = result {
143134
let inputs: Vec<Utxo> = r.cloned().collect();
144-
let expected_inputs: Vec<Utxo> =
145-
expected_inputs_str.unwrap().iter().map(|s| Utxo::from_str(s).unwrap()).collect();
146-
assert_eq!(inputs, expected_inputs);
135+
let expected_pool: UtxoPool = UtxoPool::from_str_list(expected_inputs_str.unwrap());
136+
assert_eq!(inputs, expected_pool.utxos);
147137
}
148138
}
149139

150140
fn assert_coin_select(target_str: &str, expected_inputs_str: &[&str]) {
151141
let target = Amount::from_str(target_str).unwrap();
152142
let pool = build_pool();
153143

154-
let inputs: Vec<Utxo> = select_coins_srd(target, FEE_RATE, &pool, &mut get_rng())
144+
let inputs: Vec<Utxo> = select_coins_srd(target, FEE_RATE, &pool.utxos, &mut get_rng())
155145
.expect("unexpected error")
156146
.cloned()
157147
.collect();
158148

159-
let expected_inputs: Vec<_> =
160-
expected_inputs_str.iter().map(|s| Utxo::from_str(s).unwrap()).collect();
161-
assert_eq!(expected_inputs, inputs);
149+
let expected_pool: UtxoPool = UtxoPool::from_str_list(expected_inputs_str);
150+
assert_eq!(inputs, expected_pool.utxos);
162151
}
163152

164153
#[test]

0 commit comments

Comments
 (0)