Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if racing on a pending tx in infura, backoff #157

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions chain-events/src/block_events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::sync::Arc;
use std::{sync::Arc, time::Duration};

use ethers::{
abi::{AbiDecode, Address, RawLog},
contract::EthEvent,
prelude::EthLogDecode,
providers::{Middleware, Provider, PubsubClient, Ws},
types::{BlockNumber, Filter, Log, ValueOrArray},
types::{BlockNumber, Filter, Log, Transaction, ValueOrArray, H256},
};
use futures::{Sink, SinkExt, StreamExt};

Expand All @@ -22,6 +22,9 @@ use ethers_log_decode::EthLogDecode;

use crate::{Error, Result, RECONNECT_BACKOFF};

const PENDING_TX_RETRY: usize = 5;
const PENDING_TX_RETRY_BACKOFF: Duration = Duration::from_secs(3);

#[derive(EthLogDecode)]
enum L1Events {
BlockCommit(BlockCommitFilter),
Expand Down Expand Up @@ -223,6 +226,27 @@ impl BlockEvents {
}
}

async fn get_tx_with_retries<M>(
middleware: &M,
tx_hash: H256,
retries: usize,
) -> Result<Option<Transaction>>
where
M: Middleware,
{
for _ in 0..retries {
if let Ok(Some(tx)) = middleware.get_transaction(tx_hash).await {
if tx.block_number.is_some() {
return Ok(Some(tx));
}
}

tokio::time::sleep(PENDING_TX_RETRY_BACKOFF).await;
}

Ok(None)
}

async fn process_l1_event<M, S>(
l2_erc20_bridge_addr: Address,
log: &Log,
Expand All @@ -241,15 +265,17 @@ async fn process_l1_event<M, S>(

match l1_event {
L1Events::BlockCommit(bc) => {
let Ok(tx) = middleware
.get_transaction(log.transaction_hash.unwrap_or_else(|| {
let Ok(tx) = get_tx_with_retries(
&middleware,
log.transaction_hash.unwrap_or_else(|| {
panic!("log always has a related transaction {:?}; qed", log)
}))
.await
}),
PENDING_TX_RETRY,
)
.await
else {
return;
};

let tx = tx.unwrap_or_else(|| {
panic!("mined transaction exists {:?}; qed", log.transaction_hash)
});
Expand Down
Loading