add mempool limit

This commit is contained in:
Solovyov1796 2025-03-04 15:13:54 +08:00
parent 351c2cb132
commit ea3e4b84e8

View File

@ -14,6 +14,8 @@ import (
evmtypes "github.com/evmos/ethermint/x/evm/types" evmtypes "github.com/evmos/ethermint/x/evm/types"
) )
const MAX_TXS_PRE_SENDER_IN_MEMPOOL int = 10
var ( var (
_ mempool.Mempool = (*PriorityNonceMempool)(nil) _ mempool.Mempool = (*PriorityNonceMempool)(nil)
_ mempool.Iterator = (*PriorityNonceIterator)(nil) _ mempool.Iterator = (*PriorityNonceIterator)(nil)
@ -34,6 +36,7 @@ type PriorityNonceMempool struct {
onRead func(tx sdk.Tx) onRead func(tx sdk.Tx)
txReplacement func(op, np int64, oTx, nTx sdk.Tx) bool txReplacement func(op, np int64, oTx, nTx sdk.Tx) bool
maxTx int maxTx int
counterBySender map[string]int
} }
type PriorityNonceIterator struct { type PriorityNonceIterator struct {
@ -133,6 +136,7 @@ func NewPriorityMempool(opts ...PriorityNonceMempoolOption) *PriorityNonceMempoo
priorityCounts: make(map[int64]int), priorityCounts: make(map[int64]int),
senderIndices: make(map[string]*skiplist.SkipList), senderIndices: make(map[string]*skiplist.SkipList),
scores: make(map[txMeta]txMeta), scores: make(map[txMeta]txMeta),
counterBySender: make(map[string]int),
} }
for _, opt := range opts { for _, opt := range opts {
@ -204,6 +208,16 @@ func (mp *PriorityNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error {
nonce = sig.Sequence 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
} else {
return fmt.Errorf("tx sender has too many txs in mempool")
}
}
key := txMeta{nonce: nonce, priority: priority, sender: sender} key := txMeta{nonce: nonce, priority: priority, sender: sender}
senderIndex, ok := mp.senderIndices[sender] senderIndex, ok := mp.senderIndices[sender]
@ -453,6 +467,14 @@ func (mp *PriorityNonceMempool) Remove(tx sdk.Tx) error {
delete(mp.scores, scoreKey) delete(mp.scores, scoreKey)
mp.priorityCounts[score.priority]-- 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 return nil
} }