diff --git a/pool/interfaces.go b/pool/interfaces.go index 81fa0600d6..4a6116deb9 100644 --- a/pool/interfaces.go +++ b/pool/interfaces.go @@ -25,6 +25,7 @@ type storage interface { IsTxPending(ctx context.Context, hash common.Hash) (bool, error) SetGasPrices(ctx context.Context, l2GasPrice uint64, l1GasPrice uint64) error DeleteGasPricesHistoryOlderThan(ctx context.Context, date time.Time) error + DeleteFailedTransactionsOlderThan(ctx context.Context, date time.Time) error UpdateTxsStatus(ctx context.Context, updateInfo []TxStatusUpdateInfo) error UpdateTxStatus(ctx context.Context, updateInfo TxStatusUpdateInfo) error UpdateTxWIPStatus(ctx context.Context, hash common.Hash, isWIP bool) error diff --git a/pool/pgpoolstorage/pgpoolstorage.go b/pool/pgpoolstorage/pgpoolstorage.go index 51d5aab1ba..9ba5a91ae9 100644 --- a/pool/pgpoolstorage/pgpoolstorage.go +++ b/pool/pgpoolstorage/pgpoolstorage.go @@ -415,6 +415,16 @@ func (p *PostgresPoolStorage) DeleteTransactionsByHashes(ctx context.Context, ha return nil } +// DeleteFailedTransactionsOlderThan deletes all failed transactions older than the given date +func (p *PostgresPoolStorage) DeleteFailedTransactionsOlderThan(ctx context.Context, date time.Time) error { + sql := `DELETE FROM pool.transaction WHERE status = 'failed' and received_at < $1` + + if _, err := p.db.Exec(ctx, sql, date); err != nil { + return err + } + return nil +} + // SetGasPrices sets the latest l2 and l1 gas prices func (p *PostgresPoolStorage) SetGasPrices(ctx context.Context, l2GasPrice, l1GasPrice uint64) error { sql := "INSERT INTO pool.gas_price (price, l1_price, timestamp) VALUES ($1, $2, $3)" diff --git a/sequencer/interfaces.go b/sequencer/interfaces.go index bdf9dd7829..53ca4642f3 100644 --- a/sequencer/interfaces.go +++ b/sequencer/interfaces.go @@ -20,6 +20,7 @@ import ( // txPool contains the methods required to interact with the tx pool. type txPool interface { DeleteTransactionsByHashes(ctx context.Context, hashes []common.Hash) error + DeleteFailedTransactionsOlderThan(ctx context.Context, date time.Time) error DeleteTransactionByHash(ctx context.Context, hash common.Hash) error MarkWIPTxsAsPending(ctx context.Context) error GetNonWIPPendingTxs(ctx context.Context) ([]pool.Transaction, error) diff --git a/sequencer/mock_pool.go b/sequencer/mock_pool.go index 44bcd5d463..ed580529a6 100644 --- a/sequencer/mock_pool.go +++ b/sequencer/mock_pool.go @@ -12,6 +12,8 @@ import ( pool "github.com/0xPolygonHermez/zkevm-node/pool" state "github.com/0xPolygonHermez/zkevm-node/state" + + time "time" ) // PoolMock is an autogenerated mock type for the txPool type @@ -19,6 +21,20 @@ type PoolMock struct { mock.Mock } +// DeleteFailedTransactionsOlderThan provides a mock function with given fields: ctx, date +func (_m *PoolMock) DeleteFailedTransactionsOlderThan(ctx context.Context, date time.Time) error { + ret := _m.Called(ctx, date) + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, time.Time) error); ok { + r0 = rf(ctx, date) + } else { + r0 = ret.Error(0) + } + + return r0 +} + // DeleteTransactionByHash provides a mock function with given fields: ctx, hash func (_m *PoolMock) DeleteTransactionByHash(ctx context.Context, hash common.Hash) error { ret := _m.Called(ctx, hash) diff --git a/sequencer/sequencer.go b/sequencer/sequencer.go index e8b0d05a77..8518a0aa8c 100644 --- a/sequencer/sequencer.go +++ b/sequencer/sequencer.go @@ -206,13 +206,22 @@ func (s *Sequencer) purgeOldPoolTxs(ctx context.Context) { log.Errorf("failed to get txs hashes to delete, err: %v", err) continue } - log.Infof("will try to delete %d redundant txs", len(txHashes)) + log.Infof("trying to delete %d selected txs", len(txHashes)) err = s.pool.DeleteTransactionsByHashes(ctx, txHashes) if err != nil { - log.Errorf("failed to delete txs from the pool, err: %v", err) + log.Errorf("failed to delete selected txs from the pool, err: %v", err) continue } log.Infof("deleted %d selected txs from the pool", len(txHashes)) + + log.Infof("trying to delete failed txs from the pool") + // Delete failed txs older than a certain date (14 seconds per L1 block) + err = s.pool.DeleteFailedTransactionsOlderThan(ctx, time.Now().Add(-time.Duration(s.cfg.BlocksAmountForTxsToBeDeleted*14)*time.Second)) //nolint:gomnd + if err != nil { + log.Errorf("failed to delete failed txs from the pool, err: %v", err) + continue + } + log.Infof("failed txs deleted from the pool") } }