Distinguish transient and permanent Electrum errors

This commit is contained in:
Daniel Karzel 2021-02-17 11:32:41 +11:00
parent 9b93cabfdf
commit ebb869e6f4

View File

@ -33,8 +33,8 @@ enum Error {
NotYetMined, NotYetMined,
#[error("Deserialization failed")] #[error("Deserialization failed")]
JsonDeserialization(reqwest::Error), JsonDeserialization(reqwest::Error),
#[error("Connecting to Electrum failed")] #[error("Electrum client error")]
ElectrumConnection(electrum_client::Error), ElectrumClient(electrum_client::Error),
} }
pub struct Wallet { pub struct Wallet {
@ -171,11 +171,15 @@ impl WatchForRawTransaction for Wallet {
tracing::debug!("watching for tx: {}", txid); tracing::debug!("watching for tx: {}", txid);
let tx = retry(ConstantBackoff::new(Duration::from_secs(1)), || async { let tx = retry(ConstantBackoff::new(Duration::from_secs(1)), || async {
let client = Client::new(self.rpc_url.as_ref()) let client = Client::new(self.rpc_url.as_ref())
.map_err(|err| backoff::Error::Permanent(Error::ElectrumConnection(err)))?; .map_err(|err| backoff::Error::Permanent(Error::ElectrumClient(err)))?;
let tx = client
.transaction_get(&txid) let tx = client.transaction_get(&txid).map_err(|err| match err {
.map_err(|_| backoff::Error::Transient(Error::NotYetMined))?; electrum_client::Error::Protocol(err) => {
tracing::debug!("found tx: {}", txid); tracing::debug!("Received protocol error {} from Electrum, retrying...", err);
backoff::Error::Transient(Error::NotYetMined)
}
err => backoff::Error::Permanent(Error::ElectrumClient(err)),
})?;
Result::<_, backoff::Error<Error>>::Ok(tx) Result::<_, backoff::Error<Error>>::Ok(tx)
}) })