From 37815ca9450190491ae342106150e12f59740c9d Mon Sep 17 00:00:00 2001 From: Solovyov1796 Date: Mon, 31 Mar 2025 11:10:30 +0800 Subject: [PATCH] pref: skiplist using static rand generator --- app/priority_nonce.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/priority_nonce.go b/app/priority_nonce.go index 4b607cbe..fb786540 100644 --- a/app/priority_nonce.go +++ b/app/priority_nonce.go @@ -2,7 +2,9 @@ package app import ( "context" + "math/rand" "sync" + "time" "fmt" "math" @@ -27,6 +29,9 @@ var ( errMempoolTooManyTxs = errors.New("tx sender has too many txs in mempool") errMempoolIsFull = errors.New("mempool is full") errTxInMempool = errors.New("tx already in mempool") + + staticSrc = rand.NewSource(time.Now().UnixNano()) + staticRng = rand.New(staticSrc) ) // PriorityNonceMempool is a mempool implementation that stores txs @@ -160,6 +165,8 @@ func NewPriorityMempool(opts ...PriorityNonceMempoolOption) *PriorityNonceMempoo txRecord: make(map[txMeta]struct{}), } + mp.priorityIndex.SetRandSource(staticSrc) + for _, opt := range opts { opt(mp) } @@ -218,6 +225,7 @@ func (mp *PriorityNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error { senderIndex = skiplist.New(skiplist.LessThanFunc(func(a, b any) int { return skiplist.Uint64.Compare(b.(txMeta).nonce, a.(txMeta).nonce) })) + senderIndex.SetRandSource(staticRng) // initialize sender index if not found mp.senderIndices[txInfo.Sender] = senderIndex @@ -306,7 +314,7 @@ func (mp *PriorityNonceMempool) doInsert(newKey txMeta, tx sdk.Tx, incrCnt bool) senderIndex = skiplist.New(skiplist.LessThanFunc(func(a, b any) int { return skiplist.Uint64.Compare(b.(txMeta).nonce, a.(txMeta).nonce) })) - + senderIndex.SetRandSource(staticRng) // initialize sender index if not found mp.senderIndices[newKey.sender] = senderIndex }