From a64ddaec25c91a772aa9522f230d1e538b491df3 Mon Sep 17 00:00:00 2001 From: Yiming Zang <50607998+yzang2019@users.noreply.github.com> Date: Wed, 17 Apr 2024 22:27:03 +0800 Subject: [PATCH] Remove tx from cache when canAddPendingTx fails (#230) --- internal/mempool/mempool.go | 1 + internal/mempool/mempool_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/internal/mempool/mempool.go b/internal/mempool/mempool.go index aff14a1c7..90ef114c5 100644 --- a/internal/mempool/mempool.go +++ b/internal/mempool/mempool.go @@ -337,6 +337,7 @@ func (txmp *TxMempool) CheckTx( } if err := txmp.canAddPendingTx(wtx); err != nil { // TODO: eviction strategy for pending transactions + removeHandler(true) return err } atomic.AddInt64(&txmp.pendingSizeBytes, int64(wtx.Size())) diff --git a/internal/mempool/mempool_test.go b/internal/mempool/mempool_test.go index d444cfb58..650664b19 100644 --- a/internal/mempool/mempool_test.go +++ b/internal/mempool/mempool_test.go @@ -607,6 +607,28 @@ func TestTxMempool_PendingStoreSize(t *testing.T) { require.Contains(t, err.Error(), "mempool pending set is full") } +func TestTxMempool_RemoveCacheWhenPendingTxIsFull(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + client := abciclient.NewLocalClient(log.NewNopLogger(), &application{Application: kvstore.NewApplication()}) + if err := client.Start(ctx); err != nil { + t.Fatal(err) + } + t.Cleanup(client.Wait) + + txmp := setup(t, client, 10) + txmp.config.PendingSize = 1 + peerID := uint16(1) + address1 := "0xeD23B3A9DE15e92B9ef9540E587B3661E15A12fA" + require.NoError(t, txmp.CheckTx(ctx, []byte(fmt.Sprintf("evm-sender=%s=%d=%d", address1, 1, 1)), nil, TxInfo{SenderID: peerID})) + err := txmp.CheckTx(ctx, []byte(fmt.Sprintf("evm-sender=%s=%d=%d", address1, 1, 2)), nil, TxInfo{SenderID: peerID}) + require.Error(t, err) + txCache := txmp.cache.(*LRUTxCache) + // Make sure the second tx is removed from cache + require.Equal(t, 1, len(txCache.cacheMap)) +} + func TestTxMempool_EVMEviction(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel()