From a5dc439f57ebc97806828d89afeb80231db321d1 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Sun, 10 Sep 2023 02:23:31 -0700 Subject: [PATCH 1/3] unit test iterator --- types/mempool/priority_nonce_test.go | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/types/mempool/priority_nonce_test.go b/types/mempool/priority_nonce_test.go index 838c41743e16..ad3a1006afd0 100644 --- a/types/mempool/priority_nonce_test.go +++ b/types/mempool/priority_nonce_test.go @@ -258,6 +258,62 @@ func (s *MempoolTestSuite) TestPriorityNonceTxOrder() { } } +func (s *MempoolTestSuite) TestIterator() { + t := s.T() + ctx := sdk.NewContext(nil, false, log.NewNopLogger()) + accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 2) + sa := accounts[0].Address + sb := accounts[1].Address + + tests := []struct { + txs []txSpec + fail bool + }{ + { + txs: []txSpec{ + {p: 20, n: 1, a: sa}, + {p: 15, n: 1, a: sb}, + {p: 6, n: 2, a: sa}, + {p: 21, n: 4, a: sa}, + {p: 8, n: 2, a: sb}, + }, + }, + { + txs: []txSpec{ + {p: 20, n: 1, a: sa}, + {p: 15, n: 1, a: sb}, + {p: 6, n: 2, a: sa}, + {p: 21, n: 4, a: sa}, + {p: math.MinInt64, n: 2, a: sb}, + }, + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { + pool := mempool.DefaultPriorityMempool() + + // create test txs and insert into mempool + for i, ts := range tt.txs { + tx := testTx{id: i, priority: int64(ts.p), nonce: uint64(ts.n), address: ts.a} + c := ctx.WithPriority(tx.priority) + err := pool.Insert(c, tx) + require.NoError(t, err) + } + + // iterate through txs + iterator := pool.Select(ctx, nil) + for iterator != nil { + tx := iterator.Tx().(testTx) + require.Equal(t, tt.txs[tx.id].p, int(tx.priority)) + require.Equal(t, tt.txs[tx.id].n, int(tx.nonce)) + require.Equal(t, tt.txs[tx.id].a, tx.address) + iterator = iterator.Next() + } + }) + } +} + func (s *MempoolTestSuite) TestPriorityTies() { ctx := sdk.NewContext(nil, false, log.NewNopLogger()) accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 3) From 90bbc42138c7896c58cca1ac19dd380eceb743d9 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Sun, 10 Sep 2023 02:23:46 -0700 Subject: [PATCH 2/3] fix iterator --- types/mempool/priority_nonce.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/mempool/priority_nonce.go b/types/mempool/priority_nonce.go index 401526b7b254..4073409bf0e1 100644 --- a/types/mempool/priority_nonce.go +++ b/types/mempool/priority_nonce.go @@ -314,7 +314,7 @@ func (i *PriorityNonceIterator[C]) Next() Iterator { // priority in the pool. if i.mempool.cfg.TxPriority.Compare(key.priority, i.nextPriority) < 0 { return i.iteratePriority() - } else if i.mempool.cfg.TxPriority.Compare(key.priority, i.nextPriority) == 0 { + } else if i.priorityNode.Next() != nil && i.mempool.cfg.TxPriority.Compare(key.priority, i.nextPriority) == 0 { // Weight is incorporated into the priority index key only (not sender index) // so we must fetch it here from the scores map. weight := i.mempool.scores[txMeta[C]{nonce: key.nonce, sender: key.sender}].weight From b753f39f4052ba9ca6aa85078fff92bac51b66ad Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Sun, 10 Sep 2023 14:04:16 -0700 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31eff0d6ec91..ba692c93a394 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (types) [#16583](https://github.com/cosmos/cosmos-sdk/pull/16583), [#17372](https://github.com/cosmos/cosmos-sdk/pull/17372) Add `MigrationModuleManager` to handle migration of upgrade module before other modules, ensuring access to the updated context with consensus parameters within the same block that executes the migration. * (baseapp) [#17518](https://github.com/cosmos/cosmos-sdk/pull/17518) Utilizing voting power from vote extensions (CometBFT) instead of the current bonded tokens (x/staking) to determine if a set of vote extensions are valid. * (config) [#17649](https://github.com/cosmos/cosmos-sdk/pull/17649) Fix `mempool.max-txs` configuration is invalid in `app.config`. +* (mempool) [#17668](https://github.com/cosmos/cosmos-sdk/pull/17668) Fix: `PriorityNonceIterator.Next()` nil pointer ref for min priority at the end of iteration. ### API Breaking Changes