Skip to content

Commit 3a7e727

Browse files
Additional tests.
1 parent 42abafc commit 3a7e727

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

txcache/selection_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package txcache
22

33
import (
4+
"bytes"
45
"fmt"
56
"math"
67
"math/big"
78
"testing"
89
"time"
910

1011
"github.com/multiversx/mx-chain-core-go/core"
12+
"github.com/multiversx/mx-chain-core-go/data"
1113
"github.com/multiversx/mx-chain-storage-go/testscommon/txcachemocks"
1214
"github.com/stretchr/testify/require"
1315
)
@@ -221,6 +223,34 @@ func TestTxCache_SelectTransactions_HandlesNotExecutableTransactions(t *testing.
221223
require.Len(t, sorted, expectedNumSelected)
222224
require.Equal(t, 200000, int(accumulatedGas))
223225
})
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+
})
224254
}
225255

226256
func TestTxCache_SelectTransactions_WhenTransactionsAddedInReversedNonceOrder(t *testing.T) {

txcache/testutils_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ func (wrappedTx *WrappedTransaction) withSize(size uint64) *WrappedTransaction {
155155
return wrappedTx
156156
}
157157

158+
func (wrappedTx *WrappedTransaction) withData(data []byte) *WrappedTransaction {
159+
tx := wrappedTx.Tx.(*transaction.Transaction)
160+
tx.Data = data
161+
wrappedTx.Size = int64(len(data)) + int64(estimatedSizeOfBoundedTxFields)
162+
return wrappedTx
163+
}
164+
158165
func (wrappedTx *WrappedTransaction) withDataLength(dataLength int) *WrappedTransaction {
159166
tx := wrappedTx.Tx.(*transaction.Transaction)
160167
tx.Data = make([]byte, dataLength)

0 commit comments

Comments
 (0)