Skip to content

Commit 235011f

Browse files
committed
Add assertions in the FeeRate constructor
Disallow negative, NaN, infinite or subnormal fee rate values.
1 parent 6bae52e commit 235011f

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

src/types.rs

+52-4
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,34 @@ impl AsRef<[u8]> for KeychainKind {
5151
pub struct FeeRate(f32);
5252

5353
impl FeeRate {
54+
/// Create a new instance checking the value provided
55+
///
56+
/// ## Panics
57+
///
58+
/// Panics if the value is not [normal](https://doc.rust-lang.org/std/primitive.f32.html#method.is_normal) (except if it's a positive zero) or negative.
59+
fn new_checked(value: f32) -> Self {
60+
assert!(value.is_normal() || value == 0.0);
61+
assert!(value.is_sign_positive());
62+
63+
FeeRate(value)
64+
}
65+
5466
/// Create a new instance of [`FeeRate`] given a float fee rate in btc/kvbytes
67+
///
68+
/// ## Panics
69+
///
70+
/// Panics if the value is not [normal](https://doc.rust-lang.org/std/primitive.f32.html#method.is_normal) (except if it's a positive zero) or negative.
5571
pub fn from_btc_per_kvb(btc_per_kvb: f32) -> Self {
56-
FeeRate(btc_per_kvb * 1e5)
72+
FeeRate::new_checked(btc_per_kvb * 1e5)
5773
}
5874

5975
/// Create a new instance of [`FeeRate`] given a float fee rate in satoshi/vbyte
60-
pub const fn from_sat_per_vb(sat_per_vb: f32) -> Self {
61-
FeeRate(sat_per_vb)
76+
///
77+
/// ## Panics
78+
///
79+
/// Panics if the value is not [normal](https://doc.rust-lang.org/std/primitive.f32.html#method.is_normal) (except if it's a positive zero) or negative.
80+
pub fn from_sat_per_vb(sat_per_vb: f32) -> Self {
81+
FeeRate::new_checked(sat_per_vb)
6282
}
6383

6484
/// Create a new [`FeeRate`] with the default min relay fee value
@@ -248,7 +268,35 @@ mod tests {
248268

249269
#[test]
250270
fn can_store_feerate_in_const() {
251-
const _MY_RATE: FeeRate = FeeRate::from_sat_per_vb(10.0);
252271
const _MIN_RELAY: FeeRate = FeeRate::default_min_relay_fee();
253272
}
273+
274+
#[test]
275+
#[should_panic]
276+
fn test_invalid_feerate_neg_zero() {
277+
let _ = FeeRate::from_sat_per_vb(-0.0);
278+
}
279+
280+
#[test]
281+
#[should_panic]
282+
fn test_invalid_feerate_neg_value() {
283+
let _ = FeeRate::from_sat_per_vb(-5.0);
284+
}
285+
286+
#[test]
287+
#[should_panic]
288+
fn test_invalid_feerate_nan() {
289+
let _ = FeeRate::from_sat_per_vb(f32::NAN);
290+
}
291+
292+
#[test]
293+
#[should_panic]
294+
fn test_invalid_feerate_inf() {
295+
let _ = FeeRate::from_sat_per_vb(f32::INFINITY);
296+
}
297+
298+
#[test]
299+
fn test_valid_feerate_pos_zero() {
300+
let _ = FeeRate::from_sat_per_vb(0.0);
301+
}
254302
}

0 commit comments

Comments
 (0)