Skip to content

Commit

Permalink
Update src/single_random_draw.rs
Browse files Browse the repository at this point in the history
Co-authored-by: Thibaut Le Guilly <[email protected]>
  • Loading branch information
yancyribbens and Tibo-lg committed Aug 18, 2023
1 parent 66871e3 commit b132883
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ keywords = ["crypto", "bitcoin"]
readme = "README.md"

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

[dev-dependencies]
rust-bitcoin-coin-selection = {path = ".", features = ["rand"]}
rand = "0.8.5"

[patch.crates-io]
bitcoin_hashes = { git="https://github.com/yancyribbens/rust-bitcoin.git" }
bitcoin_hashes = { git="https://github.com/rust-bitcoin/rust-bitcoin.git" }
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ pub trait Utxo: Clone {
// https://github.com/bitcoin/bitcoin/blob/f722a9bd132222d9d5cd503b5af25c905b205cdb/src/wallet/coinselection.h#L20
const CHANGE_LOWER: Amount = Amount::from_sat(50_000);

// TODO add to Rust-bitcoin
/// The base weight is the output (32 + 4) + nSequence 4
/// <https://github.com/bitcoin/bitcoin/blob/cd43a8444ba44f86ddbb313a03a2782482beda89/src/primitives/transaction.h#L74>
pub const TXIN_BASE_WEIGHT: Weight = Weight::from_wu(32 + 4 + 4);

// TODO: Use miniscript's max_weight_to_satisfy() method to calculate the
// max satisfaction weight instead. Currently, implementers of this crate
// are required to loop through each UTXO and calculate the satisfaction_weight.
Expand Down
20 changes: 8 additions & 12 deletions src/single_random_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use crate::errors::Error;
use crate::WeightedUtxo;
use crate::CHANGE_LOWER;
use crate::TXIN_BASE_WEIGHT;
use bitcoin::Amount;
use bitcoin::FeeRate;
use bitcoin::TxIn;
use rand::seq::SliceRandom;

/// Calculates the effective_value of an input.
Expand All @@ -23,19 +23,15 @@ fn get_effective_value(
) -> Result<Option<Amount>, Error> {
let satisfaction_weight = weighted_utxo.satisfaction_weight;

let checked_weight = satisfaction_weight.checked_add(TXIN_BASE_WEIGHT);
let weight = satisfaction_weight
.checked_add(TxIn::BASE_WEIGHT)
.ok_or(Error::AdditionOverflow(satisfaction_weight, TxIn::BASE_WEIGHT))?;

let weight = match checked_weight {
Some(w) => w,
None => return Err(Error::AdditionOverflow(satisfaction_weight, TXIN_BASE_WEIGHT)),
};
let input_fee = fee_rate
.checked_mul_by_weight(weight)
.ok_or(Error::MultiplicationOverflow(satisfaction_weight, fee_rate))?;

let input_fee: Option<Amount> = fee_rate.checked_mul_by_weight(weight);

match input_fee {
Some(f) => Ok(weighted_utxo.utxo.value.checked_sub(f)),
None => Err(Error::MultiplicationOverflow(satisfaction_weight, fee_rate)),
}
Ok(weighted_utxo.utxo.value.checked_sub(input_fee))
}

/// Randomly select coins for the given target by shuffling the UTXO pool and
Expand Down

0 comments on commit b132883

Please sign in to comment.