From 528c89d00ba6215df05ae65986f2a8a2bbb07314 Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Mon, 2 Dec 2024 18:32:08 +0800 Subject: [PATCH] use permanently delete instead of soft delete && add unit tests --- rollup/internal/orm/orm_test.go | 13 +++++++++++++ rollup/internal/orm/pending_transaction.go | 13 ++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/rollup/internal/orm/orm_test.go b/rollup/internal/orm/orm_test.go index 58684b744..52e70df2c 100644 --- a/rollup/internal/orm/orm_test.go +++ b/rollup/internal/orm/orm_test.go @@ -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 } diff --git a/rollup/internal/orm/pending_transaction.go b/rollup/internal/orm/pending_transaction.go index 3fe72d971..c4dd025b8 100644 --- a/rollup/internal/orm/pending_transaction.go +++ b/rollup/internal/orm/pending_transaction.go @@ -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 { @@ -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 }