Skip to content

Commit

Permalink
Do not consider transaction dropped for some time (#11)
Browse files Browse the repository at this point in the history
When using Infura, a newly created transaction may not show up when
querying the network for some time. This causes false-positive "dropped
transaction" detection.

This change will wait until 30 seconds has elapsed before considering a
transaction dropped.
  • Loading branch information
azdagron authored Jun 28, 2024
1 parent ffa5932 commit 29de077
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions internal/eth/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

const (
// notDroppedUntil is how long to wait for a transaction
// before considering it dropped.
notDroppedUntil = time.Second * 30
)

type WaitProgress interface {
Start(hash common.Hash)
Canceled()
Expand Down Expand Up @@ -64,13 +70,16 @@ func (w progressWaiter) Wait(ctx context.Context, hash common.Hash) (uint64, err
}

func WaitForTransaction(ctx context.Context, backend WaitBackend, hash common.Hash, progress WaitProgress) (uint64, error) {
startTime := time.Now()

if ctx == nil {
ctx = context.Background()
}
if progress == nil {
progress = NoProgress{}
}
progress.Start(hash)

for {
select {
case <-time.After(time.Millisecond * 100):
Expand Down Expand Up @@ -98,8 +107,13 @@ func WaitForTransaction(ctx context.Context, backend WaitBackend, hash common.Ha
switch {
case err == nil:
case err == ethereum.NotFound:
progress.Dropped()
return 0, errors.New("transaction dropped")
// Wait up to notDroppedUntil time before considering the
// transaction dropped. This avoids false positives when using
// networks like Infura, whose nodes are eventually consistent.
if time.Since(startTime) >= notDroppedUntil {
progress.Dropped()
return 0, errors.New("transaction dropped")
}
default:
progress.TempError(fmt.Errorf("failed to query for transaction by hash: %+v", err))
}
Expand Down

0 comments on commit 29de077

Please sign in to comment.