Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Mar 13, 2024
1 parent dd71105 commit d5d2c38
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 115 deletions.
212 changes: 98 additions & 114 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ pub fn select_coins_bnb(
// * Backtrack
if backtrack {
if index_selection.is_empty() {
// TODO revise
let mut r = vec![];
if let Some(b) = best_selection {
for i in b {
r.push(coin_select[i].index)
let (coin_select, _) = candidate_set[i];
r.push(coin_select.index)
}
return Some(r);
} else {
Expand Down Expand Up @@ -286,10 +288,12 @@ pub fn select_coins_bnb(
iteration += 1;
}

//TODO revise
let mut r = vec![];
if let Some(b) = best_selection {
for i in b {
r.push(coin_select[i].index)
let (coin_select, _) = candidate_set[i];
r.push(coin_select.index)
}
return Some(r);
} else {
Expand All @@ -301,6 +305,7 @@ pub fn select_coins_bnb(
mod tests {
use super::*;
use crate::WeightedUtxo;
use crate::CoinSelect;
use bitcoin::Amount;
use bitcoin::ScriptBuf;
use bitcoin::SignedAmount;
Expand Down Expand Up @@ -336,167 +341,146 @@ mod tests {
.collect()
}

fn create_coin_select() -> Vec<CoinSelect> {
let c_one = CoinSelect {
index: 0,
effective_value: Amount::from_str("1 cBTC").unwrap(),
weight_prediction: Weight::ZERO
};

let c_two = CoinSelect {
index: 1,
effective_value: Amount::from_str("2 cBTC").unwrap(),
weight_prediction: Weight::ZERO
};

let c_three = CoinSelect {
index: 2,
effective_value: Amount::from_str("3 cBTC").unwrap(),
weight_prediction: Weight::ZERO
};

let c_four = CoinSelect {
index: 3,
effective_value: Amount::from_str("4 cBTC").unwrap(),
weight_prediction: Weight::ZERO
};

vec![c_one, c_two, c_three, c_four]
}

#[test]
fn select_coins_bnb_one() {
let target = Amount::from_str("1 cBTC").unwrap();
let mut eff_values = vec![
Amount::from_str("4 cBTC").unwrap(),
Amount::from_str("3 cBTC").unwrap(),
Amount::from_str("2 cBTC").unwrap(),
Amount::from_str("1 cBTC").unwrap(),
];

let weights = vec![
Weight::ZERO,
Weight::ZERO,
Weight::ZERO,
Weight::ZERO
];

let list = select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &mut eff_values, &weights).unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

assert_eq!(list.len(), 1);
assert_eq!(list[0], 3);
assert_eq!(list[0], 0);
}

#[test]
fn select_coins_bnb_two() {
//let target = Amount::from_str("2 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();
let target = Amount::from_str("2 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//assert_eq!(list.len(), 1);
//assert_eq!(list[0].utxo.value, Amount::from_str("2 cBTC").unwrap());
assert_eq!(list.len(), 1);
assert_eq!(list[0], 1);
}

#[test]
fn select_coins_bnb_three() {
//let target = Amount::from_str("3 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);
let target = Amount::from_str("3 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();

//assert_eq!(list.len(), 2);
//assert_eq!(list[0].utxo.value, Amount::from_str("2 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("1 cBTC").unwrap());
assert_eq!(list.len(), 2);
assert_eq!(list[0], 1);
assert_eq!(list[1], 0);
}

#[test]
fn select_coins_bnb_four() {
//let target = Amount::from_str("4 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();
let target = Amount::from_str("4 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//assert_eq!(list.len(), 2);
//assert_eq!(list[0].utxo.value, Amount::from_str("3 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("1 cBTC").unwrap());
assert_eq!(list.len(), 2);
assert_eq!(list[0], 2);
assert_eq!(list[1], 0);
}

#[test]
fn select_coins_bnb_five() {
//let target = Amount::from_str("5 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);
let target = Amount::from_str("5 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();

//assert_eq!(list.len(), 2);
//assert_eq!(list[0].utxo.value, Amount::from_str("3 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("2 cBTC").unwrap());
assert_eq!(list.len(), 2);
assert_eq!(list[0], 2);
assert_eq!(list[1], 1);
}

#[test]
fn select_coins_bnb_six() {
//let target = Amount::from_str("6 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();

//assert_eq!(list.len(), 3);
//assert_eq!(list[0].utxo.value, Amount::from_str("3 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("2 cBTC").unwrap());
//assert_eq!(list[2].utxo.value, Amount::from_str("1 cBTC").unwrap());
let target = Amount::from_str("6 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

assert_eq!(list.len(), 3);
assert_eq!(list[0], 2);
assert_eq!(list[1], 1);
assert_eq!(list[2], 0);
}

#[test]
fn select_coins_bnb_seven() {
//let target = Amount::from_str("7 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();
let target = Amount::from_str("7 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//assert_eq!(list.len(), 3);
//assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("2 cBTC").unwrap());
//assert_eq!(list[2].utxo.value, Amount::from_str("1 cBTC").unwrap());
assert_eq!(list.len(), 3);
assert_eq!(list[0], 3);
assert_eq!(list[1], 1);
assert_eq!(list[2], 0);
}

#[test]
fn select_coins_bnb_eight() {
//let target = Amount::from_str("8 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);
let target = Amount::from_str("8 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();

//assert_eq!(list.len(), 3);
//assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("3 cBTC").unwrap());
//assert_eq!(list[2].utxo.value, Amount::from_str("1 cBTC").unwrap());
assert_eq!(list.len(), 3);
assert_eq!(list[0], 3);
assert_eq!(list[1], 2);
assert_eq!(list[2], 0);
}

#[test]
fn select_coins_bnb_nine() {
//let target = Amount::from_str("9 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);
let target = Amount::from_str("9 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();

//assert_eq!(list.len(), 3);
//assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("3 cBTC").unwrap());
//assert_eq!(list[2].utxo.value, Amount::from_str("2 cBTC").unwrap());
assert_eq!(list.len(), 3);
assert_eq!(list[0], 3);
assert_eq!(list[1], 2);
assert_eq!(list[2], 1);
}

#[test]
fn select_coins_bnb_ten() {
//let target = Amount::from_str("10 cBTC").unwrap();
//let weighted_utxos = create_weighted_utxos(Amount::ZERO);
let target = Amount::from_str("10 cBTC").unwrap();
let cs = create_coin_select();
let list = select_coins_bnb(target, &cs, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO).unwrap();

//let list: Vec<_> =
//select_coins_bnb(target, Amount::ZERO, FeeRate::ZERO, FeeRate::ZERO, &weighted_utxos)
//.unwrap()
//.collect();

//assert_eq!(list.len(), 4);
//assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
//assert_eq!(list[1].utxo.value, Amount::from_str("3 cBTC").unwrap());
//assert_eq!(list[2].utxo.value, Amount::from_str("2 cBTC").unwrap());
//assert_eq!(list[3].utxo.value, Amount::from_str("1 cBTC").unwrap());
assert_eq!(list.len(), 4);
assert_eq!(list[0], 3);
assert_eq!(list[1], 2);
assert_eq!(list[2], 1);
assert_eq!(list[3], 0);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct WeightedUtxo {
}

/// TODO
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct CoinSelect {
/// Corresponding UTXO index
pub index: usize,
Expand Down
1 change: 1 addition & 0 deletions src/single_random_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ mod tests {
use bitcoin::ScriptBuf;
use bitcoin::TxOut;
use bitcoin::Weight;
use bitcoin::FeeRate;
use core::str::FromStr;
use rand::rngs::mock::StepRng;

Expand Down

0 comments on commit d5d2c38

Please sign in to comment.