From c066af2a4774f040571e4fa733bbe6ea118b6dbc Mon Sep 17 00:00:00 2001 From: Solovyov1796 Date: Mon, 10 Mar 2025 17:33:31 +0800 Subject: [PATCH] fix txs limit in mempool --- app/priority_nonce.go | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/app/priority_nonce.go b/app/priority_nonce.go index a7c604de..dbbd0027 100644 --- a/app/priority_nonce.go +++ b/app/priority_nonce.go @@ -14,7 +14,7 @@ import ( evmtypes "github.com/evmos/ethermint/x/evm/types" ) -const MAX_TXS_PRE_SENDER_IN_MEMPOOL int = 10 +const MAX_TXS_PRE_SENDER_IN_MEMPOOL int = 16 var ( _ mempool.Mempool = (*PriorityNonceMempool)(nil) @@ -37,6 +37,7 @@ type PriorityNonceMempool struct { txReplacement func(op, np int64, oTx, nTx sdk.Tx) bool maxTx int counterBySender map[string]int + txRecord map[txMeta]struct{} } type PriorityNonceIterator struct { @@ -137,6 +138,7 @@ func NewPriorityMempool(opts ...PriorityNonceMempoolOption) *PriorityNonceMempoo senderIndices: make(map[string]*skiplist.SkipList), scores: make(map[txMeta]txMeta), counterBySender: make(map[string]int), + txRecord: make(map[txMeta]struct{}), } for _, opt := range opts { @@ -208,13 +210,18 @@ func (mp *PriorityNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error { nonce = sig.Sequence } - if _, exists := mp.counterBySender[sender]; !exists { - mp.counterBySender[sender] = 1 - } else { - if mp.counterBySender[sender] < MAX_TXS_PRE_SENDER_IN_MEMPOOL { - mp.counterBySender[sender] += 1 + existsKey := txMeta{nonce: nonce, sender: sender} + if _, exists := mp.txRecord[existsKey]; !exists { + mp.txRecord[existsKey] = struct{}{} + + if _, exists := mp.counterBySender[sender]; !exists { + mp.counterBySender[sender] = 1 } else { - return fmt.Errorf("tx sender has too many txs in mempool") + if mp.counterBySender[sender] < MAX_TXS_PRE_SENDER_IN_MEMPOOL { + mp.counterBySender[sender] += 1 + } else { + return fmt.Errorf("tx sender has too many txs in mempool") + } } } @@ -450,6 +457,19 @@ func (mp *PriorityNonceMempool) Remove(tx sdk.Tx) error { nonce = sig.Sequence } + existsKey := txMeta{nonce: nonce, sender: sender} + if _, exists := mp.txRecord[existsKey]; exists { + delete(mp.txRecord, existsKey) + + if _, exists := mp.counterBySender[sender]; exists { + if mp.counterBySender[sender] > 1 { + mp.counterBySender[sender] -= 1 + } else { + delete(mp.counterBySender, sender) + } + } + } + scoreKey := txMeta{nonce: nonce, sender: sender} score, ok := mp.scores[scoreKey] if !ok { @@ -467,14 +487,6 @@ func (mp *PriorityNonceMempool) Remove(tx sdk.Tx) error { delete(mp.scores, scoreKey) mp.priorityCounts[score.priority]-- - if _, exists := mp.counterBySender[sender]; exists { - if mp.counterBySender[sender] > 1 { - mp.counterBySender[sender] -= 1 - } else { - delete(mp.counterBySender, sender) - } - } - return nil }