Skip to content

Commit

Permalink
Switch to weight instead of satisfaction_weight
Browse files Browse the repository at this point in the history
Core uses just weight in coin-grinder, and it's complicated to maintain
using both satisfaction_weight and weight.  Therefore, switch to just
weight units for all algorithms.
  • Loading branch information
yancyribbens committed Feb 14, 2025
1 parent 6c3772f commit 40bb187
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 90 deletions.
10 changes: 4 additions & 6 deletions benches/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
#[derive(Debug, Clone)]
pub struct Utxo {
output: TxOut,
satisfaction_weight: Weight,
weight: Weight,
}

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

fn weight(&self) -> Weight { Weight::ZERO }
fn weight(&self) -> Weight { self.weight }
fn value(&self) -> Amount { self.output.value }
}

Expand All @@ -21,12 +19,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let one = Utxo {
output: TxOut { value: Amount::from_sat(1_000), script_pubkey: ScriptBuf::new() },
satisfaction_weight: Weight::ZERO,
weight: Weight::ZERO,
};

let two = Utxo {
output: TxOut { value: Amount::from_sat(3), script_pubkey: ScriptBuf::new() },
satisfaction_weight: Weight::ZERO,
weight: Weight::ZERO,
};

let target = Amount::from_sat(1_003);
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/select_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use libfuzzer_sys::fuzz_target;
#[derive(Arbitrary, Debug)]
pub struct Utxo {
output: TxOut,
satisfaction_weight: Weight,
weight: Weight,
}

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

fn value(&self) -> Amount {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/select_coins_bnb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use libfuzzer_sys::fuzz_target;
#[derive(Arbitrary, Debug)]
pub struct Utxo {
output: TxOut,
satisfaction_weight: Weight
weight: Weight
}

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

fn value(&self) -> Amount {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/select_coins_srd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use rand::thread_rng;
#[derive(Arbitrary, Debug)]
pub struct Utxo {
output: TxOut,
satisfaction_weight: Weight
weight: Weight
}

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

fn value(&self) -> Amount {
Expand Down
30 changes: 13 additions & 17 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@ mod tests {
use crate::tests::{assert_proptest_bnb, build_utxo, Utxo, UtxoPool};
use crate::WeightedUtxo;

const TX_IN_BASE_WEIGHT: u64 = 160;

#[derive(Debug)]
pub struct ParamsStr<'a> {
target: &'a str,
Expand All @@ -350,7 +348,7 @@ mod tests {
let mut pool = vec![];

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

Expand Down Expand Up @@ -393,7 +391,7 @@ mod tests {
.weighted_utxos
.iter()
.map(|s| Amount::from_str(s).unwrap())
.map(|a| build_utxo(a, Weight::ZERO, Weight::ZERO))
.map(|a| build_utxo(a, Weight::from_wu(160)))
.collect();

let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &w_utxos);
Expand Down Expand Up @@ -427,8 +425,6 @@ mod tests {
}

fn calculate_max_fee_rate(amount: Amount, weight: Weight) -> Option<FeeRate> {
let weight = weight + Weight::from_wu(TX_IN_BASE_WEIGHT);

let mut result = None;
if let Some(fee_rate) = amount.checked_div_by_weight_floor(weight) {
if fee_rate > FeeRate::ZERO {
Expand Down Expand Up @@ -672,7 +668,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, Weight::ZERO)).collect();
let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO)).collect();

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

Expand All @@ -691,7 +687,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, Weight::ZERO)).collect();
let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO)).collect();

let list = select_coins_bnb(
Amount::from_sat(target),
Expand Down Expand Up @@ -720,7 +716,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, Weight::ZERO)).collect();
let pool: Vec<_> = amts.into_iter().map(|a| build_utxo(a, Weight::ZERO)).collect();

let mut list = select_coins_bnb(
Amount::from_sat(target),
Expand All @@ -742,7 +738,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, Weight::ZERO);
let utxo = build_utxo(amount, Weight::ZERO);
let pool: Vec<Utxo> = vec![utxo.clone()];

