Skip to content

Commit 052514e

Browse files
committed
Merge rust-bitcoin#4615: Remove reachable unreachable call in psbt
e7c90c5 Remove reachable unreachable call in psbt (Tobin C. Harding) c736e73 Add unwrap_or and unwrap_or_else to NumOpResult (Tobin C. Harding) Pull request description: A bunch of changes have been implemented lately in the fee calculation logic. As a result of this a at once time `unreachable` statement is now reachable - bad rust-bitcoin devs, no biscuit. Saturate to `FeeRate::MAX` when calculating the fee rate, check against the limit arg, and win. (Patch 1 adds the new combinators to `NumOpResult`. This was pulled out of rust-bitcoin#4610.) ACKs for top commit: apoelstra: ACK e7c90c5; successfully ran local tests Tree-SHA512: d33b3d5d4442fb17cae4c52880bc5dafdd611d686c6923744bc5f218761e8bff32fc5ab169af9f2ed2f02011516ed850ac8c6bd4ce1d6de40c0ac0b17486bbb4
2 parents 1e4f018 + e7c90c5 commit 052514e

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

bitcoin/src/psbt/mod.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use std::collections::{HashMap, HashSet};
2020

2121
use internals::write_err;
2222
use secp256k1::{Keypair, Message, Secp256k1, Signing, Verification};
23-
use units::NumOpResult;
2423

2524
use crate::bip32::{self, DerivationPath, KeySource, Xpriv, Xpub};
2625
use crate::crypto::key::{PrivateKey, PublicKey};
@@ -206,18 +205,12 @@ impl Psbt {
206205
// Note: Move prevents usage of &self from now on.
207206
let tx = self.internal_extract_tx();
208207

209-
// Now that the extracted Transaction is made, decide how to return it.
210-
match fee / tx.weight() {
211-
NumOpResult::Valid(fee_rate) => {
212-
// Prefer to return an AbsurdFeeRate error when both trigger.
213-
if fee_rate > max_fee_rate {
214-
return Err(ExtractTxError::AbsurdFeeRate { fee_rate, tx });
215-
}
216-
}
217-
NumOpResult::Error(_) => unreachable!("weight() is always non-zero"),
208+
let fee_rate = (fee / tx.weight()).unwrap_or(FeeRate::MAX);
209+
if fee_rate > max_fee_rate {
210+
Err(ExtractTxError::AbsurdFeeRate { fee_rate, tx })
211+
} else {
212+
Ok(tx)
218213
}
219-
220-
Ok(tx)
221214
}
222215

223216
/// Combines this [`Psbt`] with `other` PSBT as described by BIP 174.

units/src/result.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,32 @@ impl<T: fmt::Debug> NumOpResult<T> {
142142
}
143143
}
144144

145+
/// Returns the contained Some value or a provided default.
146+
///
147+
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing the result of a
148+
/// function call, it is recommended to use `unwrap_or_else`, which is lazily evaluated.
149+
#[inline]
150+
#[track_caller]
151+
pub fn unwrap_or(self, default: T) -> T {
152+
match self {
153+
R::Valid(x) => x,
154+
R::Error(_) => default,
155+
}
156+
}
157+
158+
/// Returns the contained `Some` value or computes it from a closure.
159+
#[inline]
160+
#[track_caller]
161+
pub fn unwrap_or_else<F>(self, f: F) -> T
162+
where
163+
F: FnOnce() -> T,
164+
{
165+
match self {
166+
R::Valid(x) => x,
167+
R::Error(_) => f(),
168+
}
169+
}
170+
145171
/// Converts this `NumOpResult` to an `Option<T>`.
146172
#[inline]
147173
pub fn ok(self) -> Option<T> {

0 commit comments

Comments
 (0)