Skip to content

Commit 74a0015

Browse files
committed
Add error OutOfRange
1 parent 882da67 commit 74a0015

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

units/src/amount/tests.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1466,8 +1466,8 @@ fn math_op_errors() {
14661466
fn to_effective_value() {
14671467
let amt = "1 cBTC".parse::<Amount>().unwrap();
14681468
let fee_rate = FeeRate::from_sat_per_kwu(10);
1469-
let effective_value =
1470-
amt.to_effective_value(fee_rate, Weight::from_wu(272)).unwrap();
1469+
let effective_value = amt.to_effective_value(
1470+
fee_rate, Weight::from_wu(272)).unwrap();
14711471

14721472
// 10 sat/kwu * 272 wu = 4 sats (rounding up)
14731473
let expected_fee = "3 sats".parse::<SignedAmount>().unwrap();
@@ -1476,7 +1476,7 @@ fn to_effective_value() {
14761476
}
14771477

14781478
#[test]
1479-
fn effective_value_fee_rate_does_not_overflow() {
1480-
let eff_value =Amount::ZERO.to_effective_value(FeeRate::MAX, Weight::from_wu(272));
1481-
assert!(eff_value.is_none());
1479+
fn to_effective_value_error() {
1480+
let eff_val = Amount::ZERO.to_effective_value(FeeRate::MAX, Weight::from_wu(272));
1481+
assert_eq!(eff_val, Err(OutOfRangeError::too_big(false).into()));
14821482
}

units/src/amount/unsigned.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use super::{
1616
OutOfRangeError, ParseAmountError, ParseError, SignedAmount,
1717
};
1818

19-
use crate::{FeeRate, Weight};
20-
2119
mod encapsulate {
2220
use super::OutOfRangeError;
2321

@@ -236,7 +234,7 @@ impl Amount {
236234
#[cfg(feature = "alloc")]
237235
pub fn to_btc(self) -> f64 { self.to_float_in(Denomination::Bitcoin) }
238236

239-
/// Computes the value of an output accounting for the cost of spending it.
237+
/// Computes the effective value of an [`Amount`].
240238
///
241239
/// The effective value is the value of an output value minus the amount to spend it. That is, the
242240
/// effective_value can be calculated as: value - (fee_rate * weight).
@@ -251,11 +249,17 @@ impl Amount {
251249
/// * `input_weight_prediction` - the predicted input weight.
252250
pub fn to_effective_value(
253251
&self,
254-
fee_rate: FeeRate,
255-
weight: Weight,
256-
) -> Option<SignedAmount> {
257-
let signed_input_fee = fee_rate.to_fee(weight)?.to_signed();
258-
self.to_signed().checked_sub(signed_input_fee)
252+
fee_rate: crate::FeeRate,
253+
weight: crate::Weight
254+
) -> Result<SignedAmount, OutOfRangeError> {
255+
let signed_input_fee = fee_rate.to_fee(weight).ok_or(
256+
OutOfRangeError { is_signed: false, is_greater_than_max: true }
257+
)?.to_signed();
258+
259+
// it's not possible to overflow when subtracted by the smallest givem:
260+
// Amount::MIN - SignedAmount::MAX = SignedAmount::MIN
261+
let eff_val = (self.to_signed() - signed_input_fee).unwrap(); //unwrap ok
262+
Ok(eff_val)
259263
}
260264

261265
/// Converts this [`Amount`] in floating-point notation in the given [`Denomination`].

0 commit comments

Comments
 (0)