From 73b14d03b929a92e94a0614f50bf8f9df1ea3ee9 Mon Sep 17 00:00:00 2001 From: yancy Date: Tue, 4 Feb 2025 15:00:57 -0600 Subject: [PATCH 1/2] Add `to_fee` in place of `fee_wu` Use the more idiomatic to_fee instead of `fee_wu`. Since the method takes a strongly typed argument, remove `wu` from the method name to improve clarity. --- api/units/all-features.txt | 1 + api/units/alloc-only.txt | 1 + api/units/no-features.txt | 1 + bitcoin/src/blockdata/mod.rs | 2 +- units/src/fee.rs | 14 +++++++++++--- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/api/units/all-features.txt b/api/units/all-features.txt index 5d5fdeab7..8eb819b1f 100644 --- a/api/units/all-features.txt +++ b/api/units/all-features.txt @@ -1069,6 +1069,7 @@ pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: &bitcoin_uni pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: bitcoin_units::fee_rate::FeeRate) pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator +pub fn bitcoin_units::fee_rate::FeeRate::to_fee(self, weight: bitcoin_units::weight::Weight) -> core::option::Option pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: &str) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::boxed::Box) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::string::String) -> core::result::Result diff --git a/api/units/alloc-only.txt b/api/units/alloc-only.txt index 8aaeaeb7d..37c97385c 100644 --- a/api/units/alloc-only.txt +++ b/api/units/alloc-only.txt @@ -950,6 +950,7 @@ pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: &bitcoin_uni pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: bitcoin_units::fee_rate::FeeRate) pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator +pub fn bitcoin_units::fee_rate::FeeRate::to_fee(self, weight: bitcoin_units::weight::Weight) -> core::option::Option pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: &str) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::boxed::Box) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::string::String) -> core::result::Result diff --git a/api/units/no-features.txt b/api/units/no-features.txt index 19a530501..1464a2aa3 100644 --- a/api/units/no-features.txt +++ b/api/units/no-features.txt @@ -916,6 +916,7 @@ pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: &bitcoin_uni pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: bitcoin_units::fee_rate::FeeRate) pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator +pub fn bitcoin_units::fee_rate::FeeRate::to_fee(self, weight: bitcoin_units::weight::Weight) -> core::option::Option pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: &str) -> core::result::Result pub fn bitcoin_units::locktime::absolute::ConversionError::clone(&self) -> bitcoin_units::locktime::absolute::ConversionError pub fn bitcoin_units::locktime::absolute::ConversionError::eq(&self, other: &bitcoin_units::locktime::absolute::ConversionError) -> bool diff --git a/bitcoin/src/blockdata/mod.rs b/bitcoin/src/blockdata/mod.rs index a8d0af44b..e9e803fc8 100644 --- a/bitcoin/src/blockdata/mod.rs +++ b/bitcoin/src/blockdata/mod.rs @@ -43,7 +43,7 @@ pub mod fee_rate { let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/byte is valid"); - assert_eq!(rate.fee_vb(tx.vsize().to_u64()), rate.fee_wu(tx.weight())); + assert_eq!(rate.fee_vb(tx.vsize().to_u64()), rate.to_fee(tx.weight())); } } } diff --git a/units/src/fee.rs b/units/src/fee.rs index f53a15c89..23327cede 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -118,6 +118,14 @@ impl FeeRate { /// /// This is equivalent to `Self::checked_mul_by_weight()`. #[must_use] + pub fn to_fee(self, weight: Weight) -> Option { self.checked_mul_by_weight(weight) } + + /// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`] + /// if an overflow occurred. + /// + /// This is equivalent to `Self::checked_mul_by_weight()`. + #[must_use] + #[deprecated(since = "TBD", note = "use `to_fee()` instead")] pub fn fee_wu(self, weight: Weight) -> Option { self.checked_mul_by_weight(weight) } /// Calculates the fee by multiplying this fee rate by weight, in virtual bytes, returning [`None`] @@ -127,7 +135,7 @@ impl FeeRate { /// `Self::fee_wu(weight)`. #[must_use] pub fn fee_vb(self, vb: u64) -> Option { - Weight::from_vb(vb).and_then(|w| self.fee_wu(w)) + Weight::from_vb(vb).and_then(|w| self.to_fee(w)) } /// Checked weight multiplication. @@ -218,12 +226,12 @@ mod tests { #[test] fn fee_wu() { - let fee_overflow = FeeRate::from_sat_per_kwu(10).fee_wu(Weight::MAX); + let fee_overflow = FeeRate::from_sat_per_kwu(10).to_fee(Weight::MAX); assert!(fee_overflow.is_none()); let fee_rate = FeeRate::from_sat_per_vb(2).unwrap(); let weight = Weight::from_vb(3).unwrap(); - assert_eq!(fee_rate.fee_wu(weight).unwrap(), Amount::from_sat_unchecked(6)); + assert_eq!(fee_rate.to_fee(weight).unwrap(), Amount::from_sat_unchecked(6)); } #[test] From a7526b6a70642e04f0a8ad8a89aa368031fdb6cd Mon Sep 17 00:00:00 2001 From: yancy Date: Tue, 4 Feb 2025 15:13:56 -0600 Subject: [PATCH 2/2] Remove `fee_vb` This is redundant given Weight::from_vb is provided. After converting to a weight_unit, use to_fee(). --- bitcoin/src/blockdata/mod.rs | 24 ------------------------ units/src/fee.rs | 10 +--------- 2 files changed, 1 insertion(+), 33 deletions(-) diff --git a/bitcoin/src/blockdata/mod.rs b/bitcoin/src/blockdata/mod.rs index e9e803fc8..02fcb250f 100644 --- a/bitcoin/src/blockdata/mod.rs +++ b/bitcoin/src/blockdata/mod.rs @@ -22,30 +22,6 @@ pub use self::{ pub mod fee_rate { /// Re-export everything from the [`units::fee_rate`] module. pub use units::fee_rate::FeeRate; - - #[cfg(test)] - mod tests { - use internals::ToU64 as _; - - use super::*; - - #[test] - fn fee_convenience_functions_agree() { - use hex::test_hex_unwrap as hex; - - use crate::consensus::Decodable; - use crate::transaction::{Transaction, TransactionExt as _}; - - const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"; - - let raw_tx = hex!(SOME_TX); - let tx: Transaction = Decodable::consensus_decode(&mut raw_tx.as_slice()).unwrap(); - - let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/byte is valid"); - - assert_eq!(rate.fee_vb(tx.vsize().to_u64()), rate.to_fee(tx.weight())); - } - } } /// Provides absolute and relative locktimes. diff --git a/units/src/fee.rs b/units/src/fee.rs index 23327cede..94601db87 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -134,6 +134,7 @@ impl FeeRate { /// This is equivalent to converting `vb` to [`Weight`] using [`Weight::from_vb`] and then calling /// `Self::fee_wu(weight)`. #[must_use] + #[deprecated(since = "TBD", note = "use Weight::from_vb and then `to_fee()` instead")] pub fn fee_vb(self, vb: u64) -> Option { Weight::from_vb(vb).and_then(|w| self.to_fee(w)) } @@ -234,15 +235,6 @@ mod tests { assert_eq!(fee_rate.to_fee(weight).unwrap(), Amount::from_sat_unchecked(6)); } - #[test] - fn fee_vb() { - let fee_overflow = FeeRate::from_sat_per_kwu(10).fee_vb(Weight::MAX.to_wu()); - assert!(fee_overflow.is_none()); - - let fee_rate = FeeRate::from_sat_per_vb(2).unwrap(); - assert_eq!(fee_rate.fee_vb(3).unwrap(), Amount::from_sat_unchecked(6)); - } - #[test] fn checked_weight_mul() { let weight = Weight::from_vb(10).unwrap();