Skip to content

Commit 408ea92

Browse files
author
colinlyguo
committed
fix(rollup-relayer): graceful restart
1 parent 438a9fb commit 408ea92

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

common/types/db.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ const (
310310
TxStatusConfirmed
311311
// TxStatusConfirmedFailed indicates that the transaction has failed during processing.
312312
TxStatusConfirmedFailed
313+
// TxStatusSentFailed indicates that the transaction has failed to be sent.
314+
TxStatusSentFailed
313315
)
314316

315317
func (s TxStatus) String() string {
@@ -322,6 +324,8 @@ func (s TxStatus) String() string {
322324
return "TxStatusConfirmed"
323325
case TxStatusConfirmedFailed:
324326
return "TxStatusConfirmedFailed"
327+
case TxStatusSentFailed:
328+
return "TxStatusSentFailed"
325329
default:
326330
return fmt.Sprintf("Unknown TxStatus (%d)", int32(s))
327331
}

rollup/internal/controller/sender/sender.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ func (s *Sender) SendTransaction(contextID string, target *common.Address, data
232232
}
233233

234234
if err := s.client.SendTransaction(s.ctx, signedTx); err != nil {
235+
// SendTransaction failed, mark the transaction as failed
236+
if updateErr := s.pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(s.ctx, signedTx.Hash(), types.TxStatusSentFailed, nil); updateErr != nil {
237+
log.Error("failed to mark transaction as sent failed", "tx hash", signedTx.Hash().String(), "from", s.transactionSigner.GetAddr().String(), "nonce", signedTx.Nonce(), "sendTxErr", err, "updateErr", updateErr)
238+
}
239+
235240
log.Error("failed to send tx", "tx hash", signedTx.Hash().String(), "from", s.transactionSigner.GetAddr().String(), "nonce", signedTx.Nonce(), "err", err)
236241
// Check if contain nonce, and reset nonce
237242
// only reset nonce when it is not from resubmit
@@ -458,6 +463,15 @@ func (s *Sender) createReplacingTransaction(tx *gethTypes.Transaction, baseFee,
458463
blobGasFeeCap = maxBlobGasPrice
459464
}
460465

466+
// Check if any fee cap is less than double
467+
doubledTipCap := new(big.Int).Mul(originalGasTipCap, big.NewInt(2))
468+
doubledFeeCap := new(big.Int).Mul(originalGasFeeCap, big.NewInt(2))
469+
doubledBlobFeeCap := new(big.Int).Mul(originalBlobGasFeeCap, big.NewInt(2))
470+
if gasTipCap.Cmp(doubledTipCap) < 0 || gasFeeCap.Cmp(doubledFeeCap) < 0 || blobGasFeeCap.Cmp(doubledBlobFeeCap) < 0 {
471+
log.Error("gas fees must be at least double", "originalTipCap", originalGasTipCap, "currentTipCap", gasTipCap, "requiredTipCap", doubledTipCap, "originalFeeCap", originalGasFeeCap, "currentFeeCap", gasFeeCap, "requiredFeeCap", doubledFeeCap, "originalBlobFeeCap", originalBlobGasFeeCap, "currentBlobFeeCap", blobGasFeeCap, "requiredBlobFeeCap", doubledBlobFeeCap)
472+
return nil, errors.New("gas fees must be at least double")
473+
}
474+
461475
feeData.gasFeeCap = gasFeeCap
462476
feeData.gasTipCap = gasTipCap
463477
feeData.blobGasFeeCap = blobGasFeeCap
@@ -608,6 +622,23 @@ func (s *Sender) checkPendingTransaction() {
608622
}
609623

610624
if err := s.client.SendTransaction(s.ctx, newSignedTx); err != nil {
625+
// SendTransaction failed, need to rollback the previous database changes
626+
if rollbackErr := s.db.Transaction(func(tx *gorm.DB) error {
627+
// Restore original transaction status back to pending
628+
if updateErr := s.pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(s.ctx, originalTx.Hash(), types.TxStatusPending, tx); updateErr != nil {
629+
return fmt.Errorf("failed to rollback status of original transaction, err: %w", updateErr)
630+
}
631+
// Mark the new transaction as sent failed instead of deleting it
632+
if updateErr := s.pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(s.ctx, newSignedTx.Hash(), types.TxStatusSentFailed, tx); updateErr != nil {
633+
return fmt.Errorf("failed to mark transaction as sent failed, err: %w", updateErr)
634+
}
635+
return nil
636+
}); rollbackErr != nil {
637+
// Both SendTransaction and rollback failed
638+
log.Error("failed to rollback database after SendTransaction failed", "tx hash", newSignedTx.Hash().String(), "from", s.transactionSigner.GetAddr().String(), "nonce", newSignedTx.Nonce(), "sendTxErr", err, "rollbackErr", rollbackErr)
639+
return
640+
}
641+
611642
log.Error("failed to send replacing tx", "tx hash", newSignedTx.Hash().String(), "from", s.transactionSigner.GetAddr().String(), "nonce", newSignedTx.Nonce(), "err", err)
612643
return
613644
}

0 commit comments

Comments
 (0)