Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utxo to string from string #79

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 24 additions & 59 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
//!
//! This module introduces the Branch and Bound Coin-Selection Algorithm.

use std::vec::IntoIter;

use bitcoin::amount::CheckedSum;
use bitcoin::{Amount, FeeRate, SignedAmount};
use std::vec::IntoIter;

use crate::WeightedUtxo;

Expand Down Expand Up @@ -326,7 +327,7 @@ mod tests {
use bitcoin::{Amount, Weight};

use super::*;
use crate::tests::{assert_proptest_bnb, build_utxo, Utxo, UtxoPool};
use crate::tests::{assert_proptest_bnb, Utxo, UtxoPool};
use crate::WeightedUtxo;

const TX_IN_BASE_WEIGHT: u64 = 160;
Expand All @@ -340,47 +341,24 @@ mod tests {
weighted_utxos: Vec<&'a str>,
}

fn build_pool(fee: Amount) -> Vec<Utxo> {
let amts = [
Amount::from_str("1 cBTC").unwrap() + fee,
Amount::from_str("2 cBTC").unwrap() + fee,
Amount::from_str("3 cBTC").unwrap() + fee,
Amount::from_str("4 cBTC").unwrap() + fee,
];

let mut pool = vec![];

for a in amts {
let utxo = build_utxo(a, Weight::ZERO);
pool.push(utxo);
}

pool
}

fn format_utxo_list(i: &[&Utxo]) -> Vec<String> {
i.iter().map(|u| u.value().to_string()).collect()
fn build_pool() -> UtxoPool {
let utxo_str_list = vec!["1 cBTC", "2 cBTC", "3 cBTC", "4 cBTC"];
UtxoPool::from_str_list(&utxo_str_list)
}

fn format_expected_str_list(e: &[&str]) -> Vec<String> {
e.iter().map(|s| Amount::from_str(s).unwrap().to_string()).collect()
}

fn assert_coin_select(target_str: &str, expected_inputs: &[&str]) {
let fee = Amount::ZERO;
fn assert_coin_select(target_str: &str, expected_inputs_str: &[&str]) {
let target = Amount::from_str(target_str).unwrap();
let utxos = build_pool(fee);
let inputs: Vec<_> =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &utxos)
let pool = build_pool();
let inputs: Vec<Utxo> =
select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &pool.utxos)
.unwrap()
.cloned()
.collect();

let input_str_list: Vec<_> = format_utxo_list(&inputs);
let expected_str_list: Vec<_> = format_expected_str_list(expected_inputs);
assert_eq!(input_str_list, expected_str_list);
let expected_inputs: UtxoPool = UtxoPool::from_str_list(expected_inputs_str);
assert_eq!(expected_inputs.utxos, inputs);
}

fn assert_coin_select_params(p: &ParamsStr, expected_inputs: Option<&[&str]>) {
fn assert_coin_select_params(p: &ParamsStr, expected_inputs_str: Option<&[&str]>) {
let fee_rate = p.fee_rate.parse::<u64>().unwrap(); // would be nice if FeeRate had
// from_str like Amount::from_str()
let lt_fee_rate = p.lt_fee_rate.parse::<u64>().unwrap();
Expand All @@ -390,26 +368,13 @@ mod tests {
let fee_rate = FeeRate::from_sat_per_kwu(fee_rate);
let lt_fee_rate = FeeRate::from_sat_per_kwu(lt_fee_rate);

let w_utxos: Vec<_> = p
.weighted_utxos
.iter()
.map(|s| Amount::from_str(s).unwrap())
.map(|a| build_utxo(a, Weight::ZERO))
.collect();

let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &w_utxos);
let pool: UtxoPool = UtxoPool::from_str_list(&p.weighted_utxos);
let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &pool.utxos);

if expected_inputs.is_none() {
assert!(iter.is_none());
} else {
let inputs: Vec<_> = iter.unwrap().collect();
let expected_str_list: Vec<String> = expected_inputs
.unwrap()
.iter()
.map(|s| Amount::from_str(s).unwrap().to_string())
.collect();
let input_str_list: Vec<String> = format_utxo_list(&inputs);
assert_eq!(input_str_list, expected_str_list);
if let Some(i) = iter {
let inputs: Vec<Utxo> = i.cloned().collect();
let expected_inputs: UtxoPool = UtxoPool::from_str_list(expected_inputs_str.unwrap());
assert_eq!(expected_inputs.utxos, inputs);
}
}

Expand Down Expand Up @@ -673,7 +638,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| Utxo::new(a, Weight::ZERO)).collect();

let list = select_coins_bnb(target, Amount::ONE_SAT, FeeRate::ZERO, FeeRate::ZERO, &pool);

Expand All @@ -692,7 +657,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| Utxo::new(a, Weight::ZERO)).collect();

