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: better tx receipt mitigation #614

Merged
merged 3 commits into from
Apr 24, 2024
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
37 changes: 27 additions & 10 deletions crates/provider/src/heart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,35 @@ impl<'a, T: Transport + Clone, N: Network> PendingTransactionBuilder<'a, T, N> {
/// confirmed.
/// - [`watch`](Self::watch) for watching the transaction without fetching the receipt.
pub async fn get_receipt(self) -> TransportResult<N::ReceiptResponse> {
// Try fetching receipt immediately to ensure that we don't watch for a transaction that is
// already confirmed.
let receipt = self.provider.get_transaction_receipt(self.config.tx_hash).await?;
if let Some(receipt) = receipt {
return Ok(receipt);
}
let hash = self.config.tx_hash;
let mut pending_tx = self.provider.watch_pending_transaction(self.config).await?;

// FIXME: this is a hotfix to prevent a race condition where the heartbeat would miss the
// block the tx was mined in
let mut interval =
tokio::time::interval(Duration::from_millis(self.provider.client().poll_interval()));

loop {
let mut confirmed = false;

select! {
_ = interval.tick() => {},
res = &mut pending_tx => {
let _ = res?;
confirmed = true;
}
}

let pending_tx = self.provider.watch_pending_transaction(self.config).await?;
let hash = pending_tx.await?;
let receipt = self.provider.get_transaction_receipt(hash).await?;
// try to fetch the receipt
let receipt = self.provider.get_transaction_receipt(hash).await?;
if let Some(receipt) = receipt {
return Ok(receipt);
}

receipt.ok_or(RpcError::NullResp)
if confirmed {
return Err(RpcError::NullResp);
}
}
}
}

Expand Down
Loading