|
4 | 4 | "fmt" |
5 | 5 | "math/rand" |
6 | 6 | "testing" |
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/require" |
9 | 10 |
|
@@ -192,3 +193,67 @@ func (s *MempoolTestSuite) TestTxNotFoundOnSender() { |
192 | 193 | err = mp.Remove(tx) |
193 | 194 | require.Equal(t, mempool.ErrTxNotFound, err) |
194 | 195 | } |
| 196 | + |
| 197 | +func (s *MempoolTestSuite) TestUnorderedTx() { |
| 198 | + t := s.T() |
| 199 | + |
| 200 | + ctx := sdk.NewContext(nil, false, log.NewNopLogger()) |
| 201 | + accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 2) |
| 202 | + sa := accounts[0].Address |
| 203 | + sb := accounts[1].Address |
| 204 | + |
| 205 | + mp := mempool.NewSenderNonceMempool(mempool.SenderNonceMaxTxOpt(5000)) |
| 206 | + |
| 207 | + now := time.Now() |
| 208 | + oneHour := now.Add(1 * time.Hour) |
| 209 | + thirtyMin := now.Add(30 * time.Minute) |
| 210 | + twoHours := now.Add(2 * time.Hour) |
| 211 | + fifteenMin := now.Add(15 * time.Minute) |
| 212 | + |
| 213 | + txs := []testTx{ |
| 214 | + {id: 0, address: sa, timeout: &oneHour, unordered: true}, |
| 215 | + {id: 1, address: sa, timeout: &thirtyMin, unordered: true}, |
| 216 | + {id: 2, address: sb, timeout: &twoHours, unordered: true}, |
| 217 | + {id: 3, address: sb, timeout: &fifteenMin, unordered: true}, |
| 218 | + } |
| 219 | + |
| 220 | + for _, tx := range txs { |
| 221 | + c := ctx.WithPriority(tx.priority) |
| 222 | + require.NoError(t, mp.Insert(c, tx)) |
| 223 | + } |
| 224 | + |
| 225 | + require.Equal(t, 4, mp.CountTx()) |
| 226 | + |
| 227 | + orderedTxs := fetchTxs(mp.Select(ctx, nil), 100000) |
| 228 | + require.Equal(t, len(txs), len(orderedTxs)) |
| 229 | + |
| 230 | + // Because the sender is selected randomly it can be any of these options |
| 231 | + acceptableOptions := [][]int{ |
| 232 | + {3, 1, 2, 0}, |
| 233 | + {3, 1, 0, 2}, |
| 234 | + {3, 2, 1, 0}, |
| 235 | + {1, 3, 0, 2}, |
| 236 | + {1, 3, 2, 0}, |
| 237 | + {1, 0, 3, 2}, |
| 238 | + } |
| 239 | + |
| 240 | + orderedTxsIds := make([]int, len(orderedTxs)) |
| 241 | + for i, tx := range orderedTxs { |
| 242 | + orderedTxsIds[i] = tx.(testTx).id |
| 243 | + } |
| 244 | + |
| 245 | + anyAcceptableOrder := false |
| 246 | + for _, option := range acceptableOptions { |
| 247 | + for i, tx := range orderedTxs { |
| 248 | + if tx.(testTx).id != txs[option[i]].id { |
| 249 | + break |
| 250 | + } |
| 251 | + |
| 252 | + if i == len(orderedTxs)-1 { |
| 253 | + anyAcceptableOrder = true |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + require.True(t, anyAcceptableOrder, "expected any of %v but got %v", acceptableOptions, orderedTxsIds) |
| 259 | +} |
0 commit comments