Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Mar 20, 2024
1 parent f445905 commit ff98fa3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 79 deletions.
106 changes: 30 additions & 76 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,24 @@ mod tests {
use std::iter::zip;

fn create_coin_from_eff_values(eff_values: Vec<Amount>) -> Vec<Coin> {
let no_op =
TxOut { value: Amount::ZERO, script_pubkey: ScriptBuf::new() };
let no_op = TxOut { value: Amount::ZERO, script_pubkey: ScriptBuf::new() };
let fee_rate = FeeRate::ZERO;
let long_term_fee_rate = FeeRate::ZERO;

eff_values
.iter()
.map(|e| Coin { utxo: no_op.clone(), effective_value: *e, waste: SignedAmount::ZERO, fee_rate, long_term_fee_rate })
.map(|e| Coin {
utxo: no_op.clone(),
effective_value: *e,
waste: SignedAmount::ZERO,
fee_rate,
long_term_fee_rate,
})
.collect()
}

fn create_coin_with_waste(waste: SignedAmount) -> Vec<Coin> {
let no_op =
TxOut { value: Amount::ZERO, script_pubkey: ScriptBuf::new() };
let no_op = TxOut { value: Amount::ZERO, script_pubkey: ScriptBuf::new() };
let fee_rate = FeeRate::ZERO;
let long_term_fee_rate = FeeRate::ZERO;

Expand All @@ -334,31 +338,31 @@ mod tests {
effective_value: Amount::from_str("1 cBTC").unwrap(),
waste,
fee_rate,
long_term_fee_rate
long_term_fee_rate,
};

let coin_two = Coin {
utxo: no_op.clone(),
effective_value: Amount::from_str("2 cBTC").unwrap(),
waste,
fee_rate,
long_term_fee_rate
long_term_fee_rate,
};

let coin_three = Coin {
utxo: no_op.clone(),
effective_value: Amount::from_str("3 cBTC").unwrap(),
waste,
fee_rate,
long_term_fee_rate
long_term_fee_rate,
};

let coin_four = Coin {
utxo: no_op.clone(),
effective_value: Amount::from_str("4 cBTC").unwrap(),
waste,
fee_rate,
long_term_fee_rate
long_term_fee_rate,
};

vec![coin_one, coin_two, coin_three, coin_four]
Expand Down Expand Up @@ -390,10 +394,7 @@ mod tests {
let target = Amount::from_str("1 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 1);
assert_eq!(list[0].utxo.value, Amount::from_str("1 cBTC").unwrap());
Expand All @@ -404,10 +405,7 @@ mod tests {
let target = Amount::from_str("2 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 1);
assert_eq!(list[0].utxo.value, Amount::from_str("2 cBTC").unwrap());
Expand All @@ -418,10 +416,7 @@ mod tests {
let target = Amount::from_str("3 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 2);
assert_eq!(list[0].utxo.value, Amount::from_str("2 cBTC").unwrap());
Expand All @@ -433,10 +428,7 @@ mod tests {
let target = Amount::from_str("4 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 2);
assert_eq!(list[0].utxo.value, Amount::from_str("3 cBTC").unwrap());
Expand All @@ -448,10 +440,7 @@ mod tests {
let target = Amount::from_str("5 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 2);
assert_eq!(list[0].utxo.value, Amount::from_str("3 cBTC").unwrap());
Expand All @@ -463,10 +452,7 @@ mod tests {
let target = Amount::from_str("6 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 3);
assert_eq!(list[0].utxo.value, Amount::from_str("3 cBTC").unwrap());
Expand All @@ -479,10 +465,7 @@ mod tests {
let target = Amount::from_str("7 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 3);
assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
Expand All @@ -495,10 +478,7 @@ mod tests {
let target = Amount::from_str("8 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 3);
assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
Expand All @@ -511,10 +491,7 @@ mod tests {
let target = Amount::from_str("9 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 3);
assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
Expand All @@ -527,10 +504,7 @@ mod tests {
let target = Amount::from_str("10 cBTC").unwrap();
let coin = create_coin(FeeRate::ZERO, FeeRate::ZERO);

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

assert_eq!(list.len(), 4);
assert_eq!(list[0].utxo.value, Amount::from_str("4 cBTC").unwrap());
Expand All @@ -552,10 +526,7 @@ mod tests {
let coin = Coin::new(tx_out, FeeRate::ZERO, FeeRate::ZERO, Weight::ZERO).unwrap();
let coins = &[coin];

let list: Vec<_> =
select_coins_bnb(target, cost_of_change, coins)
.unwrap()
.collect();
let list: Vec<_> = select_coins_bnb(target, cost_of_change, coins).unwrap().collect();

assert_eq!(list.len(), 1);
assert_eq!(list[0].utxo.value, Amount::from_str("1.5 cBTC").unwrap());
Expand All @@ -582,8 +553,7 @@ mod tests {

// the possible combinations are 2,4 or 1,2,3
// fees are cheap, so use 1,2,3
let list: Vec<_> =
select_coins_bnb(target, Amount::ZERO, &coin).unwrap().collect();
let list: Vec<_> = select_coins_bnb(target, Amount::ZERO, &coin).unwrap().collect();

assert_eq!(list.len(), 3);
assert_eq!(list[0].effective_value, Amount::from_str("3 cBTC").unwrap());
Expand All @@ -603,8 +573,7 @@ mod tests {

// the possible combinations are 2,4 or 1,2,3
// fees are expensive, so use 2,4
let list: Vec<_> =
select_coins_bnb(target, Amount::ZERO, &coin).unwrap().collect();
let list: Vec<_> = select_coins_bnb(target, Amount::ZERO, &coin).unwrap().collect();

assert_eq!(list.len(), 2);
assert_eq!(list[0].effective_value, Amount::from_str("4 cBTC").unwrap());
Expand Down Expand Up @@ -654,10 +623,7 @@ mod tests {
];

let coins = create_coin_from_eff_values(eff_vals);
let list: Vec<_> =
select_coins_bnb(target, cost_of_change, &coins)
.unwrap()
.collect();
let list: Vec<_> = select_coins_bnb(target, cost_of_change, &coins).unwrap().collect();

assert_eq!(list.len(), 3);
assert_eq!(list[0].effective_value, Amount::from_str("3 cBTC").unwrap());
Expand All @@ -680,10 +646,7 @@ mod tests {
];

let coins = create_coin_from_eff_values(eff_vals);
let list: Vec<_> =
select_coins_bnb(target, cost_of_change, &coins)
.unwrap()
.collect();
let list: Vec<_> = select_coins_bnb(target, cost_of_change, &coins).unwrap().collect();

assert_eq!(list.len(), 3);
assert_eq!(list[0].effective_value, Amount::from_str("10 cBTC").unwrap());
Expand All @@ -704,11 +667,7 @@ mod tests {

let eff_vals: Vec<_> = vals.map(Amount::from_sat).collect();
let coins = create_coin_from_eff_values(eff_vals);
let list = select_coins_bnb(
Amount::from_sat(target),
Amount::ONE_SAT,
&coins,
);
let list = select_coins_bnb(Amount::from_sat(target), Amount::ONE_SAT, &coins);

assert!(list.is_none());
}
Expand All @@ -731,12 +690,7 @@ mod tests {
// Add a value that will match the target before iteration exhaustion occurs.
eff_vals.push(Amount::from_sat(target));
let coins = create_coin_from_eff_values(eff_vals);
let mut list = select_coins_bnb(
Amount::from_sat(target),
Amount::ONE_SAT,
&coins,
)
.unwrap();
let mut list = select_coins_bnb(Amount::from_sat(target), Amount::ONE_SAT, &coins).unwrap();

assert_eq!(list.len(), 1);
assert_eq!(list.next().unwrap().effective_value, Amount::from_sat(target));
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct Coin {
/// TODO
pub fee_rate: FeeRate,
/// TODO
pub long_term_fee_rate: FeeRate
pub long_term_fee_rate: FeeRate,
}

/// TODO
Expand Down Expand Up @@ -110,8 +110,7 @@ pub fn select_coins<T: Utxo>(
coins: &[Coin],
) -> Option<impl Iterator<Item = &Coin>> {
{
let bnb =
select_coins_bnb(target, cost_of_change, coins);
let bnb = select_coins_bnb(target, cost_of_change, coins);

if bnb.is_some() {
bnb
Expand Down

0 comments on commit ff98fa3

Please sign in to comment.