From 6e8e074b78cc65fdd5b8b8f3d6294a6aac7a82ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 5 Nov 2024 23:06:36 +0200 Subject: [PATCH] Additional tests. --- txcache/score_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/txcache/score_test.go b/txcache/score_test.go index a98dac18..73841ea9 100644 --- a/txcache/score_test.go +++ b/txcache/score_test.go @@ -71,6 +71,91 @@ func TestDefaultScoreComputer_computeRawScoreOfTxListForSender(t *testing.T) { require.InDelta(t, float64(12.4595615805), rawScore, delta) } +func TestDefaultScoreComputer_computeRawScoreOfTxListForSender_realWorld(t *testing.T) { + txGasHandler, txFeeHelper := dummyParamsWithGasPrice(oneBillion) + computer := newDefaultScoreComputer(txFeeHelper) + + t.Run("a", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("a"), ".", 1, 1000, 50000, oneBillion), txGasHandler, txFeeHelper) + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 500, 100000, oneBillion), txGasHandler, txFeeHelper) + list.AddTx(createTxWithParams([]byte("c"), ".", 1, 500, 100000, oneBillion), txGasHandler, txFeeHelper) + + require.Equal(t, 12, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("b", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("a"), ".", 1, 1000, 50000, 2*oneBillion), txGasHandler, txFeeHelper) + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 500, 100000, 2*oneBillion), txGasHandler, txFeeHelper) + list.AddTx(createTxWithParams([]byte("c"), ".", 1, 500, 100000, 2*oneBillion), txGasHandler, txFeeHelper) + + require.Equal(t, 53, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("c", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("a"), ".", 1, 1000, 50000, 3*oneBillion), txGasHandler, txFeeHelper) + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 500, 100000, 3*oneBillion), txGasHandler, txFeeHelper) + + require.Equal(t, 98, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("d", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 500, 100000, 5*oneBillion), txGasHandler, txFeeHelper) + + require.Equal(t, 99, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("e", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 500, 600_000_000, 1*oneBillion), txGasHandler, txFeeHelper) + + require.Equal(t, 15, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("f", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 200, 600_000_000, 1*oneBillion), txGasHandler, txFeeHelper) + + // Ideally, should have been much lower. + require.Equal(t, 15, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("g", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 300, 100_000_000, 1*oneBillion), txGasHandler, txFeeHelper) + + // Ideally, should have been much lower. + require.Equal(t, 15, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("h", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 300, 100_000_000, 100*oneBillion), txGasHandler, txFeeHelper) + + // This is completely wrong. + require.Equal(t, 0, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("i", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 300, 50_000_000, 90*oneBillion), txGasHandler, txFeeHelper) + + // This is completely wrong. + require.Equal(t, 0, int(computer.computeScore(list.getScoreParams()))) + }) + + t.Run("j", func(t *testing.T) { + list := newUnconstrainedListToTest() + list.AddTx(createTxWithParams([]byte("b"), ".", 1, 300, 10_000_000, 101*oneBillion), txGasHandler, txFeeHelper) + + // This is completely wrong. + require.Equal(t, 4, int(computer.computeScore(list.getScoreParams()))) + }) +} + func TestDefaultScoreComputer_scoreFluctuatesDeterministicallyWhileTxListForSenderMutates(t *testing.T) { txGasHandler, txFeeHelper := dummyParamsWithGasPrice(oneBillion) computer := newDefaultScoreComputer(txFeeHelper)