|
| 1 | +// Copyright 2025 The op-geth Authors |
| 2 | +// This file is part of the op-geth library. |
| 3 | +// |
| 4 | +// The op-geth library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The op-geth library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the op-geth library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package legacypool |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/ecdsa" |
| 21 | + "math/big" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/common" |
| 25 | + "github.com/ethereum/go-ethereum/core" |
| 26 | + "github.com/ethereum/go-ethereum/core/txpool" |
| 27 | + "github.com/ethereum/go-ethereum/core/types" |
| 28 | + "github.com/ethereum/go-ethereum/params" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func setupOPStackPool() (*LegacyPool, *ecdsa.PrivateKey) { |
| 33 | + return setupPoolWithConfig(params.OptimismTestConfig) |
| 34 | +} |
| 35 | + |
| 36 | +func setupTestL1FeeParams(t *testing.T, pool *LegacyPool) { |
| 37 | + l1FeeScalars := common.Hash{19: 1} // smallest possible base fee scalar |
| 38 | + // sanity check |
| 39 | + l1BaseFeeScalar, l1BlobBaseFeeScalar := types.ExtractEcotoneFeeParams(l1FeeScalars[:]) |
| 40 | + require.EqualValues(t, 1, l1BaseFeeScalar.Uint64()) |
| 41 | + require.Zero(t, l1BlobBaseFeeScalar.Sign()) |
| 42 | + pool.currentState.SetState(types.L1BlockAddr, types.L1FeeScalarsSlot, l1FeeScalars) |
| 43 | + l1BaseFee := big.NewInt(1e6) // to account for division by 1e12 in L1 cost |
| 44 | + pool.currentState.SetState(types.L1BlockAddr, types.L1BaseFeeSlot, common.BigToHash(l1BaseFee)) |
| 45 | + // sanity checks |
| 46 | + require.Equal(t, l1BaseFee, pool.currentState.GetState(types.L1BlockAddr, types.L1BaseFeeSlot).Big()) |
| 47 | +} |
| 48 | + |
| 49 | +func setupTestOperatorFeeParams(opFeeConst byte) func(t *testing.T, pool *LegacyPool) { |
| 50 | + return func(t *testing.T, pool *LegacyPool) { |
| 51 | + opFeeParams := common.Hash{31: opFeeConst} // 0 scalar |
| 52 | + // sanity check |
| 53 | + s, c := types.ExtractOperatorFeeParams(opFeeParams) |
| 54 | + require.Zero(t, s.Sign()) |
| 55 | + require.EqualValues(t, opFeeConst, c.Uint64()) |
| 56 | + pool.currentState.SetState(types.L1BlockAddr, types.OperatorFeeParamsSlot, opFeeParams) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestInvalidRollupTransactions(t *testing.T) { |
| 61 | + t.Run("zero-rollup-cost", func(t *testing.T) { |
| 62 | + testInvalidRollupTransactions(t, nil) |
| 63 | + }) |
| 64 | + |
| 65 | + t.Run("l1-cost", func(t *testing.T) { |
| 66 | + testInvalidRollupTransactions(t, setupTestL1FeeParams) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("operator-cost", func(t *testing.T) { |
| 70 | + testInvalidRollupTransactions(t, setupTestOperatorFeeParams(1)) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func testInvalidRollupTransactions(t *testing.T, stateMod func(t *testing.T, pool *LegacyPool)) { |
| 75 | + t.Parallel() |
| 76 | + |
| 77 | + pool, key := setupOPStackPool() |
| 78 | + defer pool.Close() |
| 79 | + |
| 80 | + const gasLimit = 100_000 |
| 81 | + tx := transaction(0, gasLimit, key) |
| 82 | + from, _ := deriveSender(tx) |
| 83 | + |
| 84 | + // base fee is 1 |
| 85 | + testAddBalance(pool, from, new(big.Int).Add(big.NewInt(gasLimit), tx.Value())) |
| 86 | + // we add the test variant with zero rollup cost as a sanity check that the tx would indeed be valid |
| 87 | + if stateMod == nil { |
| 88 | + require.NoError(t, pool.addRemote(tx)) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + // Now we cause insufficient funds error due to rollup cost |
| 93 | + stateMod(t, pool) |
| 94 | + |
| 95 | + rcost := pool.rollupCostFn(tx) |
| 96 | + require.Equal(t, 1, rcost.Sign(), "rollup cost must be >0") |
| 97 | + |
| 98 | + require.ErrorIs(t, pool.addRemote(tx), core.ErrInsufficientFunds) |
| 99 | +} |
| 100 | + |
| 101 | +func TestRollupTransactionCostAccounting(t *testing.T) { |
| 102 | + t.Run("zero-rollup-cost", func(t *testing.T) { |
| 103 | + testRollupTransactionCostAccounting(t, nil) |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("l1-cost", func(t *testing.T) { |
| 107 | + testRollupTransactionCostAccounting(t, setupTestL1FeeParams) |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("operator-cost", func(t *testing.T) { |
| 111 | + testRollupTransactionCostAccounting(t, setupTestOperatorFeeParams(1)) |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func testRollupTransactionCostAccounting(t *testing.T, stateMod func(t *testing.T, pool *LegacyPool)) { |
| 116 | + t.Parallel() |
| 117 | + |
| 118 | + pool, key := setupOPStackPool() |
| 119 | + defer pool.Close() |
| 120 | + |
| 121 | + const gasLimit = 100_000 |
| 122 | + gasPrice0, gasPrice1 := big.NewInt(100), big.NewInt(110) |
| 123 | + tx0 := pricedTransaction(0, gasLimit, gasPrice0, key) |
| 124 | + tx1 := pricedTransaction(0, gasLimit, gasPrice1, key) |
| 125 | + from, _ := deriveSender(tx0) |
| 126 | + |
| 127 | + require.NotNil(t, pool.rollupCostFn) |
| 128 | + |
| 129 | + if stateMod != nil { |
| 130 | + stateMod(t, pool) |
| 131 | + } |
| 132 | + |
| 133 | + cost0, of := txpool.TotalTxCost(tx0, pool.rollupCostFn) |
| 134 | + require.False(t, of) |
| 135 | + cost1, of := txpool.TotalTxCost(tx1, pool.rollupCostFn) |
| 136 | + require.False(t, of) |
| 137 | + |
| 138 | + if stateMod != nil { |
| 139 | + require.Greater(t, cost0.Uint64(), tx0.Cost().Uint64(), "tx0 total cost should be greater than regular cost") |
| 140 | + } |
| 141 | + |
| 142 | + // we add the initial tx to the pool |
| 143 | + testAddBalance(pool, from, cost1.ToBig()) // already give enough funds for tx1 |
| 144 | + require.NoError(t, pool.addRemoteSync(tx0)) |
| 145 | + _, ok := pool.queue[from] |
| 146 | + require.False(t, ok, "tx0 should not be in queue, but pending") |
| 147 | + pending, ok := pool.pending[from] |
| 148 | + require.True(t, ok, "tx0 should be pending") |
| 149 | + require.Equal(t, cost0, pending.totalcost, "tx0 total pending cost should match") |
| 150 | + |
| 151 | + // now we add a replacement and check the accounting |
| 152 | + require.NoError(t, pool.addRemoteSync(tx1)) |
| 153 | + _, ok = pool.queue[from] |
| 154 | + require.False(t, ok, "tx1 should not be in queue, but pending") |
| 155 | + pending, ok = pool.pending[from] |
| 156 | + require.True(t, ok, "tx1 should be pending") |
| 157 | + require.Equal(t, cost1, pending.totalcost, "tx1 total pending cost should match") |
| 158 | +} |
| 159 | + |
| 160 | +// TestRollupCostFuncChange tests that changes in the underlying rollup cost parameters |
| 161 | +// are correctly picked up by the transaction pool and the underlying list implementation. |
| 162 | +func TestRollupCostFuncChange(t *testing.T) { |
| 163 | + t.Parallel() |
| 164 | + |
| 165 | + pool, key := setupOPStackPool() |
| 166 | + defer pool.Close() |
| 167 | + |
| 168 | + const gasLimit = 100_000 |
| 169 | + gasPrice := big.NewInt(100) |
| 170 | + tx0 := pricedTransaction(0, gasLimit, gasPrice, key) |
| 171 | + tx1 := pricedTransaction(1, gasLimit, gasPrice, key) |
| 172 | + from, _ := deriveSender(tx0) |
| 173 | + |
| 174 | + require.NotNil(t, pool.rollupCostFn) |
| 175 | + |
| 176 | + setupTestOperatorFeeParams(10)(t, pool) |
| 177 | + |
| 178 | + cost0, of := txpool.TotalTxCost(tx0, pool.rollupCostFn) |
| 179 | + require.False(t, of) |
| 180 | + |
| 181 | + // 1st add tx0, consuming all balance |
| 182 | + testAddBalance(pool, from, cost0.ToBig()) |
| 183 | + require.NoError(t, pool.addRemoteSync(tx0)) |
| 184 | + |
| 185 | + // 2nd add same balance but increase op fee const by 10 |
| 186 | + // so adding 2nd tx should fail with 10 missing. |
| 187 | + testAddBalance(pool, from, cost0.ToBig()) |
| 188 | + pool.reset(nil, nil) // reset the rollup cost function, simulates a head change |
| 189 | + setupTestOperatorFeeParams(20)(t, pool) |
| 190 | + require.ErrorContains(t, pool.addRemoteSync(tx1), "overshot 10") |
| 191 | + |
| 192 | + // 3rd now add missing 10, adding tx1 should succeed |
| 193 | + testAddBalance(pool, from, big.NewInt(10)) |
| 194 | + require.NoError(t, pool.addRemoteSync(tx1)) |
| 195 | +} |
0 commit comments