Skip to content

Commit 1c07916

Browse files
committed
Merge rust-bitcoin#4538: Use _u32 in FeeRate constructor instead of _unchecked
a1ce2d1 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 a1ce2d1; successfully ran local tests Tree-SHA512: 339974c954ad613b0be684f7b2fa1402f59fe401969f3785a702ffbb14ac913ecf4c8228240d068ba4b482e38263590154167a071d823ccc9b4d0691036ca484
2 parents 2f2c914 + a1ce2d1 commit 1c07916

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

bitcoin/src/blockdata/script/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ fn default_dust_value() {
751751
assert!(script_p2wpkh.is_p2wpkh());
752752
assert_eq!(script_p2wpkh.minimal_non_dust(), Amount::from_sat_u32(294));
753753
assert_eq!(
754-
script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
754+
script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_u32(6)),
755755
Some(Amount::from_sat_u32(588))
756756
);
757757

@@ -765,7 +765,7 @@ fn default_dust_value() {
765765
assert!(script_p2pkh.is_p2pkh());
766766
assert_eq!(script_p2pkh.minimal_non_dust(), Amount::from_sat_u32(546));
767767
assert_eq!(
768-
script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
768+
script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_u32(6)),
769769
Some(Amount::from_sat_u32(1092))
770770
);
771771
}

bitcoin/src/psbt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl Psbt {
131131
/// 1000 sats/vByte. 25k sats/vByte is obviously a mistake at this point.
132132
///
133133
/// [`extract_tx`]: Psbt::extract_tx
134-
pub const DEFAULT_MAX_FEE_RATE: FeeRate = FeeRate::from_sat_per_vb_unchecked(25_000);
134+
pub const DEFAULT_MAX_FEE_RATE: FeeRate = FeeRate::from_sat_per_vb_u32(25_000);
135135

136136
/// An alias for [`extract_tx_fee_rate_limit`].
137137
///

units/src/fee_rate/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ impl FeeRate {
4949
/// Minimum fee rate required to broadcast a transaction.
5050
///
5151
/// The value matches the default Bitcoin Core policy at the time of library release.
52-
pub const BROADCAST_MIN: FeeRate = FeeRate::from_sat_per_vb_unchecked(1);
52+
pub const BROADCAST_MIN: FeeRate = FeeRate::from_sat_per_vb_u32(1);
5353

5454
/// Fee rate used to compute dust amount.
55-
pub const DUST: FeeRate = FeeRate::from_sat_per_vb_unchecked(3);
55+
pub const DUST: FeeRate = FeeRate::from_sat_per_vb_u32(3);
5656

5757
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes.
5858
///
@@ -66,8 +66,9 @@ impl FeeRate {
6666
Some(FeeRate::from_sat_per_kwu(sat_vb.checked_mul(1000 / 4)?))
6767
}
6868

69-
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes without overflow check.
70-
pub const fn from_sat_per_vb_unchecked(sat_vb: u64) -> Self {
69+
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes.
70+
pub const fn from_sat_per_vb_u32(sat_vb: u32) -> Self {
71+
let sat_vb = sat_vb as u64; // No `Into` in const context.
7172
FeeRate::from_sat_per_kwu(sat_vb * (1000 / 4))
7273
}
7374

@@ -299,15 +300,14 @@ mod tests {
299300
}
300301

301302
#[test]
302-
fn from_sat_per_vb_unchecked() {
303-
let fee_rate = FeeRate::from_sat_per_vb_unchecked(10);
303+
fn from_sat_per_vb_u32() {
304+
let fee_rate = FeeRate::from_sat_per_vb_u32(10);
304305
assert_eq!(FeeRate::from_sat_per_kwu(2500), fee_rate);
305306
}
306307

307308
#[test]
308309
#[cfg(debug_assertions)]
309-
#[should_panic = "attempt to multiply with overflow"]
310-
fn from_sat_per_vb_unchecked_panic() { FeeRate::from_sat_per_vb_unchecked(u64::MAX); }
310+
fn from_sat_per_vb_u32_cannot_panic() { FeeRate::from_sat_per_vb_u32(u32::MAX); }
311311

312312
#[test]
313313
fn raw_feerate() {

0 commit comments

Comments
 (0)