Skip to content

Commit a8610a9

Browse files
committed
Inline checked mul / to fee back into fee_rate module
A while back we move all the 'fee' stuff into a separate module because I thought it would help with clarity - I was wrong. Move the checked mul and to fee functions back into the `fee_rate` module on the main `FeeRate` impl block. Internal change only - code move.
1 parent e17c391 commit a8610a9

File tree

2 files changed

+57
-59
lines changed

2 files changed

+57
-59
lines changed

units/src/fee.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,6 @@ use NumOpResult as R;
1717

1818
use crate::{Amount, FeeRate, MathOp, NumOpError as E, NumOpResult, OptionExt, Weight};
1919

20-
impl FeeRate {
21-
/// Calculates the fee by multiplying this fee rate by weight.
22-
///
23-
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate. When the resulting
24-
/// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is
25-
/// enough instead of falling short if rounded down.
26-
///
27-
/// If the calculation would overflow we saturate to [`Amount::MAX`]. Since such a fee can never
28-
/// be paid this is meaningful as an error case while still removing the possibility of silently
29-
/// wrapping.
30-
pub const fn to_fee(self, weight: Weight) -> Amount {
31-
// No `unwrap_or()` in const context.
32-
match self.checked_mul_by_weight(weight) {
33-
Some(fee) => fee,
34-
None => Amount::MAX,
35-
}
36-
}
37-
38-
/// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`]
39-
/// if an overflow occurred.
40-
///
41-
/// This is equivalent to `Self::checked_mul_by_weight()`.
42-
#[must_use]
43-
#[deprecated(since = "TBD", note = "use `to_fee()` instead")]
44-
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.checked_mul_by_weight(weight) }
45-
46-
/// Calculates the fee by multiplying this fee rate by weight, in virtual bytes, returning [`None`]
47-
/// if an overflow occurred.
48-
///
49-
/// This is equivalent to converting `vb` to [`Weight`] using [`Weight::from_vb`] and then calling
50-
/// `Self::fee_wu(weight)`.
51-
#[must_use]
52-
#[deprecated(since = "TBD", note = "use Weight::from_vb and then `to_fee()` instead")]
53-
pub fn fee_vb(self, vb: u64) -> Option<Amount> { Weight::from_vb(vb).map(|w| self.to_fee(w)) }
54-
55-
/// Checked weight multiplication.
56-
///
57-
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate. When the resulting
58-
/// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is
59-
/// enough instead of falling short if rounded down.
60-
///
61-
/// Returns [`None`] if overflow occurred.
62-
#[must_use]
63-
pub const fn checked_mul_by_weight(self, weight: Weight) -> Option<Amount> {
64-
let wu = weight.to_wu();
65-
if let Some(fee_kwu) = self.to_sat_per_kwu_floor().checked_mul(wu) {
66-
// Bump by 999 to do ceil division using kwu.
67-
if let Some(bump) = fee_kwu.checked_add(999) {
68-
let fee = bump / 1_000;
69-
if let Ok(fee_amount) = Amount::from_sat(fee) {
70-
return Some(fee_amount);
71-
}
72-
}
73-
}
74-
None
75-
}
76-
}
77-
7820
crate::internal_macros::impl_op_for_references! {
7921
impl ops::Mul<FeeRate> for Weight {
8022
type Output = NumOpResult<Amount>;

units/src/fee_rate/mod.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use arbitrary::{Arbitrary, Unstructured};
1313

1414
use NumOpResult as R;
1515

16-
use crate::{Amount,MathOp, NumOpError as E, NumOpResult};
16+
use crate::{Amount, MathOp, NumOpError as E, NumOpResult, Weight};
1717

1818
mod encapsulate {
1919
/// Fee rate.
@@ -184,6 +184,62 @@ impl FeeRate {
184184
None => None,
185185
}
186186
}
187+
188+
/// Calculates the fee by multiplying this fee rate by weight.
189+
///
190+
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate. When the resulting
191+
/// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is
192+
/// enough instead of falling short if rounded down.
193+
///
194+
/// If the calculation would overflow we saturate to [`Amount::MAX`]. Since such a fee can never
195+
/// be paid this is meaningful as an error case while still removing the possibility of silently
196+
/// wrapping.
197+
pub const fn to_fee(self, weight: Weight) -> Amount {
198+
// No `unwrap_or()` in const context.
199+
match self.checked_mul_by_weight(weight) {
200+
Some(fee) => fee,
201+
None => Amount::MAX,
202+
}
203+
}
204+
205+
/// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`]
206+
/// if an overflow occurred.
207+
///
208+
/// This is equivalent to `Self::checked_mul_by_weight()`.
209+
#[must_use]
210+
#[deprecated(since = "TBD", note = "use `to_fee()` instead")]
211+
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.checked_mul_by_weight(weight) }
212+
213+
/// Calculates the fee by multiplying this fee rate by weight, in virtual bytes, returning [`None`]
214+
/// if an overflow occurred.
215+
///
216+
/// This is equivalent to converting `vb` to [`Weight`] using [`Weight::from_vb`] and then calling
217+
/// `Self::fee_wu(weight)`.
218+
#[must_use]
219+
#[deprecated(since = "TBD", note = "use Weight::from_vb and then `to_fee()` instead")]
220+
pub fn fee_vb(self, vb: u64) -> Option<Amount> { Weight::from_vb(vb).map(|w| self.to_fee(w)) }
221+
222+
/// Checked weight multiplication.
223+
///
224+
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate. When the resulting
225+
/// fee is a non-integer amount, the amount is rounded up, ensuring that the transaction fee is
226+
/// enough instead of falling short if rounded down.
227+
///
228+
/// Returns [`None`] if overflow occurred.
229+
#[must_use]
230+
pub const fn checked_mul_by_weight(self, weight: Weight) -> Option<Amount> {
231+
let wu = weight.to_wu();
232+
if let Some(fee_kwu) = self.to_sat_per_kwu_floor().checked_mul(wu) {
233+
// Bump by 999 to do ceil division using kwu.
234+
if let Some(bump) = fee_kwu.checked_add(999) {
235+
let fee = bump / 1_000;
236+
if let Ok(fee_amount) = Amount::from_sat(fee) {
237+
return Some(fee_amount);
238+
}
239+
}
240+
}
241+
None
242+
}
187243
}
188244

189245
crate::internal_macros::impl_op_for_references! {

0 commit comments

Comments
 (0)