From 2e0b88ba76ebb5543c49d4184badaa1fe46fff39 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 21 May 2025 11:09:06 +1000 Subject: [PATCH] bitcoin: Fix dust 'fee' identifiers Currently we get the fee_rate per kwu then multiply it by 4. Instead lets add a per_kvb function to `FeeRate`. We are about to change the internal representation of `FeeRate` to use MvB so for now just panic on ovelflow. Also these are fee _rates_ - we already have suboptimal names from Core in the consts in `policy`, no need to let them infect other identifiers. --- bitcoin/src/blockdata/script/borrowed.rs | 8 ++++---- units/src/fee_rate/mod.rs | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bitcoin/src/blockdata/script/borrowed.rs b/bitcoin/src/blockdata/script/borrowed.rs index 41a89e4a7..97c294b33 100644 --- a/bitcoin/src/blockdata/script/borrowed.rs +++ b/bitcoin/src/blockdata/script/borrowed.rs @@ -289,8 +289,8 @@ crate::internal_macros::define_extension_trait! { /// To use the default Bitcoin Core value, use [`minimal_non_dust`]. /// /// [`minimal_non_dust`]: Script::minimal_non_dust - fn minimal_non_dust_custom(&self, dust_relay_fee: FeeRate) -> Option { - self.minimal_non_dust_internal(dust_relay_fee.to_sat_per_kwu_ceil() * 4) + fn minimal_non_dust_custom(&self, dust_relay: FeeRate) -> Option { + self.minimal_non_dust_internal(dust_relay.to_sat_per_kvb()) } /// Counts the sigops for this Script using accurate counting. @@ -407,10 +407,10 @@ mod sealed { crate::internal_macros::define_extension_trait! { pub(crate) trait ScriptExtPriv impl for Script { - fn minimal_non_dust_internal(&self, dust_relay_fee: u64) -> Option { + fn minimal_non_dust_internal(&self, dust_relay_fee_rate_per_kvb: u64) -> Option { // This must never be lower than Bitcoin Core's GetDustThreshold() (as of v0.21) as it may // otherwise allow users to create transactions which likely can never be broadcast/confirmed. - let sats = dust_relay_fee + let sats = dust_relay_fee_rate_per_kvb .checked_mul(if self.is_op_return() { 0 } else if self.is_witness_program() { diff --git a/units/src/fee_rate/mod.rs b/units/src/fee_rate/mod.rs index 84e8d9f86..f2f73e162 100644 --- a/units/src/fee_rate/mod.rs +++ b/units/src/fee_rate/mod.rs @@ -78,6 +78,9 @@ impl FeeRate { (self.to_sat_per_kwu_floor() + (1000 / 4 - 1)) / (1000 / 4) } + /// Converts to sat/kvb. + pub const fn to_sat_per_kvb(self) -> u64 { self.to_sat_per_kwu_floor() * 4 } + /// Checked multiplication. /// /// Computes `self * rhs` returning [`None`] if overflow occurred.