Skip to content

Commit

Permalink
Amount and SignedAmount now have a cap of 21 Million
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Dec 14, 2024
1 parent 4472ffa commit 1cb039a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["bitcoin", "coin-selection", "coin", "coinselection", "utxo"]
readme = "README.md"

[dependencies]
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
bitcoin = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
rand = {version = "0.8.5", default-features = false, optional = true}

[dev-dependencies]
Expand All @@ -28,10 +28,10 @@ harness = false


[patch.crates-io]
bitcoin_hashes = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
base58ck = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
bitcoin-internals = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
bitcoin-io = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
bitcoin-primitives = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
bitcoin-addresses = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
bitcoin-units = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git" }
bitcoin_hashes = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
base58ck = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
bitcoin-internals = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
bitcoin-io = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
bitcoin-primitives = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
bitcoin-addresses = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
bitcoin-units = { git = "https://github.com/yancyribbens/rust-bitcoin", rev="0a6ac8456247dc2583465528b444cd33aadc59be" }
14 changes: 9 additions & 5 deletions src/branch_and_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
let mut index_selection: Vec<usize> = vec![];
let mut best_selection: Vec<usize> = vec![];

println!("add: {:?} {:?}", target, cost_of_change);
let upper_bound = target.checked_add(cost_of_change)?;

// Creates a tuple of (effective_value, waste, weighted_utxo)
Expand Down Expand Up @@ -234,8 +235,8 @@ pub fn select_coins_bnb<Utxo: WeightedUtxo>(
else if value >= target {
backtrack = true;

let v = value.to_signed().ok()?;
let t = target.to_signed().ok()?;
let v = value.to_signed();
let t = target.to_signed();
let waste: SignedAmount = v.checked_sub(t)?;
current_waste = current_waste.checked_add(waste)?;

Expand Down Expand Up @@ -379,6 +380,7 @@ mod tests {
// published. See: https://github.com/rust-bitcoin/rust-bitcoin/pull/3346
fn amount_from_str_patch(amount: &str) -> Amount {
let a = Amount::from_str(amount);
println!("{:?}", a);

match a {
Ok(a) => a,
Expand All @@ -392,10 +394,13 @@ mod tests {
let lt_fee_rate = p.lt_fee_rate.parse::<u64>().unwrap();

let target = amount_from_str_patch(p.target);

println!("{}", p.cost_of_change);
let cost_of_change = amount_from_str_patch(p.cost_of_change);
let fee_rate = FeeRate::from_sat_per_kwu(fee_rate);
let lt_fee_rate = FeeRate::from_sat_per_kwu(lt_fee_rate);

println!("cost of change {:?}", cost_of_change);
let w_utxos: Vec<_> = p
.weighted_utxos
.iter()
Expand All @@ -404,7 +409,6 @@ mod tests {
.collect();

let iter = select_coins_bnb(target, cost_of_change, fee_rate, lt_fee_rate, &w_utxos);

if expected_inputs.is_none() {
assert!(iter.is_none());
} else {
Expand Down Expand Up @@ -565,7 +569,7 @@ mod tests {
cost_of_change: "0",
fee_rate: "0",
lt_fee_rate: "0",
weighted_utxos: vec!["18446744073709551615 sats", "1 sats"], // [u64::MAX, 1 sat]
weighted_utxos: vec!["2100000000000000 sats", "1 sats"], // [Amount::MAX, 1 sat]
};

assert_coin_select_params(&params, None);
Expand All @@ -575,7 +579,7 @@ mod tests {
fn select_coins_bnb_upper_bound_overflow() {
let params = ParamsStr {
target: "1 sats",
cost_of_change: "18446744073709551615 sats", // u64::MAX
cost_of_change: "2100000000000000 sats", // Amount::MAX
fee_rate: "0",
lt_fee_rate: "0",
weighted_utxos: vec!["1 sats"],
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub trait WeightedUtxo {
/// see also:
/// <https://github.com/rust-bitcoin/rust-bitcoin/blob/59c806996ce18e88394eb4e2c265986c8d3a6620/bitcoin/src/blockdata/transaction.rs>
fn effective_value(&self, fee_rate: FeeRate) -> Option<SignedAmount> {
let signed_input_fee = self.calculate_fee(fee_rate)?.to_signed().ok()?;
self.value().to_signed().ok()?.checked_sub(signed_input_fee)
let signed_input_fee = self.calculate_fee(fee_rate)?.to_signed();
self.value().to_signed().checked_sub(signed_input_fee)
}

/// Computes the fee to spend this `Utxo`.
Expand All @@ -78,8 +78,8 @@ pub trait WeightedUtxo {
/// The waste is the difference of the fee to spend this `Utxo` now compared with the expected
/// fee to spend in the future (long_term_fee_rate).
fn waste(&self, fee_rate: FeeRate, long_term_fee_rate: FeeRate) -> Option<SignedAmount> {
let fee: SignedAmount = self.calculate_fee(fee_rate)?.to_signed().ok()?;
let lt_fee: SignedAmount = self.calculate_fee(long_term_fee_rate)?.to_signed().ok()?;
let fee: SignedAmount = self.calculate_fee(fee_rate)?.to_signed();
let lt_fee: SignedAmount = self.calculate_fee(long_term_fee_rate)?.to_signed();
fee.checked_sub(lt_fee)
}
}
Expand Down
20 changes: 4 additions & 16 deletions src/single_random_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ pub fn select_coins_srd<'a, R: rand::Rng + ?Sized, Utxo: WeightedUtxo>(

result.clear();

let threshold = target + CHANGE_LOWER;
let threshold = target.checked_add(CHANGE_LOWER)?;

let mut value = Amount::ZERO;

for w_utxo in origin {
Expand Down Expand Up @@ -155,6 +156,7 @@ mod tests {
(a, w)
}
1 => {
println!("{}", v[0]);
let a = Amount::from_str(v[0]).unwrap();
(a, Weight::ZERO)
}
Expand Down Expand Up @@ -265,25 +267,11 @@ mod tests {
#[test]
fn select_coins_srd_threshold_overflow() {
let params = ParamsStr {
target: "18446744073709551615 sat", // u64::MAX
target: "2100000000000000 sats", // Amount::MAX
fee_rate: "10",
weighted_utxos: vec!["1 cBTC/18446744073709551615"],
};

assert_coin_select_params(&params, None);
}

#[test]
fn select_coins_srd_none_effective_value() {
let params = ParamsStr {
target: ".95 cBTC",
fee_rate: "0",
weighted_utxos: vec![
"1 cBTC",
"9223372036854775808 sat", //i64::MAX + 1
],
};

assert_coin_select_params(&params, Some(&["1 cBTC"]));
}
}

0 comments on commit 1cb039a

Please sign in to comment.