let list = select_coins_bnb(
Amount::from_sat(target),
Expand Down Expand Up @@ -721,7 +686,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| Utxo::new(a, Weight::ZERO)).collect();

let mut list = select_coins_bnb(
Amount::from_sat(target),
Expand All @@ -743,7 +708,7 @@ mod tests {

arbtest(|u| {
let amount = arb_amount_in_range(u, minimal_non_dust..=effective_value_max);
let utxo = build_utxo(amount, Weight::ZERO);
let utxo = Utxo::new(amount, Weight::ZERO);
let pool: Vec<Utxo> = vec![utxo.clone()];

let coins: Vec<Utxo> =
Expand Down
65 changes: 59 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ pub fn select_coins<Utxo: WeightedUtxo>(

#[cfg(test)]
mod tests {
use arbitrary::{Arbitrary, Unstructured, Result};
use std::str::FromStr;

use arbitrary::{Arbitrary, Result, Unstructured};
use arbtest::arbtest;
use bitcoin::amount::CheckedSum;
use bitcoin::transaction::effective_value;
Expand All @@ -124,7 +126,7 @@ mod tests {
.map(|a| {
let amt = Amount::from_sat(*a);
let weight = Weight::ZERO;
build_utxo(amt, weight)
Utxo::new(amt, weight)
})
.collect();

Expand All @@ -137,10 +139,8 @@ mod tests {
pub satisfaction_weight: Weight,
}

pub fn build_utxo(amt: Amount, satisfaction_weight: Weight) -> Utxo {
let output = TxOut { value: amt, script_pubkey: ScriptBuf::new() };
Utxo { output, satisfaction_weight }
}
#[derive(Debug, PartialEq, Eq)]
pub struct ParseUtxoError;

impl<'a> Arbitrary<'a> for UtxoPool {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Expand All @@ -161,11 +161,64 @@ mod tests {
pub utxos: Vec<Utxo>,
}

impl UtxoPool {
pub fn new(utxos: Vec<Utxo>) -> UtxoPool { UtxoPool { utxos } }

pub fn from_str_list(list: &[&str]) -> UtxoPool {
let utxos: Vec<Utxo> = list.iter().map(|s| Utxo::from_str(s).unwrap()).collect();
Self::new(utxos)
}
}

impl WeightedUtxo for Utxo {
fn satisfaction_weight(&self) -> Weight { self.satisfaction_weight }
fn value(&self) -> Amount { self.output.value }
}

impl Utxo {
pub fn new(value: Amount, satisfaction_weight: Weight) -> Utxo {
let output = TxOut { value, script_pubkey: ScriptBuf::new() };
Utxo { output, satisfaction_weight }
}
}

impl FromStr for Utxo {
type Err = ParseUtxoError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let v: Vec<_> = s.split("/").collect();

let amt;
let weight;
match v.len() {
2 => {
amt = Amount::from_str(v[0]).unwrap();
let size: String = v[1].parse().unwrap();
let size_parts: Vec<_> = size.split(" ").collect();
assert_eq!(size_parts[1], "wu");
weight = Weight::from_str(size_parts[0]).unwrap();
}
1 => {
amt = Amount::from_str(v[0]).unwrap();
weight = Weight::ZERO;
}
_ => panic!(), //TODO return error
}

Ok(Utxo::new(amt, weight))
}
}

#[test]
fn utxo_to_from_string() {
let utxo = Utxo::from_str("1001 sat/124 wu").unwrap();

let amount = Amount::from_str("1001 sat").unwrap();
let weight = Weight::from_wu(124);
let expected_utxo = Utxo::new(amount, weight);
assert_eq!(utxo, expected_utxo);
}

#[test]
fn select_coins_no_solution() {
let target = Amount::from_sat(255432);
Expand Down
Loading
Loading