Skip to content

Commit

Permalink
fix: timeout for tx confirmation (#2098)
Browse files Browse the repository at this point in the history
  • Loading branch information
m30m authored Nov 6, 2024
1 parent ed0f273 commit e2ef7a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/fortuna/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/fortuna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fortuna"
version = "6.5.4"
version = "6.5.5"
edition = "2021"

[dependencies]
Expand Down
32 changes: 27 additions & 5 deletions apps/fortuna/src/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use {
},
time::{
self,
timeout,
Duration,
},
},
Expand Down Expand Up @@ -421,6 +422,8 @@ pub async fn process_event_with_backoff(
}


const TX_CONFIRMATION_TIMEOUT_SECS: u64 = 30;

/// Process a callback on a chain. It estimates the gas for the reveal with callback and
/// submits the transaction if the gas estimate is below the gas limit.
/// It will return a permanent or transient error depending on the error type and whether
Expand Down Expand Up @@ -501,8 +504,28 @@ pub async fn process_event(
))
})?;

let receipt = pending_tx
.await
let reset_nonce = || {
let nonce_manager = contract.client_ref().inner().inner();
nonce_manager.reset();
};

let pending_receipt = timeout(
Duration::from_secs(TX_CONFIRMATION_TIMEOUT_SECS),
pending_tx,
)
.await
.map_err(|_| {
// Tx can get stuck in mempool without any progress if the nonce is too high
// in this case ethers internal polling will not reduce the number of retries
// and keep retrying indefinitely. So we set a manual timeout here and reset the nonce.
reset_nonce();
backoff::Error::transient(anyhow!(
"Tx stuck in mempool. Resetting nonce. Tx:{:?}",
transaction
))
})?;

let receipt = pending_receipt
.map_err(|e| {
backoff::Error::transient(anyhow!(
"Error waiting for transaction receipt. Tx:{:?} Error:{:?}",
Expand All @@ -513,10 +536,9 @@ pub async fn process_event(
.ok_or_else(|| {
// RPC may not return an error on tx submission if the nonce is too high.
// But we will never get a receipt. So we reset the nonce manager to get the correct nonce.
let nonce_manager = contract.client_ref().inner().inner();
nonce_manager.reset();
reset_nonce();
backoff::Error::transient(anyhow!(
"Can't verify the reveal, probably dropped from mempool Tx:{:?}",
"Can't verify the reveal, probably dropped from mempool. Resetting nonce. Tx:{:?}",
transaction
))
})?;
Expand Down

0 comments on commit e2ef7a4

Please sign in to comment.