Merge rust-bitcoin/rust-bitcoin#4538: Use `_u32` in `FeeRate` constructor instead of `_unchecked`

a1ce2d1ac8 Use _u32 in FeeRate constructor instead of _unchecked (Tobin C. Harding)

Pull request description:

  When constructing an `Amount` it was observed recently that a `u32` is often big enough. For the same reason we can use a `u32` to construct a fee rate instead of overflowing.

  Use `_u32`in the constructor and remove the panic.

ACKs for top commit:
  apoelstra:
    ACK a1ce2d1ac8d646b63532ed9ac459ffea39ec8c20; successfully ran local tests

Tree-SHA512: 339974c954ad613b0be684f7b2fa1402f59fe401969f3785a702ffbb14ac913ecf4c8228240d068ba4b482e38263590154167a071d823ccc9b4d0691036ca484
This commit is contained in:
merge-script 2025-05-23 16:18:59 +00:00
commit 1c07916777
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 11 additions and 11 deletions

View File

@ -751,7 +751,7 @@ fn default_dust_value() {
assert!(script_p2wpkh.is_p2wpkh()); assert!(script_p2wpkh.is_p2wpkh());
assert_eq!(script_p2wpkh.minimal_non_dust(), Amount::from_sat_u32(294)); assert_eq!(script_p2wpkh.minimal_non_dust(), Amount::from_sat_u32(294));
assert_eq!( assert_eq!(
script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)), script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_u32(6)),
Some(Amount::from_sat_u32(588)) Some(Amount::from_sat_u32(588))
); );
@ -765,7 +765,7 @@ fn default_dust_value() {
assert!(script_p2pkh.is_p2pkh()); assert!(script_p2pkh.is_p2pkh());
assert_eq!(script_p2pkh.minimal_non_dust(), Amount::from_sat_u32(546)); assert_eq!(script_p2pkh.minimal_non_dust(), Amount::from_sat_u32(546));
assert_eq!( assert_eq!(
script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)), script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_u32(6)),
Some(Amount::from_sat_u32(1092)) Some(Amount::from_sat_u32(1092))
); );
} }

View File

@ -131,7 +131,7 @@ impl Psbt {
/// 1000 sats/vByte. 25k sats/vByte is obviously a mistake at this point. /// 1000 sats/vByte. 25k sats/vByte is obviously a mistake at this point.
/// ///
/// [`extract_tx`]: Psbt::extract_tx /// [`extract_tx`]: Psbt::extract_tx
pub const DEFAULT_MAX_FEE_RATE: FeeRate = FeeRate::from_sat_per_vb_unchecked(25_000); pub const DEFAULT_MAX_FEE_RATE: FeeRate = FeeRate::from_sat_per_vb_u32(25_000);
/// An alias for [`extract_tx_fee_rate_limit`]. /// An alias for [`extract_tx_fee_rate_limit`].
/// ///

View File

@ -49,10 +49,10 @@ impl FeeRate {
/// Minimum fee rate required to broadcast a transaction. /// Minimum fee rate required to broadcast a transaction.
/// ///
/// The value matches the default Bitcoin Core policy at the time of library release. /// The value matches the default Bitcoin Core policy at the time of library release.
pub const BROADCAST_MIN: FeeRate = FeeRate::from_sat_per_vb_unchecked(1); pub const BROADCAST_MIN: FeeRate = FeeRate::from_sat_per_vb_u32(1);
/// Fee rate used to compute dust amount. /// Fee rate used to compute dust amount.
pub const DUST: FeeRate = FeeRate::from_sat_per_vb_unchecked(3); pub const DUST: FeeRate = FeeRate::from_sat_per_vb_u32(3);
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes. /// Constructs a new [`FeeRate`] from satoshis per virtual bytes.
/// ///
@ -66,8 +66,9 @@ impl FeeRate {
Some(FeeRate::from_sat_per_kwu(sat_vb.checked_mul(1000 / 4)?)) Some(FeeRate::from_sat_per_kwu(sat_vb.checked_mul(1000 / 4)?))
} }
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes without overflow check. /// Constructs a new [`FeeRate`] from satoshis per virtual bytes.
pub const fn from_sat_per_vb_unchecked(sat_vb: u64) -> Self { pub const fn from_sat_per_vb_u32(sat_vb: u32) -> Self {
let sat_vb = sat_vb as u64; // No `Into` in const context.
FeeRate::from_sat_per_kwu(sat_vb * (1000 / 4)) FeeRate::from_sat_per_kwu(sat_vb * (1000 / 4))
} }
@ -299,15 +300,14 @@ mod tests {
} }
#[test] #[test]
fn from_sat_per_vb_unchecked() { fn from_sat_per_vb_u32() {
let fee_rate = FeeRate::from_sat_per_vb_unchecked(10); let fee_rate = FeeRate::from_sat_per_vb_u32(10);
assert_eq!(FeeRate::from_sat_per_kwu(2500), fee_rate); assert_eq!(FeeRate::from_sat_per_kwu(2500), fee_rate);
} }
#[test] #[test]
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
#[should_panic = "attempt to multiply with overflow"] fn from_sat_per_vb_u32_cannot_panic() { FeeRate::from_sat_per_vb_u32(u32::MAX); }
fn from_sat_per_vb_unchecked_panic() { FeeRate::from_sat_per_vb_unchecked(u64::MAX); }
#[test] #[test]
fn raw_feerate() { fn raw_feerate() {