Browse Source

Improve readability of special handling for electrum error

The default path through the function would be to propagate every
error. However, we want to inspect whether it is one very specific
error.

Make this clear in the code structure by having a dedicated `if let`
for the special case but keep using `?` afterwards.
fix-olivia-event-id
Thomas Eizinger 3 years ago
parent
commit
cf5a3752d3
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 33
      daemon/src/wallet.rs

33
daemon/src/wallet.rs

@ -1,11 +1,11 @@
use crate::model::WalletInfo; use crate::model::WalletInfo;
use anyhow::{anyhow, Context, Result}; use anyhow::{Context, Result};
use bdk::bitcoin::util::bip32::ExtendedPrivKey; use bdk::bitcoin::util::bip32::ExtendedPrivKey;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction; use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{Amount, PublicKey, Transaction, Txid}; use bdk::bitcoin::{Amount, PublicKey, Transaction, Txid};
use bdk::blockchain::{ElectrumBlockchain, NoopProgress}; use bdk::blockchain::{ElectrumBlockchain, NoopProgress};
use bdk::wallet::AddressIndex; use bdk::wallet::AddressIndex;
use bdk::{electrum_client, Error, KeychainKind, SignOptions}; use bdk::{electrum_client, KeychainKind, SignOptions};
use cfd_protocol::{PartyParams, WalletExt}; use cfd_protocol::{PartyParams, WalletExt};
use rocket::serde::json::Value; use rocket::serde::json::Value;
use std::path::Path; use std::path::Path;
@ -98,29 +98,24 @@ impl Wallet {
let wallet = self.wallet.lock().await; let wallet = self.wallet.lock().await;
// TODO: Optimize this match to be a map_err / more readable in general // TODO: Optimize this match to be a map_err / more readable in general
let txid = tx.txid(); let txid = tx.txid();
match wallet.broadcast(tx) {
Ok(txid) => Ok(txid), let result = wallet.broadcast(tx);
Err(e) => {
if let Error::Electrum(electrum_client::Error::Protocol(ref value)) = e { if let Err(&bdk::Error::Electrum(electrum_client::Error::Protocol(ref value))) =
let error_code = match parse_rpc_protocol_error_code(value) { result.as_ref()
Ok(error_code) => error_code, {
Err(inner) => { let error_code = parse_rpc_protocol_error_code(value).with_context(|| {
tracing::error!( format!("Failed to parse electrum error response '{:?}'", value)
"Failed to parse error code from RPC message: {}", })?;
inner
);
return Err(anyhow!(e));
}
};
if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) { if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
return Ok(txid); return Ok(txid);
} }
} }
Err(anyhow!(e)) let txid = result?;
}
} Ok(txid)
} }
} }

Loading…
Cancel
Save