Skip to content

Commit

Permalink
delete failed txs during pool cleanup (0xPolygonHermez#2733)
Browse files Browse the repository at this point in the history
* delete failed txs during pool cleanup

* delete failed txs during pool cleanup

* improve logs
  • Loading branch information
ToniRamirezM authored Oct 31, 2023
1 parent 46d88db commit 8c349ee
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions pool/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pool/pgpoolstorage/pgpoolstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
1 change: 1 addition & 0 deletions sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions sequencer/mock_pool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down

0 comments on commit 8c349ee

Please sign in to comment.