forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_l1_chain_config_test.go
More file actions
139 lines (115 loc) · 3.77 KB
/
service_l1_chain_config_test.go
File metadata and controls
139 lines (115 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package opnode
import (
"encoding/json"
"math/big"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
nodeflags "github.com/ethereum-optimism/optimism/op-node/flags"
)
func TestNewL1ChainConfig_KnownChains(t *testing.T) {
logger := log.New()
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
t.Run("mainnet", func(t *testing.T) {
cfg, err := NewL1ChainConfig(new(big.Int).Set(params.MainnetChainConfig.ChainID), ctx, logger)
require.NoError(t, err)
require.Equal(t, params.MainnetChainConfig, cfg)
})
t.Run("sepolia", func(t *testing.T) {
cfg, err := NewL1ChainConfig(new(big.Int).Set(params.SepoliaChainConfig.ChainID), ctx, logger)
require.NoError(t, err)
require.Equal(t, params.SepoliaChainConfig, cfg)
})
}
func TestNewL1ChainConfig_CustomDirectAndEmbeddedAndNil(t *testing.T) {
logger := log.New()
testChainID := big.NewInt(424242)
// Build a minimal custom ChainConfig
custom := ¶ms.ChainConfig{
ChainID: testChainID,
BlobScheduleConfig: ¶ms.BlobScheduleConfig{},
}
customFaulty := ¶ms.ChainConfig{
ChainID: testChainID,
BlobScheduleConfig: nil,
}
// Indicative of an L2 chain config
// Which is what L3 chains need to provide
customOPStack := ¶ms.ChainConfig{
ChainID: testChainID,
BlobScheduleConfig: nil,
Optimism: ¶ms.OptimismConfig{},
}
// Prepare temp dir
dir := t.TempDir()
encode := func(path string, cfg any) {
f, err := os.Create(path)
require.NoError(t, err)
enc := json.NewEncoder(f)
err = enc.Encode(cfg)
require.NoError(t, err)
require.NoError(t, f.Close())
}
// Direct JSON file containing a ChainConfig
directPath := filepath.Join(dir, "chainconfig.json")
encode(directPath, custom)
directFaultyPath := filepath.Join(dir, "chainconfig_faulty.json")
encode(directFaultyPath, customFaulty)
// Embedded JSON file that contains { "config": <ChainConfig> }
embeddedPath := filepath.Join(dir, "genesis_like.json")
type wrapper struct {
Config *params.ChainConfig `json:"config"`
}
encode(embeddedPath, wrapper{Config: custom})
// Embedded JSON file that contains { "config": <ChainConfig> }
customOPStackPath := filepath.Join(dir, "optimism_chain.json")
encode(customOPStackPath, wrapper{Config: customOPStack})
// Helper to run the CLI with a given file path
runWithPath := func(path string) (*params.ChainConfig, error) {
app := cli.NewApp()
app.Flags = []cli.Flag{nodeflags.L1ChainConfig}
var out *params.ChainConfig
app.Action = func(ctx *cli.Context) error {
cfg, err := NewL1ChainConfig(testChainID, ctx, logger)
out = cfg
return err
}
// run with arg: --rollup.l1-chain-config <path>
err := app.Run([]string{"op-node", "--" + nodeflags.L1ChainConfig.Name, path})
return out, err
}
t.Run("custom-direct", func(t *testing.T) {
cfg, err := runWithPath(directPath)
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, custom.ChainID, cfg.ChainID)
})
t.Run("custom-embedded", func(t *testing.T) {
cfg, err := runWithPath(embeddedPath)
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, custom.ChainID, cfg.ChainID)
})
t.Run("nil-chainid-panics", func(t *testing.T) {
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
require.Panics(t, func() {
_, _ = NewL1ChainConfig(nil, ctx, logger)
})
})
t.Run("nil-blob-schedule-config-returns-error", func(t *testing.T) {
cfg, err := runWithPath(directFaultyPath)
require.Nil(t, cfg)
require.Error(t, err)
})
t.Run("nil-blob-schedule-config-returns-no-error-for-l2-chain-config", func(t *testing.T) {
cfg, err := runWithPath(customOPStackPath)
require.NotNil(t, cfg)
require.NoError(t, err)
})
}