Skip to content

Commit

Permalink
Remove tx from cache when canAddPendingTx fails (sei-protocol#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzang2019 authored and udpatil committed Apr 19, 2024
1 parent 64a5e7f commit a64ddae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
22 changes: 22 additions & 0 deletions internal/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a64ddae

Please sign in to comment.