Skip to content

Commit 21c5afb

Browse files
author
WorldDogs
committed
refactor: streamline rollup configuration in tests
- Renamed the rollup configuration variable for clarity. - Simplified the setup of rollup instances in test cases by directly using the default configuration. - Enhanced readability by reducing redundancy in the test setup code.
1 parent c7d0d12 commit 21c5afb

File tree

2 files changed

+51
-80
lines changed

2 files changed

+51
-80
lines changed

tx-submitter/services/rollup_handle_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func setupTestRollup(t *testing.T) (*Rollup, *mock.L1ClientWrapper, *mock.L2Clie
7575
l1Staking.SetActiveStakers(testStakers)
7676

7777
// Create rollup config
78-
cfg := utils.Config{
78+
defaultCfg := utils.Config{
7979
MaxTip: 10e9,
8080
MaxBaseFee: 100e9,
8181
MinTip: 1e9,
@@ -99,21 +99,21 @@ func setupTestRollup(t *testing.T) (*Rollup, *mock.L1ClientWrapper, *mock.L2Clie
9999
rollup := NewRollup(
100100
context.Background(),
101101
metrics,
102-
nil, // l1RpcClient
103-
l1Mock, // l1Client
104-
[]iface.L2Client{l2Mock}, // l2Clients
105-
mockRollup, // rollup
106-
l1Staking, // staking
107-
big.NewInt(1), // chainId
108-
privateKey, // privKey
109-
common.Address{}, // rollupAddr
110-
rollupAbi, // abi
111-
cfg, // cfg
112-
nil, // rsaPriv
113-
rotator, // rotator
114-
nil, // ldb
115-
nil, // bm
116-
eventStorage, // eventInfoStorage
102+
nil,
103+
l1Mock,
104+
[]iface.L2Client{l2Mock},
105+
mockRollup,
106+
l1Staking,
107+
big.NewInt(1),
108+
privateKey,
109+
common.Address{},
110+
rollupAbi,
111+
defaultCfg,
112+
nil,
113+
rotator,
114+
nil,
115+
nil,
116+
eventStorage,
117117
)
118118

119119
// Initialize pending transactions

tx-submitter/services/rollup_test.go

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package services
22

33
import (
4-
"context"
54
"math/big"
65
"testing"
76

87
"github.com/holiman/uint256"
98
"github.com/morph-l2/go-ethereum/common"
109
"github.com/morph-l2/go-ethereum/core/types"
11-
"github.com/morph-l2/go-ethereum/crypto"
1210
"github.com/morph-l2/go-ethereum/crypto/kzg4844"
1311
"github.com/stretchr/testify/require"
1412

15-
"morph-l2/tx-submitter/mock"
1613
"morph-l2/tx-submitter/utils"
1714
)
1815

@@ -55,92 +52,76 @@ func TestSendTx(t *testing.T) {
5552
}
5653

5754
func TestGetGasTipAndCap(t *testing.T) {
58-
l1Mock := mock.NewL1ClientWrapper()
5955
initTip := big.NewInt(1e9)
60-
6156
baseFee := big.NewInt(1e9)
6257
block := types.NewBlockWithHeader(
6358
&types.Header{
6459
BaseFee: baseFee,
6560
},
6661
)
62+
63+
r, l1Mock, _, _ := setupTestRollup(t)
6764
l1Mock.TipCap = initTip
6865
l1Mock.Block = block
69-
config := utils.Config{
70-
MaxTip: 10e9,
71-
MaxBaseFee: 100e9,
72-
MinTip: 1e9,
73-
TipFeeBump: 100,
74-
}
75-
r := NewRollup(context.Background(), nil, nil, l1Mock, nil, nil, nil, nil, nil, common.Address{}, nil, config, nil, nil, nil, nil, nil)
66+
7667
tip, feecap, blobfee, err := r.GetGasTipAndCap()
7768
require.NoError(t, err)
7869
require.NotNil(t, tip)
7970
require.NotNil(t, feecap)
8071
require.NotNil(t, blobfee)
8172
require.Equal(t, initTip, tip)
8273

83-
config = utils.Config{
84-
MaxTip: 10e9,
85-
MaxBaseFee: 100e9,
86-
MinTip: 1e9,
87-
TipFeeBump: 200,
88-
}
89-
r = NewRollup(context.Background(), nil, nil, l1Mock, nil, nil, nil, nil, nil, common.Address{}, nil, config, nil, nil, nil, nil, nil)
74+
// Test with different TipFeeBump
75+
r, l1Mock, _, _ = setupTestRollup(t)
76+
l1Mock.TipCap = initTip
77+
l1Mock.Block = block
78+
r.cfg.TipFeeBump = 200
79+
9080
tip, feecap, blobfee, err = r.GetGasTipAndCap()
9181
require.NoError(t, err)
9282
require.NotNil(t, tip)
9383
require.NotNil(t, feecap)
9484
require.NotNil(t, blobfee)
9585
require.Equal(t, tip, initTip.Mul(initTip, big.NewInt(2)))
9686

97-
config = utils.Config{
98-
MaxTip: 10e9,
99-
MaxBaseFee: baseFee.Uint64() - 1,
100-
MinTip: 1e9,
101-
TipFeeBump: 200,
102-
}
103-
r = NewRollup(context.Background(), nil, nil, l1Mock, nil, nil, nil, nil, nil, common.Address{}, nil, config, nil, nil, nil, nil, nil)
87+
// Test with base fee too high
88+
r, l1Mock, _, _ = setupTestRollup(t)
89+
l1Mock.TipCap = initTip
90+
l1Mock.Block = block
91+
r.cfg.MaxBaseFee = baseFee.Uint64() - 1
92+
10493
_, _, _, err = r.GetGasTipAndCap()
10594
require.ErrorContains(t, err, "base fee is too high")
10695

107-
config = utils.Config{
108-
MaxTip: initTip.Uint64() - 1,
109-
MaxBaseFee: 100e9,
110-
MinTip: 1e9,
111-
TipFeeBump: 200,
112-
}
113-
r = NewRollup(context.Background(), nil, nil, l1Mock, nil, nil, nil, nil, nil, common.Address{}, nil, config, nil, nil, nil, nil, nil)
96+
// Test with tip too high
97+
r, l1Mock, _, _ = setupTestRollup(t)
98+
l1Mock.TipCap = initTip
99+
l1Mock.Block = block
100+
r.cfg.MaxTip = initTip.Uint64() - 1
101+
114102
_, _, _, err = r.GetGasTipAndCap()
115103
require.ErrorContains(t, err, "tip is too high")
116-
117104
}
118105

119106
func TestReSubmitTx(t *testing.T) {
120-
l1Mock := mock.NewL1ClientWrapper()
121107
marketTip := big.NewInt(3e9) // 3 Gwei market tip
122108
baseFee := big.NewInt(2e9) // 2 Gwei base fee
123109
block := types.NewBlockWithHeader(
124110
&types.Header{
125111
BaseFee: baseFee,
126112
},
127113
)
114+
115+
r, l1Mock, _, _ := setupTestRollup(t)
128116
l1Mock.TipCap = marketTip
129117
l1Mock.Block = block
130-
config := utils.Config{
131-
MaxTip: 10e12,
132-
MaxBaseFee: 100e9,
133-
MinTip: 1e9,
134-
TipFeeBump: 0, // no bump for replace mode
135-
}
136-
137-
priv, err := crypto.GenerateKey()
138-
require.NoError(t, err)
139-
140-
r := NewRollup(context.Background(), nil, nil, l1Mock, nil, nil, nil, nil, priv, common.Address{}, nil, config, nil, nil, nil, nil, nil)
118+
r.cfg.MaxTip = 10e12
119+
r.cfg.MaxBaseFee = 100e9
120+
r.cfg.MinTip = 1e9
121+
r.cfg.TipFeeBump = 0 // no bump for replace mode
141122

142123
// Test nil tx
143-
_, err = r.ReSubmitTx(false, nil)
124+
_, err := r.ReSubmitTx(false, nil)
144125
require.ErrorContains(t, err, "nil tx")
145126

146127
t.Run("DynamicFeeTx", func(t *testing.T) {
@@ -275,34 +256,24 @@ func TestReSubmitTx(t *testing.T) {
275256

276257
func TestCancelTx(t *testing.T) {
277258
// Setup mock L1 client
278-
l1Mock := mock.NewL1ClientWrapper()
279259
initTip := big.NewInt(1e9)
280260
baseFee := big.NewInt(1e9)
281261
block := types.NewBlockWithHeader(
282262
&types.Header{
283263
BaseFee: baseFee,
284264
},
285265
)
266+
267+
r, l1Mock, _, _ := setupTestRollup(t)
286268
l1Mock.TipCap = initTip
287269
l1Mock.Block = block
288-
289-
// Setup config
290-
config := utils.Config{
291-
MaxTip: 10e12,
292-
MaxBaseFee: 100e9,
293-
MinTip: 1e9,
294-
TipFeeBump: 120, // 20% bump
295-
}
296-
297-
// Setup private key
298-
priv, err := crypto.GenerateKey()
299-
require.NoError(t, err)
300-
301-
// Create rollup instance
302-
r := NewRollup(context.Background(), nil, nil, l1Mock, nil, nil, nil, nil, priv, common.Address{}, nil, config, nil, nil, nil, nil, nil)
270+
r.cfg.MaxTip = 10e12
271+
r.cfg.MaxBaseFee = 100e9
272+
r.cfg.MinTip = 1e9
273+
r.cfg.TipFeeBump = 120 // 20% bump
303274

304275
// Test 1: Cancel nil transaction
305-
_, err = r.CancelTx(nil)
276+
_, err := r.CancelTx(nil)
306277
require.Error(t, err)
307278
require.Contains(t, err.Error(), "nil tx")
308279

0 commit comments

Comments
 (0)