|
1 | 1 | package txcache
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "fmt"
|
5 | 6 | "math"
|
6 | 7 | "math/big"
|
7 | 8 | "testing"
|
8 | 9 | "time"
|
9 | 10 |
|
10 | 11 | "github.com/multiversx/mx-chain-core-go/core"
|
| 12 | + "github.com/multiversx/mx-chain-core-go/data" |
11 | 13 | "github.com/multiversx/mx-chain-storage-go/testscommon/txcachemocks"
|
12 | 14 | "github.com/stretchr/testify/require"
|
13 | 15 | )
|
@@ -221,6 +223,34 @@ func TestTxCache_SelectTransactions_HandlesNotExecutableTransactions(t *testing.
|
221 | 223 | require.Len(t, sorted, expectedNumSelected)
|
222 | 224 | require.Equal(t, 200000, int(accumulatedGas))
|
223 | 225 | })
|
| 226 | + |
| 227 | + t.Run("with badly guarded", func(t *testing.T) { |
| 228 | + cache := newUnconstrainedCacheToTest() |
| 229 | + session := txcachemocks.NewSelectionSessionMock() |
| 230 | + session.SetNonce([]byte("alice"), 1) |
| 231 | + session.SetNonce([]byte("bob"), 42) |
| 232 | + |
| 233 | + session.IsBadlyGuardedCalled = func(tx data.TransactionHandler) bool { |
| 234 | + if bytes.Equal(tx.GetData(), []byte("t")) { |
| 235 | + return true |
| 236 | + } |
| 237 | + |
| 238 | + return false |
| 239 | + } |
| 240 | + |
| 241 | + cache.AddTx(createTx([]byte("hash-alice-1"), "alice", 1).withData([]byte("x")).withGasLimit(100000)) |
| 242 | + cache.AddTx(createTx([]byte("hash-bob-42a"), "bob", 42).withData([]byte("y")).withGasLimit(100000)) |
| 243 | + cache.AddTx(createTx([]byte("hash-bob-43a"), "bob", 43).withData([]byte("z")).withGasLimit(100000)) |
| 244 | + cache.AddTx(createTx([]byte("hash-bob-43b"), "bob", 43).withData([]byte("t")).withGasLimit(100000)) |
| 245 | + |
| 246 | + sorted, accumulatedGas := cache.SelectTransactions(session, math.MaxUint64, math.MaxInt, selectionLoopMaximumDuration) |
| 247 | + require.Len(t, sorted, 3) |
| 248 | + require.Equal(t, 300000, int(accumulatedGas)) |
| 249 | + |
| 250 | + require.Equal(t, "hash-alice-1", string(sorted[0].TxHash)) |
| 251 | + require.Equal(t, "hash-bob-42a", string(sorted[1].TxHash)) |
| 252 | + require.Equal(t, "hash-bob-43a", string(sorted[2].TxHash)) |
| 253 | + }) |
224 | 254 | }
|
225 | 255 |
|
226 | 256 | func TestTxCache_SelectTransactions_WhenTransactionsAddedInReversedNonceOrder(t *testing.T) {
|
|
0 commit comments