From f7c76f5c19e6843cad3358c974f5b160c4b0b0c2 Mon Sep 17 00:00:00 2001 From: yancy Date: Tue, 21 Nov 2023 07:21:47 +0100 Subject: [PATCH] Move txin base weight calculation The base weight calculation was previously provided by rust-bitcoin but has since been removed. Adding the base weight calculation to this local crate instead. --- Cargo.toml | 2 +- src/lib.rs | 3 +++ src/single_random_draw.rs | 8 ++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 181920d1..6ba3eb0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["crypto", "bitcoin"] readme = "README.md" [dependencies] -bitcoin = { git="https://github.com/rust-bitcoin/rust-bitcoin", branch="master" } +bitcoin = { git="https://github.com/yancyribbens/rust-bitcoin.git", branch="add-from-vb-const" } rand = {version = "0.8.5", default-features = false, optional = true} [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index a5653eb7..3eef7162 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,6 +41,9 @@ 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); +/// The weight of a `TxIn` excluding the `script_sig` and `witness`. +pub const BASE_WEIGHT: Weight = Weight::from_vb_const(32 + 4 + 4); + /// This struct contains the weight of all params needed to satisfy the UTXO. /// /// The idea of using a WeightUtxo type was inspired by the BDK implementation: diff --git a/src/single_random_draw.rs b/src/single_random_draw.rs index 42c602f3..ac4c7e5c 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::BASE_WEIGHT; use bitcoin::Amount; use bitcoin::FeeRate; -use bitcoin::TxIn; use rand::seq::SliceRandom; /// Calculates the effective_value of an input. @@ -24,8 +24,8 @@ fn get_effective_value( let satisfaction_weight = weighted_utxo.satisfaction_weight; let weight = satisfaction_weight - .checked_add(TxIn::BASE_WEIGHT) - .ok_or(Error::AdditionOverflow(satisfaction_weight, TxIn::BASE_WEIGHT))?; + .checked_add(BASE_WEIGHT) + .ok_or(Error::AdditionOverflow(satisfaction_weight, BASE_WEIGHT))?; let input_fee = fee_rate .checked_mul_by_weight(weight) @@ -239,6 +239,6 @@ mod tests { let result: Error = select_coins_srd(target, FEE_RATE, &mut weighted_utxos, &mut get_rng()) .expect_err("expected error"); - assert_eq!(result.to_string(), "18446744073709551615 + 40 exceeds u64 Max"); + assert_eq!(result.to_string(), "18446744073709551615 + 160 exceeds u64 Max"); } }