From 00adab2de46a1089739562dd5651f2a7112c5d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 25 Nov 2024 19:59:18 +0200 Subject: [PATCH] Additional unit tests. --- txcache/transactionsHeapItem_test.go | 43 ++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/txcache/transactionsHeapItem_test.go b/txcache/transactionsHeapItem_test.go index dffa9c4b..96f7afdc 100644 --- a/txcache/transactionsHeapItem_test.go +++ b/txcache/transactionsHeapItem_test.go @@ -139,17 +139,22 @@ func TestTransactionsHeapItem_detectWillFeeExceedBalance(t *testing.T) { a := createTx([]byte("tx-1"), "alice", 42) b := createTx([]byte("tx-2"), "alice", 43) + c := createTx([]byte("tx-3"), "alice", 44).withValue(big.NewInt(1000000000000000000)) + d := createTx([]byte("tx-4"), "alice", 45) + a.precomputeFields(txGasHandler) b.precomputeFields(txGasHandler) + c.precomputeFields(txGasHandler) + d.precomputeFields(txGasHandler) t.Run("unknown", func(t *testing.T) { item, err := newTransactionsHeapItem(bunchOfTransactions{a, b}) - require.NoError(t, err) + require.NoError(t, err) require.False(t, item.detectWillFeeExceedBalance()) }) - t.Run("known, not exceeded, then exceeded", func(t *testing.T) { + t.Run("known, not exceeded, then exceeded (a)", func(t *testing.T) { session := txcachemocks.NewSelectionSessionMock() item, err := newTransactionsHeapItem(bunchOfTransactions{a, b}) @@ -163,8 +168,42 @@ func TestTransactionsHeapItem_detectWillFeeExceedBalance(t *testing.T) { _ = item.selectCurrentTransaction(session) _ = item.gotoNextTransaction() + require.Equal(t, "50000000000000", item.consumedBalance.String()) + require.True(t, item.detectWillFeeExceedBalance()) + }) + + t.Run("known, not exceeded, then exceeded (b)", func(t *testing.T) { + session := txcachemocks.NewSelectionSessionMock() + + item, err := newTransactionsHeapItem(bunchOfTransactions{a, b, c, d}) + require.NoError(t, err) + + item.senderState = &types.AccountState{ + Balance: big.NewInt(1000000000000000000 + 2*50000000000000 + 1), + } + + require.False(t, item.detectWillFeeExceedBalance()) + + // Select "a", move to "b". + _ = item.selectCurrentTransaction(session) + _ = item.gotoNextTransaction() + + require.Equal(t, "50000000000000", item.consumedBalance.String()) + require.False(t, item.detectWillFeeExceedBalance()) + + // Select "b", move to "c". + _ = item.selectCurrentTransaction(session) + _ = item.gotoNextTransaction() + + require.Equal(t, "100000000000000", item.consumedBalance.String()) + require.False(t, item.detectWillFeeExceedBalance()) + + // Select "c", move to "d". + _ = item.selectCurrentTransaction(session) + _ = item.gotoNextTransaction() + require.Equal(t, "1000150000000000000", item.consumedBalance.String()) require.True(t, item.detectWillFeeExceedBalance()) }) }