github.com/cosmos/cosmos-sdk@v0.50.10/types/mempool/sender_nonce_test.go (about) 1 package mempool_test 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 8 cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" 9 "github.com/stretchr/testify/require" 10 11 "cosmossdk.io/log" 12 13 sdk "github.com/cosmos/cosmos-sdk/types" 14 "github.com/cosmos/cosmos-sdk/types/mempool" 15 simtypes "github.com/cosmos/cosmos-sdk/types/simulation" 16 ) 17 18 func (s *MempoolTestSuite) TestTxOrder() { 19 t := s.T() 20 ctx := sdk.NewContext(nil, cmtproto.Header{}, false, log.NewNopLogger()) 21 accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 5) 22 sa := accounts[0].Address 23 sb := accounts[1].Address 24 25 tests := []struct { 26 txs []txSpec 27 order []int 28 fail bool 29 seed int64 30 }{ 31 { 32 txs: []txSpec{ 33 {p: 21, n: 4, a: sa}, 34 {p: 8, n: 3, a: sa}, 35 {p: 6, n: 2, a: sa}, 36 {p: 15, n: 1, a: sb}, 37 {p: 20, n: 1, a: sa}, 38 }, 39 order: []int{3, 4, 2, 1, 0}, 40 // Index order base on seed 0: 0 0 1 0 1 0 0 41 seed: 0, 42 }, 43 { 44 txs: []txSpec{ 45 {p: 3, n: 0, a: sa}, 46 {p: 5, n: 1, a: sa}, 47 {p: 9, n: 2, a: sa}, 48 {p: 6, n: 0, a: sb}, 49 {p: 5, n: 1, a: sb}, 50 {p: 8, n: 2, a: sb}, 51 }, 52 order: []int{3, 4, 0, 5, 1, 2}, 53 // Index order base on seed 0: 0 0 1 0 1 0 0 54 seed: 0, 55 }, 56 { 57 txs: []txSpec{ 58 {p: 21, n: 4, a: sa}, 59 {p: 15, n: 1, a: sb}, 60 {p: 20, n: 1, a: sa}, 61 }, 62 order: []int{1, 2, 0}, 63 // Index order base on seed 0: 0 0 1 0 1 0 0 64 seed: 0, 65 }, 66 { 67 txs: []txSpec{ 68 {p: 50, n: 3, a: sa}, 69 {p: 30, n: 2, a: sa}, 70 {p: 10, n: 1, a: sa}, 71 {p: 15, n: 1, a: sb}, 72 {p: 21, n: 2, a: sb}, 73 }, 74 order: []int{3, 4, 2, 1, 0}, 75 // Index order base on seed 0: 0 0 1 0 1 0 0 76 seed: 0, 77 }, 78 { 79 txs: []txSpec{ 80 {p: 50, n: 3, a: sa}, 81 {p: 10, n: 2, a: sa}, 82 {p: 99, n: 1, a: sa}, 83 {p: 15, n: 1, a: sb}, 84 {p: 8, n: 2, a: sb}, 85 }, 86 order: []int{3, 4, 2, 1, 0}, 87 // Index order base on seed 0: 0 0 1 0 1 0 0 88 seed: 0, 89 }, 90 { 91 txs: []txSpec{ 92 {p: 30, a: sa, n: 2}, 93 {p: 20, a: sb, n: 1}, 94 {p: 15, a: sa, n: 1}, 95 {p: 10, a: sa, n: 0}, 96 {p: 8, a: sb, n: 0}, 97 {p: 6, a: sa, n: 3}, 98 {p: 4, a: sb, n: 3}, 99 }, 100 order: []int{4, 1, 3, 6, 2, 0, 5}, 101 // Index order base on seed 0: 0 0 1 0 1 0 1 1 0 102 seed: 0, 103 }, 104 { 105 txs: []txSpec{ 106 {p: 6, n: 1, a: sa}, 107 {p: 10, n: 2, a: sa}, 108 {p: 5, n: 1, a: sb}, 109 {p: 99, n: 2, a: sb}, 110 }, 111 order: []int{2, 3, 0, 1}, 112 // Index order base on seed 0: 0 0 1 0 1 0 1 1 0 113 seed: 0, 114 }, 115 } 116 for i, tt := range tests { 117 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 118 pool := mempool.NewSenderNonceMempool(mempool.SenderNonceMaxTxOpt(5000), mempool.SenderNonceSeedOpt(tt.seed)) 119 // create test txs and insert into mempool 120 for i, ts := range tt.txs { 121 tx := testTx{id: i, priority: int64(ts.p), nonce: uint64(ts.n), address: ts.a} 122 c := ctx.WithPriority(tx.priority) 123 err := pool.Insert(c, tx) 124 require.NoError(t, err) 125 } 126 127 itr := pool.Select(ctx, nil) 128 orderedTxs := fetchTxs(itr, 1000) 129 var txOrder []int 130 for _, tx := range orderedTxs { 131 txOrder = append(txOrder, tx.(testTx).id) 132 } 133 for _, tx := range orderedTxs { 134 require.NoError(t, pool.Remove(tx)) 135 } 136 require.Equal(t, tt.order, txOrder) 137 require.Equal(t, 0, pool.CountTx()) 138 }) 139 } 140 } 141 142 func (s *MempoolTestSuite) TestMaxTx() { 143 t := s.T() 144 ctx := sdk.NewContext(nil, cmtproto.Header{}, false, log.NewNopLogger()) 145 accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 1) 146 mp := mempool.NewSenderNonceMempool(mempool.SenderNonceMaxTxOpt(1)) 147 148 tx := testTx{ 149 nonce: 0, 150 address: accounts[0].Address, 151 priority: rand.Int63(), 152 } 153 tx2 := testTx{ 154 nonce: 1, 155 address: accounts[0].Address, 156 priority: rand.Int63(), 157 } 158 159 // empty mempool behavior 160 require.Equal(t, 0, s.mempool.CountTx()) 161 itr := mp.Select(ctx, nil) 162 require.Nil(t, itr) 163 164 ctx = ctx.WithPriority(tx.priority) 165 err := mp.Insert(ctx, tx) 166 require.NoError(t, err) 167 ctx = ctx.WithPriority(tx.priority) 168 err = mp.Insert(ctx, tx2) 169 require.Equal(t, mempool.ErrMempoolTxMaxCapacity, err) 170 } 171 172 func (s *MempoolTestSuite) TestTxNotFoundOnSender() { 173 t := s.T() 174 ctx := sdk.NewContext(nil, cmtproto.Header{}, false, log.NewNopLogger()) 175 accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 1) 176 mp := mempool.NewSenderNonceMempool(mempool.SenderNonceMaxTxOpt(5000)) 177 178 txSender := testTx{ 179 nonce: 0, 180 address: accounts[0].Address, 181 priority: rand.Int63(), 182 } 183 184 tx := testTx{ 185 nonce: 1, 186 address: accounts[0].Address, 187 priority: rand.Int63(), 188 } 189 190 ctx = ctx.WithPriority(tx.priority) 191 err := mp.Insert(ctx, txSender) 192 require.NoError(t, err) 193 err = mp.Remove(tx) 194 require.Equal(t, mempool.ErrTxNotFound, err) 195 }