Skip to content

Commit e17c391

Browse files
committed
Inline checked div functions back into unsigned 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 div functions back into the `unsigned` module on the main `Amount` impl block. Internal change only - code move.
1 parent aab9c2d commit e17c391

File tree

2 files changed

+101
-102
lines changed

2 files changed

+101
-102
lines changed

units/src/amount/unsigned.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::{
1515
parse_signed_to_satoshi, split_amount_and_denomination, Denomination, Display, DisplayStyle,
1616
OutOfRangeError, ParseAmountError, ParseError, SignedAmount,
1717
};
18+
use crate::{FeeRate, Weight};
1819

1920
mod encapsulate {
2021
use super::OutOfRangeError;
@@ -402,6 +403,106 @@ impl Amount {
402403
SignedAmount::from_sat(self.to_sat() as i64) // Cast ok, signed amount and amount share positive range.
403404
.expect("range of Amount is within range of SignedAmount")
404405
}
406+
407+
/// Checked weight floor division.
408+
///
409+
/// Be aware that integer division loses the remainder if no exact division
410+
/// can be made. See also [`Self::checked_div_by_weight_ceil`].
411+
///
412+
/// Returns [`None`] if overflow occurred.
413+
#[must_use]
414+
pub const fn checked_div_by_weight_floor(self, weight: Weight) -> Option<FeeRate> {
415+
let wu = weight.to_wu();
416+
if wu == 0 {
417+
return None;
418+
}
419+
420+
// Mul by 1,000 because we use per/kwu.
421+
match self.to_sat().checked_mul(1_000) {
422+
Some(sats) => {
423+
let fee_rate = sats / wu;
424+
FeeRate::from_sat_per_kwu(fee_rate)
425+
}
426+
None => None,
427+
}
428+
}
429+
430+
/// Checked weight ceiling division.
431+
///
432+
/// Be aware that integer division loses the remainder if no exact division
433+
/// can be made. This method rounds up ensuring the transaction fee rate is
434+
/// sufficient. See also [`Self::checked_div_by_weight_floor`].
435+
///
436+
/// Returns [`None`] if overflow occurred.
437+
///
438+
/// # Examples
439+
///
440+
/// ```
441+
/// # use bitcoin_units::{amount, Amount, FeeRate, Weight};
442+
/// let amount = Amount::from_sat(10)?;
443+
/// let weight = Weight::from_wu(300);
444+
/// let fee_rate = amount.checked_div_by_weight_ceil(weight);
445+
/// assert_eq!(fee_rate, FeeRate::from_sat_per_kwu(34));
446+
/// # Ok::<_, amount::OutOfRangeError>(())
447+
/// ```
448+
#[must_use]
449+
pub const fn checked_div_by_weight_ceil(self, weight: Weight) -> Option<FeeRate> {
450+
let wu = weight.to_wu();
451+
if wu == 0 {
452+
return None;
453+
}
454+
455+
// Mul by 1,000 because we use per/kwu.
456+
if let Some(sats) = self.to_sat().checked_mul(1_000) {
457+
// No need to used checked arithmetic because wu is non-zero.
458+
if let Some(bump) = sats.checked_add(wu - 1) {
459+
let fee_rate = bump / wu;
460+
return FeeRate::from_sat_per_kwu(fee_rate);
461+
}
462+
}
463+
None
464+
}
465+
466+
/// Checked fee rate floor division.
467+
///
468+
/// Computes the maximum weight that would result in a fee less than or equal to this amount
469+
/// at the given `fee_rate`. Uses floor division to ensure the resulting weight doesn't cause
470+
/// the fee to exceed the amount.
471+
///
472+
/// Returns [`None`] if overflow occurred or if `fee_rate` is zero.
473+
#[must_use]
474+
pub const fn checked_div_by_fee_rate_floor(self, fee_rate: FeeRate) -> Option<Weight> {
475+
if let Some(msats) = self.to_sat().checked_mul(1000) {
476+
if let Some(wu) = msats.checked_div(fee_rate.to_sat_per_kwu_ceil()) {
477+
return Some(Weight::from_wu(wu));
478+
}
479+
}
480+
None
481+
}
482+
483+
/// Checked fee rate ceiling division.
484+
///
485+
/// Computes the minimum weight that would result in a fee greater than or equal to this amount
486+
/// at the given `fee_rate`. Uses ceiling division to ensure the resulting weight is sufficient.
487+
///
488+
/// Returns [`None`] if overflow occurred or if `fee_rate` is zero.
489+
#[must_use]
490+
pub const fn checked_div_by_fee_rate_ceil(self, fee_rate: FeeRate) -> Option<Weight> {
491+
// Use ceil because result is used as the divisor.
492+
let rate = fee_rate.to_sat_per_kwu_ceil();
493+
if rate == 0 {
494+
return None;
495+
}
496+
497+
if let Some(msats) = self.to_sat().checked_mul(1000) {
498+
// No need to used checked arithmetic because rate is non-zero.
499+
if let Some(bump) = msats.checked_add(rate - 1) {
500+
let wu = bump / rate;
501+
return Some(Weight::from_wu(wu));
502+
}
503+
}
504+
None
505+
}
405506
}
406507

