Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feature: pending transaction retries repeatedly before assuming dropped #1221

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@

### Unreleased

- Add retries to the pending transaction future
[1221](https://github.com/gakonst/ethers-rs/pull/1221)
- Add support for basic and bearer authentication in http and non-wasm websockets.
[829](https://github.com/gakonst/ethers-rs/pull/829)
- Export `ethers_providers::IpcError` and `ethers_providers::QuorumError`
Expand Down
22 changes: 19 additions & 3 deletions ethers-providers/src/pending_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ pub struct PendingTransaction<'a, P> {
provider: &'a Provider<P>,
state: PendingTxState<'a>,
interval: Box<dyn Stream<Item = ()> + Send + Unpin>,
retries_remaining: usize,
}

const DEFAULT_RETRIES: usize = 3;

impl<'a, P: JsonRpcClient> PendingTransaction<'a, P> {
/// Creates a new pending transaction poller from a hash and a provider
pub fn new(tx_hash: TxHash, provider: &'a Provider<P>) -> Self {
Expand All @@ -74,6 +77,7 @@ impl<'a, P: JsonRpcClient> PendingTransaction<'a, P> {
provider,
state: PendingTxState::InitialDelay(delay),
interval: Box::new(interval(DEFAULT_POLL_INTERVAL)),
retries_remaining: DEFAULT_RETRIES,
}
}

Expand Down Expand Up @@ -111,6 +115,13 @@ impl<'a, P: JsonRpcClient> PendingTransaction<'a, P> {

self
}

/// Set retries
#[must_use]
pub fn retries(mut self, retries: usize) -> Self {
self.retries_remaining = retries;
self
}
}

impl<'a, P> PendingTransaction<'a, P> {
Expand Down Expand Up @@ -188,9 +199,14 @@ impl<'a, P: JsonRpcClient> Future for PendingTransaction<'a, P> {
let tx_opt = tx_res.unwrap();
// If the tx is no longer in the mempool, return Ok(None)
if tx_opt.is_none() {
tracing::debug!("Dropped from mempool, pending tx {:?}", *this.tx_hash);
*this.state = PendingTxState::Completed;
return Poll::Ready(Ok(None))
if *this.retries_remaining == 0 {
tracing::debug!("Dropped from mempool, pending tx {:?}", *this.tx_hash);
*this.state = PendingTxState::Completed;
return Poll::Ready(Ok(None))
}

*this.retries_remaining -= 1;
rewake_with_new_state!(ctx, this, PendingTxState::PausedGettingTx);
}

// If it hasn't confirmed yet, poll again later
Expand Down