You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge #694: Add assertions in the FeeRate constructor
235011f Add assertions in the FeeRate constructor (Alekos Filini)
Pull request description:
### Description
Disallow negative, NaN, infinite or subnormal fee rate values.
### Notes to the reviewers
This commit is technically an API break because it makes the `FeeRate::from_sat_per_vb` function non-const. I think it's worth it compared to the risk of having completely nonsensical fee rates (that can break the coin selection in interesting ways).
EDIT: it's also a breaking change because our code can now panic in scenarios where it didn't before. Again, I think it's worth it.
### Checklists
#### All Submissions:
* [x] I've signed all my commits
* [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md)
* [x] I ran `cargo fmt` and `cargo clippy` before committing
#### Bugfixes:
* [x] This pull request breaks the existing API
* [x] I've added tests to reproduce the issue which are now passing
* [ ] I'm linking the issue being fixed by this PR
ACKs for top commit:
danielabrozzoni:
re-ACK 235011f
Tree-SHA512: c9432956162fadfd255edf20b825635a487adb29c88d791e18f170da79a2aac6f8e745b5e5be09be3c211697d0b1f4bddc1da75c181e8f9fc4fddf566a7a3e5c
Copy file name to clipboardExpand all lines: src/types.rs
+52-4Lines changed: 52 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -51,14 +51,34 @@ impl AsRef<[u8]> for KeychainKind {
51
51
pubstructFeeRate(f32);
52
52
53
53
implFeeRate{
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
+
fnnew_checked(value:f32) -> Self{
60
+
assert!(value.is_normal() || value == 0.0);
61
+
assert!(value.is_sign_positive());
62
+
63
+
FeeRate(value)
64
+
}
65
+
54
66
/// 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.
55
71
pubfnfrom_btc_per_kvb(btc_per_kvb:f32) -> Self{
56
-
FeeRate(btc_per_kvb *1e5)
72
+
FeeRate::new_checked(btc_per_kvb *1e5)
57
73
}
58
74
59
75
/// Create a new instance of [`FeeRate`] given a float fee rate in satoshi/vbyte
/// 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
+
pubfnfrom_sat_per_vb(sat_per_vb:f32) -> Self{
81
+
FeeRate::new_checked(sat_per_vb)
62
82
}
63
83
64
84
/// Create a new [`FeeRate`] with the default min relay fee value
0 commit comments