Skip to content

Commit

Permalink
retry sendtransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-shim committed Apr 2, 2024
1 parent 3d9fff2 commit bb960b1
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions disperser/batcher/txn_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
var (
gasPricePercentageMultiplier = big.NewInt(10)
hundred = big.NewInt(100)
maxSpeedUpRetry = 3
maxSendTransactionRetry = 3
)

// TxnManager receives transactions from the caller, sends them to the chain, and monitors their status.
Expand Down Expand Up @@ -141,20 +141,31 @@ func (t *txnManager) ProcessTransaction(ctx context.Context, req *TxnRequest) er
t.mu.Lock()
defer t.mu.Unlock()
t.logger.Debug("new transaction", "component", "TxnManager", "method", "ProcessTransaction", "tag", req.Tag, "nonce", req.Tx.Nonce(), "gasFeeCap", req.Tx.GasFeeCap(), "gasTipCap", req.Tx.GasTipCap())
gasTipCap, gasFeeCap, err := t.ethClient.GetLatestGasCaps(ctx)
if err != nil {
return fmt.Errorf("failed to get latest gas caps: %w", err)
}

txn, err := t.ethClient.UpdateGas(ctx, req.Tx, req.Value, gasTipCap, gasFeeCap)
if err != nil {
return fmt.Errorf("failed to update gas price: %w", err)
}
txID, err := t.wallet.SendTransaction(ctx, txn)
if err != nil {
return fmt.Errorf("failed to send txn (%s) %s: %w", req.Tag, req.Tx.Hash().Hex(), err)
} else {
t.logger.Debug("successfully sent txn", "component", "TxnManager", "method", "ProcessTransaction", "tag", req.Tag, "txID", txID, "txHash", txn.Hash().Hex())
var txn *types.Transaction
var txID walletsdk.TxID
retryFromFailure := 0
for retryFromFailure < maxSendTransactionRetry {
gasTipCap, gasFeeCap, err := t.ethClient.GetLatestGasCaps(ctx)
if err != nil {
return fmt.Errorf("failed to get latest gas caps: %w", err)
}

txn, err := t.ethClient.UpdateGas(ctx, req.Tx, req.Value, gasTipCap, gasFeeCap)
if err != nil {
return fmt.Errorf("failed to update gas price: %w", err)
}
txID, err := t.wallet.SendTransaction(ctx, txn)
if errors.Is(err, context.DeadlineExceeded) {
t.logger.Warn("failed to send txn", req.Tag, req.Tx.Hash().Hex(), retryFromFailure, maxSendTransactionRetry, err)
retryFromFailure++
continue
} else if err != nil {
return fmt.Errorf("failed to send txn (%s) %s: %w", req.Tag, req.Tx.Hash().Hex(), err)
} else {
t.logger.Debug("successfully sent txn", "component", "TxnManager", "method", "ProcessTransaction", "tag", req.Tag, "txID", txID, "txHash", txn.Hash().Hex())
break
}
}
req.Tx = txn
req.txAttempts = append(req.txAttempts, &transaction{
Expand Down Expand Up @@ -261,12 +272,12 @@ func (t *txnManager) monitorTransaction(ctx context.Context, req *TxnRequest) (*
}
txID, err := t.wallet.SendTransaction(ctx, newTx)
if err != nil {
if retryFromFailure >= maxSpeedUpRetry {
t.logger.Warn("failed to send txn - retries exhausted", "component", "TxnManager", "method", "monitorTransaction", "tag", req.Tag, "txn", req.Tx.Hash().Hex(), "attempt", retryFromFailure, "maxRetry", maxSpeedUpRetry, "err", err)
if retryFromFailure >= maxSendTransactionRetry {
t.logger.Warn("failed to send txn - retries exhausted", "component", "TxnManager", "method", "monitorTransaction", "tag", req.Tag, "txn", req.Tx.Hash().Hex(), "attempt", retryFromFailure, "maxRetry", maxSendTransactionRetry, "err", err)
t.metrics.IncrementTxnCount("failure")
return nil, err
} else {
t.logger.Warn("failed to send txn - retrying", "component", "TxnManager", "method", "monitorTransaction", "tag", req.Tag, "txn", req.Tx.Hash().Hex(), "attempt", retryFromFailure, "maxRetry", maxSpeedUpRetry, "err", err)
t.logger.Warn("failed to send txn - retrying", "component", "TxnManager", "method", "monitorTransaction", "tag", req.Tag, "txn", req.Tx.Hash().Hex(), "attempt", retryFromFailure, "maxRetry", maxSendTransactionRetry, "err", err)
}
retryFromFailure++
continue
Expand Down

0 comments on commit bb960b1

Please sign in to comment.