Skip to content

Commit

Permalink
use permanently delete instead of soft delete && add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 2, 2024
1 parent feb3294 commit 528c89d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 13 additions & 0 deletions rollup/internal/orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,17 @@ func TestPendingTransactionOrm(t *testing.T) {
status, err := pendingTransactionOrm.GetTxStatusByTxHash(context.Background(), tx0.Hash())
assert.NoError(t, err)
assert.Equal(t, types.TxStatusConfirmedFailed, status)

// Test DeleteTransactionByTxHash
err = pendingTransactionOrm.DeleteTransactionByTxHash(context.Background(), tx0.Hash())
assert.NoError(t, err)

// Verify the transaction is deleted
status, err = pendingTransactionOrm.GetTxStatusByTxHash(context.Background(), tx0.Hash())
assert.NoError(t, err)
assert.Equal(t, types.TxStatusUnknown, status) // Should return unknown status for deleted transaction

// Try to delete non-existent transaction
err = pendingTransactionOrm.DeleteTransactionByTxHash(context.Background(), common.HexToHash("0x123"))
assert.Error(t, err) // Should return error for non-existent transaction
}
13 changes: 8 additions & 5 deletions rollup/internal/orm/pending_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func (o *PendingTransaction) InsertPendingTransaction(ctx context.Context, conte
return nil
}

// DeleteTransactionByTxHash deletes a transaction record from the database by transaction hash.
// DeleteTransactionByTxHash permanently deletes a transaction record from the database by transaction hash.
// Using hard delete instead of soft delete to prevent database bloat, as repeated SendTransaction failures
// could write a large number of transactions to the database.
func (o *PendingTransaction) DeleteTransactionByTxHash(ctx context.Context, hash common.Hash, dbTX ...*gorm.DB) error {
db := o.db
if len(dbTX) > 0 && dbTX[0] != nil {
Expand All @@ -160,15 +162,16 @@ func (o *PendingTransaction) DeleteTransactionByTxHash(ctx context.Context, hash
db = db.WithContext(ctx)
db = db.Model(&PendingTransaction{})

result := db.Where("hash = ?", hash.String()).Delete(&PendingTransaction{})
// Perform hard delete by using Unscoped()
result := db.Where("hash = ?", hash.String()).Unscoped().Delete(&PendingTransaction{})
if result.Error != nil {
return fmt.Errorf("failed to delete pending transaction, err: %w", result.Error)
return fmt.Errorf("failed to delete transaction, err: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("no pending transaction found with hash: %s", hash.String())
return fmt.Errorf("no transaction found with hash: %s", hash.String())
}
if result.RowsAffected > 0 {
log.Warn("Successfully deleted pending transaction", "hash", hash.String())
log.Warn("Successfully deleted transaction", "hash", hash.String())
}
return nil
}
Expand Down

0 comments on commit 528c89d

Please sign in to comment.