From b132883310bb9aefafe625536ed1b4d19657bd0b Mon Sep 17 00:00:00 2001 From: yancyribbens Date: Wed, 16 Aug 2023 13:16:08 +0200 Subject: [PATCH] Update src/single_random_draw.rs Co-authored-by: Thibaut Le Guilly --- Cargo.toml | 4 ++-- src/lib.rs | 5 ----- src/single_random_draw.rs | 20 ++++++++------------ 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab8cb3b..181920d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ 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] @@ -23,4 +23,4 @@ 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" } diff --git a/src/lib.rs b/src/lib.rs index f3edef4..3868944 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 -/// -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. diff --git a/src/single_random_draw.rs b/src/single_random_draw.rs index 465c796..ecff442 100644 --- a/src/single_random_draw.rs +++ b/src/single_random_draw.rs @@ -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. @@ -23,19 +23,15 @@ fn get_effective_value( ) -> Result, 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 = 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