Skip to content

Commit

Permalink
feat: add new error variant, remove unwrap usage
Browse files Browse the repository at this point in the history
  • Loading branch information
oleonardolima committed Jun 13, 2024
1 parent 1e980c4 commit 51e6f6d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
13 changes: 13 additions & 0 deletions crates/wallet/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<miniscript::Error> for BuildFeeBumpError {
fn from(v: miniscript::Error) -> Self {
Self::MiniscriptError(v)
}
}

impl fmt::Display for BuildFeeBumpError {
Expand All @@ -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
),
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 14 additions & 2 deletions crates/wallet/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<miniscript::Error> for AddUtxoError {
fn from(v: miniscript::Error) -> Self {
Self::MiniscriptError(v)
}
}

impl fmt::Display for AddUtxoError {
Expand All @@ -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
),
}
}
}
Expand Down

0 comments on commit 51e6f6d

Please sign in to comment.