407508
impl default::Default for Amount {

units/src/fee.rs

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

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

20-
impl Amount {
21-
/// Checked weight floor division.
22-
///
23-
/// Be aware that integer division loses the remainder if no exact division
24-
/// can be made. See also [`Self::checked_div_by_weight_ceil`].
25-
///
26-
/// Returns [`None`] if overflow occurred.
27-
#[must_use]
28-
pub const fn checked_div_by_weight_floor(self, weight: Weight) -> Option<FeeRate> {
29-
let wu = weight.to_wu();
30-
if wu == 0 {
31-
return None;
32-
}
33-
34-
// Mul by 1,000 because we use per/kwu.
35-
match self.to_sat().checked_mul(1_000) {
36-
Some(sats) => {
37-
let fee_rate = sats / wu;
38-
FeeRate::from_sat_per_kwu(fee_rate)
39-
}
40-
None => None,
41-
}
42-
}
43-
44-
/// Checked weight ceiling division.
45-
///
46-
/// Be aware that integer division loses the remainder if no exact division
47-
/// can be made. This method rounds up ensuring the transaction fee rate is
48-
/// sufficient. See also [`Self::checked_div_by_weight_floor`].
49-
///
50-
/// Returns [`None`] if overflow occurred.
51-
///
52-
/// # Examples
53-
///
54-
/// ```
55-
/// # use bitcoin_units::{amount, Amount, FeeRate, Weight};
56-
/// let amount = Amount::from_sat(10)?;
57-
/// let weight = Weight::from_wu(300);
58-
/// let fee_rate = amount.checked_div_by_weight_ceil(weight);
59-
/// assert_eq!(fee_rate, FeeRate::from_sat_per_kwu(34));
60-
/// # Ok::<_, amount::OutOfRangeError>(())
61-
/// ```
62-
#[must_use]
63-
pub const fn checked_div_by_weight_ceil(self, weight: Weight) -> Option<FeeRate> {
64-
let wu = weight.to_wu();
65-
if wu == 0 {
66-
return None;
67-
}
68-
69-
// Mul by 1,000 because we use per/kwu.
70-
if let Some(sats) = self.to_sat().checked_mul(1_000) {
71-
// No need to used checked arithmetic because wu is non-zero.
72-
if let Some(bump) = sats.checked_add(wu - 1) {
73-
let fee_rate = bump / wu;
74-
return FeeRate::from_sat_per_kwu(fee_rate);
75-
}
76-
}
77-
None
78-
}
79-
80-
/// Checked fee rate floor division.
81-
///
82-
/// Computes the maximum weight that would result in a fee less than or equal to this amount
83-
/// at the given `fee_rate`. Uses floor division to ensure the resulting weight doesn't cause
84-
/// the fee to exceed the amount.
85-
///
86-
/// Returns [`None`] if overflow occurred or if `fee_rate` is zero.
87-
#[must_use]
88-
pub const fn checked_div_by_fee_rate_floor(self, fee_rate: FeeRate) -> Option<Weight> {
89-
if let Some(msats) = self.to_sat().checked_mul(1000) {
90-
if let Some(wu) = msats.checked_div(fee_rate.to_sat_per_kwu_ceil()) {
91-
return Some(Weight::from_wu(wu));
92-
}
93-
}
94-
None
95-
}
96-
97-
/// Checked fee rate ceiling division.
98-
///
99-
/// Computes the minimum weight that would result in a fee greater than or equal to this amount
100-
/// at the given `fee_rate`. Uses ceiling division to ensure the resulting weight is sufficient.
101-
///
102-
/// Returns [`None`] if overflow occurred or if `fee_rate` is zero.
103-
#[must_use]
104-
pub const fn checked_div_by_fee_rate_ceil(self, fee_rate: FeeRate) -> Option<Weight> {
105-
// Use ceil because result is used as the divisor.
106-
let rate = fee_rate.to_sat_per_kwu_ceil();
107-
if rate == 0 {
108-
return None;
109-
}
110-
111-
if let Some(msats) = self.to_sat().checked_mul(1000) {
112-
// No need to used checked arithmetic because rate is non-zero.
113-
if let Some(bump) = msats.checked_add(rate - 1) {
114-
let wu = bump / rate;
115-
return Some(Weight::from_wu(wu));
116-
}
117-
}
118-
None
119-
}
120-
}
121-
12220
impl FeeRate {
12321
/// Calculates the fee by multiplying this fee rate by weight.
12422
///

0 commit comments

Comments
 (0)