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.
This commit is contained in:
Tobin C. Harding 2025-05-21 11:09:06 +10:00
parent 399bca531c
commit 2e0b88ba76
No known key found for this signature in database
GPG Key ID: 0AEF0A899E41F7DD
2 changed files with 7 additions and 4 deletions

View File

@ -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<Amount> {
self.minimal_non_dust_internal(dust_relay_fee.to_sat_per_kwu_ceil() * 4)
fn minimal_non_dust_custom(&self, dust_relay: FeeRate) -> Option<Amount> {
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<Amount> {
fn minimal_non_dust_internal(&self, dust_relay_fee_rate_per_kvb: u64) -> Option<Amount> {
// 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() {

View File

@ -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.