From 51e6f6dbe91b80c7239c5367e6c654a749678962 Mon Sep 17 00:00:00 2001 From: Leonardo Lima Date: Tue, 11 Jun 2024 23:13:12 -0300 Subject: [PATCH] feat: add new error variant, remove unwrap usage --- crates/wallet/src/wallet/error.rs | 13 +++++++++++++ crates/wallet/src/wallet/mod.rs | 6 ++---- crates/wallet/src/wallet/tx_builder.rs | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/crates/wallet/src/wallet/error.rs b/crates/wallet/src/wallet/error.rs index 7b19a2ec5b..8adb069025 100644 --- a/crates/wallet/src/wallet/error.rs +++ b/crates/wallet/src/wallet/error.rs @@ -237,6 +237,14 @@ pub enum BuildFeeBumpError { IrreplaceableTransaction(Txid), /// Node doesn't have data to estimate a fee rate FeeRateUnavailable, + /// Happens when the descriptor is impossible to satisfy + MiniscriptError(miniscript::Error), +} + +impl From for BuildFeeBumpError { + fn from(v: miniscript::Error) -> Self { + Self::MiniscriptError(v) + } } impl fmt::Display for BuildFeeBumpError { @@ -261,6 +269,11 @@ impl fmt::Display for BuildFeeBumpError { write!(f, "Transaction can't be replaced with txid: {}", txid) } Self::FeeRateUnavailable => write!(f, "Fee rate unavailable"), + Self::MiniscriptError(error) => write!( + f, + "It's not possible to satisfy the descriptor, miniscript error: {}", + error + ), } } } diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index 32cfa7bfe8..bedb7cbf47 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -1703,11 +1703,9 @@ impl Wallet { let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) { Some((keychain, derivation_index)) => { - // TODO: (@leonardo) remove unwrap() use here, use expect or proper error! let satisfaction_weight = self .get_descriptor_for_keychain(keychain) - .max_weight_to_satisfy() - .unwrap(); + .max_weight_to_satisfy()?; WeightedUtxo { utxo: Utxo::Local(LocalOutput { outpoint: txin.previous_output, @@ -2040,7 +2038,7 @@ impl Wallet { (utxo, { self.get_descriptor_for_keychain(keychain) .max_weight_to_satisfy() - .unwrap() // TODO: (@leonardo) remove unwrap() use here, use expect or proper error! + .unwrap() }) }) .collect() diff --git a/crates/wallet/src/wallet/tx_builder.rs b/crates/wallet/src/wallet/tx_builder.rs index 8f8b99dc5f..86b1478bdf 100644 --- a/crates/wallet/src/wallet/tx_builder.rs +++ b/crates/wallet/src/wallet/tx_builder.rs @@ -297,8 +297,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> { for utxo in utxos { let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain); - // TODO: (@leonardo) remove unwrap() use here, use expect or proper error! - let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap(); + let satisfaction_weight = descriptor.max_weight_to_satisfy()?; self.params.utxos.push(WeightedUtxo { satisfaction_weight, utxo: Utxo::Local(utxo), @@ -688,6 +687,14 @@ impl<'a, Cs: CoinSelectionAlgorithm> TxBuilder<'a, Cs> { pub enum AddUtxoError { /// Happens when trying to spend an UTXO that is not in the internal database UnknownUtxo(OutPoint), + /// Happens when the descriptor is impossible to satisfy + MiniscriptError(miniscript::Error), +} + +impl From for AddUtxoError { + fn from(v: miniscript::Error) -> Self { + Self::MiniscriptError(v) + } } impl fmt::Display for AddUtxoError { @@ -698,6 +705,11 @@ impl fmt::Display for AddUtxoError { "UTXO not found in the internal database for txid: {} with vout: {}", outpoint.txid, outpoint.vout ), + Self::MiniscriptError(error) => write!( + f, + "It's not possible to satisfy the descriptor, miniscript error: {}", + error + ), } } }