Skip to content

Commit 3b0286b

Browse files
committed
Return NumOpResult for FeeRate and Weight
As we did for `Amount` change the fee functions on `FeeRate` and `Weight` to return `NumOpResult`. This change does not add that much value but it does not make things worse and it makes the API more uniform. Also reduces lines of code.
1 parent 15065b7 commit 3b0286b

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

units/src/fee.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ use core::ops;
2727

2828
use NumOpResult as R;
2929

30-
use crate::{Amount, FeeRate, MathOp, NumOpError as E, NumOpResult, Weight};
30+
use crate::{Amount, FeeRate, NumOpResult, Weight};
3131

3232
crate::internal_macros::impl_op_for_references! {
3333
impl ops::Mul<FeeRate> for Weight {
3434
type Output = NumOpResult<Amount>;
35-
fn mul(self, rhs: FeeRate) -> Self::Output {
36-
match rhs.mul_by_weight(self) {
37-
Some(amount) => R::Valid(amount),
38-
None => R::Error(E::while_doing(MathOp::Mul)),
39-
}
40-
}
35+
fn mul(self, rhs: FeeRate) -> Self::Output { rhs.mul_by_weight(self) }
4136
}
4237
impl ops::Mul<FeeRate> for NumOpResult<Weight> {
4338
type Output = NumOpResult<Amount>;
@@ -72,12 +67,7 @@ crate::internal_macros::impl_op_for_references! {
7267

7368
impl ops::Mul<Weight> for FeeRate {
7469
type Output = NumOpResult<Amount>;
75-
fn mul(self, rhs: Weight) -> Self::Output {
76-
match self.mul_by_weight(rhs) {
77-
Some(amount) => R::Valid(amount),
78-
None => R::Error(E::while_doing(MathOp::Mul)),
79-
}
80-
}
70+
fn mul(self, rhs: Weight) -> Self::Output { self.mul_by_weight(rhs) }
8171
}
8272
impl ops::Mul<Weight> for NumOpResult<FeeRate> {
8373
type Output = NumOpResult<Amount>;
@@ -218,7 +208,7 @@ mod tests {
218208
assert_eq!(Amount::from_sat_u32(100), fee);
219209

220210
let fee = FeeRate::from_sat_per_kwu(10).unwrap().mul_by_weight(Weight::MAX);
221-
assert!(fee.is_none());
211+
assert!(fee.is_error());
222212

223213
let weight = Weight::from_vb(3).unwrap();
224214
let fee_rate = FeeRate::from_sat_per_vb(3).unwrap();

units/src/fee_rate/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ impl FeeRate {
196196
pub const fn to_fee(self, weight: Weight) -> Amount {
197197
// No `unwrap_or()` in const context.
198198
match self.mul_by_weight(weight) {
199-
Some(fee) => fee,
200-
None => Amount::MAX,
199+
NumOpResult::Valid(fee) => fee,
200+
NumOpResult::Error(_) => Amount::MAX,
201201
}
202202
}
203203

@@ -207,7 +207,7 @@ impl FeeRate {
207207
/// This is equivalent to `Self::checked_mul_by_weight()`.
208208
#[must_use]
209209
#[deprecated(since = "TBD", note = "use `to_fee()` instead")]
210-
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.mul_by_weight(weight) }
210+
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.mul_by_weight(weight).ok() }
211211

212212
/// Calculates the fee by multiplying this fee rate by weight, in virtual bytes, returning [`None`]
213213
/// if an overflow occurred.
@@ -223,21 +223,18 @@ impl FeeRate {
223223
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate. When the resulting
224224
/// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is
225225
/// enough instead of falling short if rounded down.
226-
///
227-
/// Returns [`None`] if overflow occurred.
228-
#[must_use]
229-
pub const fn mul_by_weight(self, weight: Weight) -> Option<Amount> {
226+
pub const fn mul_by_weight(self, weight: Weight) -> NumOpResult<Amount> {
230227
let wu = weight.to_wu();
231228
if let Some(fee_kwu) = self.to_sat_per_kwu_floor().checked_mul(wu) {
232229
// Bump by 999 to do ceil division using kwu.
233230
if let Some(bump) = fee_kwu.checked_add(999) {
234231
let fee = bump / 1_000;
235232
if let Ok(fee_amount) = Amount::from_sat(fee) {
236-
return Some(fee_amount);
233+
return NumOpResult::Valid(fee_amount);
237234
}
238235
}
239236
}
240-
None
237+
NumOpResult::Error(E::while_doing(MathOp::Mul))
241238
}
242239
}
243240

units/src/weight.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use arbitrary::{Arbitrary, Unstructured};
1010
#[cfg(feature = "serde")]
1111
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1212

13-
use crate::{Amount, CheckedSum, FeeRate};
13+
use crate::{Amount, CheckedSum, FeeRate, NumOpResult};
1414

1515
/// The factor that non-witness serialization data is multiplied by during weight calculation.
1616
pub const WITNESS_SCALE_FACTOR: usize = 4;
@@ -168,10 +168,7 @@ impl Weight {
168168
/// Computes the absolute fee amount for a given [`FeeRate`] at this weight. When the resulting
169169
/// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is
170170
/// enough instead of falling short if rounded down.
171-
///
172-
/// Returns [`None`] if overflow occurred.
173-
#[must_use]
174-
pub const fn mul_by_fee_rate(self, fee_rate: FeeRate) -> Option<Amount> {
171+
pub const fn mul_by_fee_rate(self, fee_rate: FeeRate) -> NumOpResult<Amount> {
175172
fee_rate.mul_by_weight(self)
176173
}
177174
}

0 commit comments

Comments
 (0)