Skip to content

Commit

Permalink
Add weight to weighted_utxo
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Nov 20, 2024
1 parent 2aced45 commit 75f99d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ mod tests {
let mut pool = vec![];

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

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

let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &w_utxos);
Expand Down Expand Up @@ -639,7 +639,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| build_utxo(a, Weight::ZERO, Weight::ZERO)).collect();

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

Expand All @@ -658,7 +658,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| build_utxo(a, Weight::ZERO, Weight::ZERO)).collect();

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

let mut list = select_coins_bnb(
Amount::from_sat(target),
Expand Down
44 changes: 19 additions & 25 deletions src/coin_grinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ mod tests {

let mut pool = Vec::new();
for i in 0..10 {
let one_cbtc = build_utxo(Amount::from_str("1 cBTC").unwrap(), Weight::ZERO);
let two_cbtc = build_utxo(Amount::from_str("2 cBTC").unwrap(), Weight::ZERO);
let one_cbtc = build_utxo(Amount::from_str("1 cBTC").unwrap(), Weight::ZERO, Weight::ZERO);
let two_cbtc = build_utxo(Amount::from_str("2 cBTC").unwrap(), Weight::ZERO, Weight::ZERO);
pool.push(one_cbtc);
pool.push(two_cbtc);
}
Expand All @@ -138,7 +138,7 @@ mod tests {
//from_str like Amount::from_str()
let target = Amount::from_str(p.target).unwrap();
let change_target = Amount::from_str(p.change_target).unwrap();
let fee_rate = FeeRate::from_sat_per_kwu(fee_rate);
let fee_rate = FeeRate::from_sat_per_vb(fee_rate).unwrap();
let max_weight = Weight::from_str(p.max_weight).unwrap();

let w_utxos: Vec<_> = p
Expand All @@ -159,7 +159,7 @@ mod tests {
_ => panic!(),
}
})
.map(|(a, w)| build_utxo(a, w))
.map(|(a, w)| build_utxo(a, w, w - Weight::from_wu(40)))
.collect();

let c = coin_grinder(target, change_target, max_weight, fee_rate, &w_utxos);
Expand Down Expand Up @@ -192,33 +192,27 @@ mod tests {
// 110 * segwit multiplyer + input_base_weight =
// 110 * 4 + 160 =
// 150
let target = Amount::from_str("30 BTC").unwrap();
let max_weight = Weight::from_wu(400_000);
let change_target = Amount::from_str("1 BTC").unwrap();

let fee_rate = FeeRate::from_sat_per_vb(5).unwrap();

let params = ParamsStr {
target: "30 BTC",
change_target: "1 BTC",
max_weight: "400000",
fee_rate: "5", //from sat per vb
weighted_utxos: vec![
"3 BTC/310",
"6 BTC/310",
"9 BTC/310",
"12 BTC/310",
"15 BTC/310",
"2 BTC/210",
"5 BTC/210",
"8 BTC/210",
"11 BTC/210",
"14 BTC/210",
"1 BTC/110",
"4 BTC/110",
"7 BTC/110",
"10 BTC/110",
"13 BTC/110",
"3 BTC/350",
"6 BTC/350",
"9 BTC/350",
"12 BTC/350",
"15 BTC/350",
"2 BTC/250",
"5 BTC/250",
"8 BTC/250",
"11 BTC/250",
"14 BTC/250",
"1 BTC/150",
"4 BTC/150",
"7 BTC/150",
"10 BTC/150",
"13 BTC/150",
]
};
}
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub trait WeightedUtxo {
/// <https://github.com/bitcoindevkit/bdk/blob/feafaaca31a0a40afc03ce98591d151c48c74fa2/crates/bdk/src/types.rs#L181>
fn satisfaction_weight(&self) -> Weight;

/// The weight
fn weight(&self) -> Weight;

/// The UTXO value.
fn value(&self) -> Amount;

Expand Down Expand Up @@ -119,7 +122,8 @@ mod tests {
.map(|a| {
let amt = Amount::from_sat(*a);
let weight = Weight::ZERO;
build_utxo(amt, weight)
let satisfaction_weight = Weight::ZERO;
build_utxo(amt, weight, satisfaction_weight)
})
.collect();

Expand All @@ -129,16 +133,18 @@ mod tests {
#[derive(Debug)]
pub struct Utxo {
pub output: TxOut,
pub weight: Weight,
pub satisfaction_weight: Weight,
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/single_random_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod tests {
let mut pool = vec![];

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

Expand Down Expand Up @@ -161,7 +161,7 @@ mod tests {
_ => panic!(),
}
})
.map(|(a, w)| build_utxo(a, w))
.map(|(a, w)| build_utxo(a, Weight::ZERO, w))
.collect();

let result = select_coins_srd(target, fee_rate, &w_utxos, &mut get_rng());
Expand Down

0 comments on commit 75f99d2

Please sign in to comment.