|
| 1 | +package loadtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/core/types" |
| 11 | + "github.com/ethereum/go-ethereum/ethclient" |
| 12 | +) |
| 13 | + |
| 14 | +// waitReceipt waits for a transaction receipt with default parameters. |
| 15 | +func waitReceipt(ctx context.Context, client *ethclient.Client, txHash common.Hash) (*types.Receipt, error) { |
| 16 | + return internalWaitReceipt(ctx, client, txHash, 0, 0, 0) |
| 17 | +} |
| 18 | + |
| 19 | +// waitReceiptWithRetries waits for a transaction receipt with retries and exponential backoff. |
| 20 | +func waitReceiptWithRetries(ctx context.Context, client *ethclient.Client, txHash common.Hash, maxRetries uint, initialDelayMs uint) (*types.Receipt, error) { |
| 21 | + return internalWaitReceipt(ctx, client, txHash, maxRetries, initialDelayMs, 0) |
| 22 | +} |
| 23 | + |
| 24 | +// waitReceiptWithTimeout waits for a transaction receipt with a specified timeout. |
| 25 | +func waitReceiptWithTimeout(ctx context.Context, client *ethclient.Client, txHash common.Hash, timeout time.Duration) (*types.Receipt, error) { |
| 26 | + return internalWaitReceipt(ctx, client, txHash, 0, 0, timeout) |
| 27 | +} |
| 28 | + |
| 29 | +// waitReceiptWithRetriesAndTimeout waits for a transaction receipt with retries, exponential backoff, and a timeout. |
| 30 | +func internalWaitReceipt(ctx context.Context, client *ethclient.Client, txHash common.Hash, maxRetries uint, initialDelayMs uint, timeout time.Duration) (*types.Receipt, error) { |
| 31 | + // Set defaults for zero values |
| 32 | + effectiveTimeout := timeout |
| 33 | + if effectiveTimeout == 0 { |
| 34 | + effectiveTimeout = 1 * time.Minute // Default: 1 minute |
| 35 | + } |
| 36 | + |
| 37 | + // Create context with timeout |
| 38 | + timeoutCtx, cancel := context.WithTimeout(ctx, effectiveTimeout) |
| 39 | + defer cancel() |
| 40 | + |
| 41 | + effectiveInitialDelayMs := initialDelayMs |
| 42 | + if effectiveInitialDelayMs == 0 { |
| 43 | + effectiveInitialDelayMs = 100 // Default: 100ms |
| 44 | + } else if effectiveInitialDelayMs < 10 { |
| 45 | + effectiveInitialDelayMs = 10 // Minimum 10ms |
| 46 | + } |
| 47 | + |
| 48 | + for attempt := uint(0); ; attempt++ { |
| 49 | + receipt, err := client.TransactionReceipt(timeoutCtx, txHash) |
| 50 | + if err == nil && receipt != nil { |
| 51 | + return receipt, nil |
| 52 | + } |
| 53 | + |
| 54 | + // If maxRetries > 0 and we've reached the limit, exit |
| 55 | + // Note: effectiveMaxRetries is always > 0 due to default above |
| 56 | + if maxRetries > 0 && attempt >= maxRetries-1 { |
| 57 | + return nil, fmt.Errorf("failed to get receipt after %d attempts: %w", maxRetries, err) |
| 58 | + } |
| 59 | + |
| 60 | + // Calculate delay |
| 61 | + baseDelay := time.Duration(effectiveInitialDelayMs) * time.Millisecond |
| 62 | + exponentialDelay := baseDelay * time.Duration(1<<attempt) |
| 63 | + |
| 64 | + // Add cap to prevent extremely long delays |
| 65 | + maxDelay := 30 * time.Second |
| 66 | + if exponentialDelay > maxDelay { |
| 67 | + exponentialDelay = maxDelay |
| 68 | + } |
| 69 | + |
| 70 | + maxJitter := exponentialDelay / 2 |
| 71 | + if maxJitter <= 0 { |
| 72 | + maxJitter = 1 * time.Millisecond |
| 73 | + } |
| 74 | + jitter := time.Duration(rand.Int63n(int64(maxJitter))) |
| 75 | + totalDelay := exponentialDelay + jitter |
| 76 | + |
| 77 | + select { |
| 78 | + case <-timeoutCtx.Done(): |
| 79 | + return nil, timeoutCtx.Err() |
| 80 | + case <-time.After(totalDelay): |
| 81 | + // Continue |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments