Skip to content

Commit

Permalink
use delete instead of update
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Nov 28, 2024
1 parent 158ff6b commit 2eb7266
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 0 additions & 4 deletions common/types/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ const (
TxStatusConfirmed
// TxStatusConfirmedFailed indicates that the transaction has failed during processing.
TxStatusConfirmedFailed
// TxStatusSentFailed indicates that the transaction has failed to be sent.
TxStatusSentFailed
)

func (s TxStatus) String() string {
Expand All @@ -324,8 +322,6 @@ func (s TxStatus) String() string {
return "TxStatusConfirmed"
case TxStatusConfirmedFailed:
return "TxStatusConfirmedFailed"
case TxStatusSentFailed:
return "TxStatusSentFailed"
default:
return fmt.Sprintf("Unknown TxStatus (%d)", int32(s))
}
Expand Down
14 changes: 7 additions & 7 deletions rollup/internal/controller/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ func (s *Sender) SendTransaction(contextID string, target *common.Address, data
}

if err := s.client.SendTransaction(s.ctx, signedTx); err != nil {
// SendTransaction failed, mark the transaction as failed
if updateErr := s.pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(s.ctx, signedTx.Hash(), types.TxStatusSentFailed, nil); updateErr != nil {
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)
return common.Hash{}, fmt.Errorf("failed to mark transaction as sent failed, err: %w", updateErr)
// Delete the transaction from the pending transaction table if it fails to send.
if updateErr := s.pendingTransactionOrm.DeletePendingTransactionByTxHash(s.ctx, signedTx.Hash()); updateErr != nil {
log.Error("failed to delete transaction", "tx hash", signedTx.Hash().String(), "from", s.transactionSigner.GetAddr().String(), "nonce", signedTx.Nonce(), "err", updateErr)
return common.Hash{}, fmt.Errorf("failed to delete transaction, err: %w", updateErr)
}

log.Error("failed to send tx", "tx hash", signedTx.Hash().String(), "from", s.transactionSigner.GetAddr().String(), "nonce", signedTx.Nonce(), "err", err)
Expand Down Expand Up @@ -629,9 +629,9 @@ func (s *Sender) checkPendingTransaction() {
if updateErr := s.pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(s.ctx, originalTx.Hash(), types.TxStatusPending, tx); updateErr != nil {
return fmt.Errorf("failed to rollback status of original transaction, err: %w", updateErr)
}
// Mark the new transaction as sent failed instead of deleting it
if updateErr := s.pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(s.ctx, newSignedTx.Hash(), types.TxStatusSentFailed, tx); updateErr != nil {
return fmt.Errorf("failed to mark transaction as sent failed, err: %w", updateErr)
// Delete the new transaction that was inserted
if updateErr := s.pendingTransactionOrm.DeletePendingTransactionByTxHash(s.ctx, newSignedTx.Hash(), tx); updateErr != nil {
return fmt.Errorf("failed to delete new transaction, err: %w", updateErr)
}
return nil
}); rollbackErr != nil {
Expand Down
16 changes: 16 additions & 0 deletions rollup/internal/orm/pending_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ func (o *PendingTransaction) InsertPendingTransaction(ctx context.Context, conte
return nil
}

// DeletePendingTransactionByTxHash deletes a pending transaction record from the database by transaction hash.
func (o *PendingTransaction) DeletePendingTransactionByTxHash(ctx context.Context, hash common.Hash, dbTX ...*gorm.DB) error {
db := o.db
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
}
db = db.WithContext(ctx)
db = db.Model(&PendingTransaction{})

result := db.Where("hash = ?", hash.String()).Delete(&PendingTransaction{})
if result.Error != nil {
return fmt.Errorf("failed to delete pending transaction, err: %w", result.Error)
}
return nil
}

// UpdatePendingTransactionStatusByTxHash updates the status of a transaction based on the transaction hash.
func (o *PendingTransaction) UpdatePendingTransactionStatusByTxHash(ctx context.Context, hash common.Hash, status types.TxStatus, dbTX ...*gorm.DB) error {
db := o.db
Expand Down

0 comments on commit 2eb7266

Please sign in to comment.