Distinguish permanent and transient error when watching tx

This commit is contained in:
Daniel Karzel 2021-02-16 15:19:11 +11:00
parent b66bb00c77
commit 77ec7e502f
2 changed files with 13 additions and 6 deletions

View File

@ -29,6 +29,7 @@ enum Error {
Parse(std::num::ParseIntError),
NotYetMined,
JsonDeserialisation(reqwest::Error),
ElectrumConnection(electrum_client::Error),
}
pub struct Wallet {
@ -163,14 +164,20 @@ impl BroadcastSignedTransaction for Wallet {
impl WatchForRawTransaction for Wallet {
async fn watch_for_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
tracing::debug!("watching for tx: {}", txid);
retry(ConstantBackoff::new(Duration::from_secs(1)), || async {
let client = Client::new(self.rpc_url.as_ref())?;
let tx = client.transaction_get(&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);
Ok(tx)
Result::<_, backoff::Error<Error>>::Ok(tx)
})
.await
.map_err(|err| anyhow!("transient errors to be retried: {:?}", err))
.map_err(|err| anyhow!("transient errors to be retried: {:?}", err))?;
Ok(tx)
}
}

View File

@ -86,7 +86,7 @@ async fn run_until_internal(
swap_id: Uuid,
db: Arc<Database>,
) -> Result<AliceState> {
info!("Current state:{}", state);
info!("Current state: {}", state);
if is_target_state(&state) {
Ok(state)
} else {