let coins: Vec<Utxo> =
Expand All @@ -765,20 +761,20 @@ mod tests {

let utxo = u.choose(&utxos)?;

let max_fee_rate = calculate_max_fee_rate(utxo.value(), utxo.satisfaction_weight());
let max_fee_rate = calculate_max_fee_rate(utxo.value(), utxo.weight());
if let Some(f) = max_fee_rate {
let fee_rate = arb_fee_rate_in_range(u, 1..=f.to_sat_per_kwu());

let target_effective_value =
effective_value(fee_rate, utxo.satisfaction_weight(), utxo.value()).unwrap();
effective_value(fee_rate, utxo.weight() - Weight::from_wu(160), utxo.value()).unwrap();

if let Ok(target) = target_effective_value.to_unsigned() {
let result = select_coins_bnb(target, Amount::ZERO, fee_rate, fee_rate, &utxos);

if let Some(r) = result {
let sum: SignedAmount = r
.map(|u| {
effective_value(fee_rate, u.satisfaction_weight(), u.value())
effective_value(fee_rate, u.weight() - Weight::from_wu(160), u.value())
.unwrap()
})
.sum();
Expand All @@ -794,7 +790,7 @@ mod tests {
}

Ok(())
});
}).seed(0xe26be2f200000020);
}

#[test]
Expand All @@ -819,7 +815,7 @@ mod tests {
let mut fee_rates: Vec<FeeRate> = target_selection
.iter()
.map(|u| {
calculate_max_fee_rate(u.value(), u.satisfaction_weight())
calculate_max_fee_rate(u.value(), u.weight())
.unwrap_or(FeeRate::ZERO)
})
.collect();
Expand All @@ -831,7 +827,7 @@ mod tests {
let effective_values: Vec<SignedAmount> = target_selection
.iter()
.map(|u| {
let e = effective_value(fee_rate, u.satisfaction_weight(), u.value());
let e = effective_value(fee_rate, u.weight(), u.value());

e.unwrap_or(SignedAmount::ZERO)
})
Expand All @@ -847,7 +843,7 @@ mod tests {
if let Some(r) = result {
let effective_value_sum: Amount = r
.map(|u| {
effective_value(fee_rate, u.satisfaction_weight(), u.value())
effective_value(fee_rate, u.weight(), u.value())
.unwrap()
.to_unsigned()
.unwrap()
Expand Down
41 changes: 17 additions & 24 deletions src/coin_grinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,19 @@ mod tests {
.map(|s| {
let v: Vec<_> = s.split("/").collect();
match v.len() {
3 => {
let a = Amount::from_str(v[0]).unwrap();
let w = Weight::from_wu(v[1].parse().unwrap());
let s = Weight::from_wu(v[2].parse().unwrap());
(a, w, s)
}
2 => {
let a = Amount::from_str(v[0]).unwrap();
let w = Weight::from_wu(v[1].parse().unwrap());
let s = w - Weight::from_wu(160);
(a, w, s)
(a, w)
}
1 => {
let a = Amount::from_str(v[0]).unwrap();
(a, Weight::ZERO, Weight::ZERO)
(a, Weight::ZERO)
}
_ => panic!(),
}
})
.map(|(a, w, s)| build_utxo(a, w, s))
.map(|(a, w)| build_utxo(a, w))
.collect()
}

Expand Down Expand Up @@ -326,10 +319,10 @@ mod tests {
#[test]
fn min_tail_weight() {
let weighted_utxos = vec![
"10 sats/8/8",
"7 sats/4/4",
"5 sats/4/4",
"4 sats/8/8"
"10 sats/8",
"7 sats/4",
"5 sats/4",
"4 sats/8"
];

let utxos: Vec<_> = build_utxos(weighted_utxos);
Expand All @@ -348,10 +341,10 @@ mod tests {
#[test]
fn lookahead() {
let weighted_utxos = vec![
"10 sats/8/8",
"7 sats/4/4",
"5 sats/4/4",
"4 sats/8/8"
"10 sats/8",
"7 sats/4",
"5 sats/4",
"4 sats/8"
];

let utxos: Vec<_> = build_utxos(weighted_utxos);
Expand All @@ -377,10 +370,10 @@ mod tests {
max_weight: "100",
fee_rate: "0", //from sat per vb
weighted_utxos: vec![
"10 sats/8/8",
"7 sats/4/4",
"5 sats/4/4",
"4 sats/8/8"
"10 sats/8",
"7 sats/4",
"5 sats/4",
"4 sats/8"
]
};

Expand All @@ -395,8 +388,8 @@ mod tests {
max_weight: "10000",
fee_rate: "0",
weighted_utxos: vec![
"1 BTC/0/0",
"2 BTC/0/0",
"1 BTC/0",
"2 BTC/0",
]
};

Expand Down
Loading

0 comments on commit 40bb187

Please sign in to comment.