From ebb869e6f4a83b661519de7084015339467544ee Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Wed, 17 Feb 2021 11:32:41 +1100 Subject: [PATCH] Distinguish transient and permanent Electrum errors --- swap/src/bitcoin/wallet.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 1dfbb320..774b473d 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -33,8 +33,8 @@ enum Error { NotYetMined, #[error("Deserialization failed")] JsonDeserialization(reqwest::Error), - #[error("Connecting to Electrum failed")] - ElectrumConnection(electrum_client::Error), + #[error("Electrum client error")] + ElectrumClient(electrum_client::Error), } pub struct Wallet { @@ -171,11 +171,15 @@ impl WatchForRawTransaction for Wallet { tracing::debug!("watching for tx: {}", txid); let tx = retry(ConstantBackoff::new(Duration::from_secs(1)), || async { let client = Client::new(self.rpc_url.as_ref()) - .map_err(|err| backoff::Error::Permanent(Error::ElectrumConnection(err)))?; - let tx = client - .transaction_get(&txid) - .map_err(|_| backoff::Error::Transient(Error::NotYetMined))?; - tracing::debug!("found tx: {}", txid); + .map_err(|err| backoff::Error::Permanent(Error::ElectrumClient(err)))?; + + let tx = client.transaction_get(&txid).map_err(|err| match err { + electrum_client::Error::Protocol(err) => { + tracing::debug!("Received protocol error {} from Electrum, retrying...", err); + backoff::Error::Transient(Error::NotYetMined) + } + err => backoff::Error::Permanent(Error::ElectrumClient(err)), + })?; Result::<_, backoff::Error>::Ok(tx) })