Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Oct 15, 2024
1 parent 4cce555 commit a144874
Showing 1 changed file with 63 additions and 67 deletions.
130 changes: 63 additions & 67 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,82 +805,78 @@ mod tests {
});
}

//#[test]
//fn select_many_of_many_proptest() {
//arbtest(|u| {
//let pool = UtxoPool::arbitrary(u)?;
#[test]
fn select_many_of_many_proptest() {
arbtest(|u| {
let pool = UtxoPool::arbitrary(u)?;
let utxos = pool.utxos.clone();

// generate all the possible utxos subsets
//let mut gen = exhaustigen::Gen::new();
//let mut subsets: Vec<Vec<&Utxo>> = Vec::new();
//while !gen.done() {
//let s = gen.gen_subset(&pool.utxos).collect::<Vec<_>>();
//subsets.push(s);
//}
let mut gen = exhaustigen::Gen::new();
let mut subsets: Vec<Vec<&Utxo>> = Vec::new();
while !gen.done() {
let s = gen.gen_subset(&pool.utxos).collect::<Vec<_>>();
subsets.push(s);
}

// choose a set at random to be the target
//let selection: &Vec<&Utxo> = u.choose(&subsets).unwrap();
let target_selection: &Vec<&Utxo> = u.choose(&subsets).unwrap();

// find the minmum fee_rate that will result in all utxos having a posiive
// effective_value
//let mut fee_rates: Vec<FeeRate> = selection.iter().map(|u| {
//calculate_max_fee_rate(u.value(), u.satisfaction_weight())
//}).collect();
//fee_rates.sort();

//let min_fee_rate = fee_rates.first().unwrap_or(&FeeRate::ZERO).to_sat_per_kwu();
//let fee_rate = arb_fee_rate_in_range(u, 0..=min_fee_rate);

//let effective_values: Vec<SignedAmount> = selection.iter().map(|u| {
//let e = effective_value(
//fee_rate,
//u.satisfaction_weight(),
//u.value()
//);

//if e.is_some() {
//e.unwrap()
//} else {
//SignedAmount::ZERO
//}
//}).collect();

//let eff_values_sum = effective_values.into_iter().checked_sum();

// if None, then this random subset is an invalid target
//if let Some(s) = eff_values_sum {
//let target = s.to_unsigned().unwrap();

//let coins = select_coins_bnb(
//target,
//Amount::ZERO,
//fee_rate,
//fee_rate,
//&pool.utxos,
//);

//if selection.is_empty() || target == Amount::ZERO {
//assert!(coins.is_none());
//} else {
//let result: Amount = coins.unwrap().map(|u| {
//effective_value(
//fee_rate,
//u.satisfaction_weight(),
//u.value(),
//)
//.unwrap()
//.to_unsigned()
//.unwrap()
//}).sum();

//assert_eq!(result, target);
//}
let mut fee_rates: Vec<FeeRate> = target_selection.iter().map(|u| {
calculate_max_fee_rate(u.value(), u.satisfaction_weight()).unwrap_or(FeeRate::ZERO)
}).collect();
fee_rates.sort();

//}
let min_fee_rate = fee_rates.first().unwrap_or(&FeeRate::ZERO).to_sat_per_kwu();
let fee_rate = arb_fee_rate_in_range(u, 0..=min_fee_rate);

//Ok(())
//});
//}
let effective_values: Vec<SignedAmount> = target_selection.iter().map(|u| {
let e = effective_value(
fee_rate,
u.satisfaction_weight(),
u.value()
);

e.unwrap_or(SignedAmount::ZERO)
}).collect();

let eff_values_sum = effective_values.into_iter().checked_sum();

// if None, then this random subset is an invalid target (skip)
if let Some(s) = eff_values_sum {
if let Ok(target) = s.to_unsigned() {
let result = select_coins_bnb(
target,
Amount::ZERO,
fee_rate,
fee_rate,
&utxos,
);

if let Some(r) = result {
let effective_value_sum: Amount = r.map(|u| {
effective_value(
fee_rate,
u.satisfaction_weight(),
u.value(),
)
.unwrap()
.to_unsigned()
.unwrap()
}).sum();
assert_eq!(effective_value_sum, target);
} else {
let available_value = utxos.into_iter().map(|u| u.value()).checked_sum();
assert!(available_value.is_none() || target_selection.is_empty() || target == Amount::ZERO);
}
}
}

Ok(())
});
}

//#[test]
//fn select_bnb_proptest() {
Expand Down

0 comments on commit a144874

Please sign in to comment.