forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
291 lines (256 loc) · 9.73 KB
/
service.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package opnode
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
plasma "github.com/ethereum-optimism/optimism/op-plasma"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/sources"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli/v2"
"github.com/ethereum-optimism/optimism/op-node/flags"
"github.com/ethereum-optimism/optimism/op-node/node"
p2pcli "github.com/ethereum-optimism/optimism/op-node/p2p/cli"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
"github.com/ethereum-optimism/optimism/op-node/rollup/sync"
opflags "github.com/ethereum-optimism/optimism/op-service/flags"
)
// NewConfig creates a Config from the provided flags or environment variables.
func NewConfig(ctx *cli.Context, log log.Logger) (*node.Config, error) {
if err := flags.CheckRequired(ctx); err != nil {
return nil, err
}
rollupConfig, err := NewRollupConfigFromCLI(log, ctx)
if err != nil {
return nil, err
}
if !ctx.Bool(flags.RollupLoadProtocolVersions.Name) {
log.Info("Not opted in to ProtocolVersions signal loading, disabling ProtocolVersions contract now.")
rollupConfig.ProtocolVersionsAddress = common.Address{}
}
configPersistence := NewConfigPersistence(ctx)
driverConfig := NewDriverConfig(ctx)
p2pSignerSetup, err := p2pcli.LoadSignerSetup(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load p2p signer: %w", err)
}
p2pConfig, err := p2pcli.NewConfig(ctx, rollupConfig)
if err != nil {
return nil, fmt.Errorf("failed to load p2p config: %w", err)
}
l1Endpoint := NewL1EndpointConfig(ctx)
l2Endpoint, err := NewL2EndpointConfig(ctx, log)
if err != nil {
return nil, fmt.Errorf("failed to load l2 endpoints info: %w", err)
}
syncConfig, err := NewSyncConfig(ctx, log)
if err != nil {
return nil, fmt.Errorf("failed to create the sync config: %w", err)
}
haltOption := ctx.String(flags.RollupHalt.Name)
if haltOption == "none" {
haltOption = ""
}
cfg := &node.Config{
L1: l1Endpoint,
L2: l2Endpoint,
Rollup: *rollupConfig,
Driver: *driverConfig,
Beacon: NewBeaconEndpointConfig(ctx),
RPC: node.RPCConfig{
ListenAddr: ctx.String(flags.RPCListenAddr.Name),
ListenPort: ctx.Int(flags.RPCListenPort.Name),
EnableAdmin: ctx.Bool(flags.RPCEnableAdmin.Name),
},
Metrics: node.MetricsConfig{
Enabled: ctx.Bool(flags.MetricsEnabledFlag.Name),
ListenAddr: ctx.String(flags.MetricsAddrFlag.Name),
ListenPort: ctx.Int(flags.MetricsPortFlag.Name),
},
Pprof: oppprof.ReadCLIConfig(ctx),
P2P: p2pConfig,
P2PSigner: p2pSignerSetup,
L1EpochPollInterval: ctx.Duration(flags.L1EpochPollIntervalFlag.Name),
RuntimeConfigReloadInterval: ctx.Duration(flags.RuntimeConfigReloadIntervalFlag.Name),
Heartbeat: node.HeartbeatConfig{
Enabled: ctx.Bool(flags.HeartbeatEnabledFlag.Name),
Moniker: ctx.String(flags.HeartbeatMonikerFlag.Name),
URL: ctx.String(flags.HeartbeatURLFlag.Name),
},
ConfigPersistence: configPersistence,
SafeDBPath: ctx.String(flags.SafeDBPath.Name),
Sync: *syncConfig,
RollupHalt: haltOption,
RethDBPath: ctx.String(flags.L1RethDBPath.Name),
ConductorEnabled: ctx.Bool(flags.ConductorEnabledFlag.Name),
ConductorRpc: ctx.String(flags.ConductorRpcFlag.Name),
ConductorRpcTimeout: ctx.Duration(flags.ConductorRpcTimeoutFlag.Name),
Plasma: plasma.ReadCLIConfig(ctx),
}
if err := cfg.LoadPersisted(log); err != nil {
return nil, fmt.Errorf("failed to load driver config: %w", err)
}
// conductor controls the sequencer state
if cfg.ConductorEnabled {
cfg.Driver.SequencerStopped = true
}
if err := cfg.Check(); err != nil {
return nil, err
}
return cfg, nil
}
func NewBeaconEndpointConfig(ctx *cli.Context) node.L1BeaconEndpointSetup {
return &node.L1BeaconEndpointConfig{
BeaconAddr: ctx.String(flags.BeaconAddr.Name),
BeaconHeader: ctx.String(flags.BeaconHeader.Name),
BeaconArchiverAddr: ctx.String(flags.BeaconArchiverAddr.Name),
BeaconCheckIgnore: ctx.Bool(flags.BeaconCheckIgnore.Name),
BeaconFetchAllSidecars: ctx.Bool(flags.BeaconFetchAllSidecars.Name),
}
}
func NewL1EndpointConfig(ctx *cli.Context) *node.L1EndpointConfig {
return &node.L1EndpointConfig{
L1NodeAddr: ctx.String(flags.L1NodeAddr.Name),
L1TrustRPC: ctx.Bool(flags.L1TrustRPC.Name),
L1RPCKind: sources.RPCProviderKind(strings.ToLower(ctx.String(flags.L1RPCProviderKind.Name))),
RateLimit: ctx.Float64(flags.L1RPCRateLimit.Name),
BatchSize: ctx.Int(flags.L1RPCMaxBatchSize.Name),
HttpPollInterval: ctx.Duration(flags.L1HTTPPollInterval.Name),
MaxConcurrency: ctx.Int(flags.L1RPCMaxConcurrency.Name),
}
}
func NewL2EndpointConfig(ctx *cli.Context, log log.Logger) (*node.L2EndpointConfig, error) {
l2Addr := ctx.String(flags.L2EngineAddr.Name)
fileName := ctx.String(flags.L2EngineJWTSecret.Name)
var secret [32]byte
fileName = strings.TrimSpace(fileName)
if fileName == "" {
return nil, fmt.Errorf("file-name of jwt secret is empty")
}
if data, err := os.ReadFile(fileName); err == nil {
jwtSecret := common.FromHex(strings.TrimSpace(string(data)))
if len(jwtSecret) != 32 {
return nil, fmt.Errorf("invalid jwt secret in path %s, not 32 hex-formatted bytes", fileName)
}
copy(secret[:], jwtSecret)
} else {
log.Warn("Failed to read JWT secret from file, generating a new one now. Configure L2 geth with --authrpc.jwt-secret=" + fmt.Sprintf("%q", fileName))
if _, err := io.ReadFull(rand.Reader, secret[:]); err != nil {
return nil, fmt.Errorf("failed to generate jwt secret: %w", err)
}
if err := os.WriteFile(fileName, []byte(hexutil.Encode(secret[:])), 0o600); err != nil {
return nil, err
}
}
return &node.L2EndpointConfig{
L2EngineAddr: l2Addr,
L2EngineJWTSecret: secret,
}, nil
}
func NewConfigPersistence(ctx *cli.Context) node.ConfigPersistence {
stateFile := ctx.String(flags.RPCAdminPersistence.Name)
if stateFile == "" {
return node.DisabledConfigPersistence{}
}
return node.NewConfigPersistence(stateFile)
}
func NewDriverConfig(ctx *cli.Context) *driver.Config {
return &driver.Config{
VerifierConfDepth: ctx.Uint64(flags.VerifierL1Confs.Name),
SequencerConfDepth: ctx.Uint64(flags.SequencerL1Confs.Name),
SequencerEnabled: ctx.Bool(flags.SequencerEnabledFlag.Name),
SequencerStopped: ctx.Bool(flags.SequencerStoppedFlag.Name),
SequencerMaxSafeLag: ctx.Uint64(flags.SequencerMaxSafeLagFlag.Name),
}
}
func NewRollupConfigFromCLI(log log.Logger, ctx *cli.Context) (*rollup.Config, error) {
network := ctx.String(opflags.NetworkFlagName)
rollupConfigPath := ctx.String(opflags.RollupConfigFlagName)
if ctx.Bool(flags.BetaExtraNetworks.Name) {
log.Warn("The beta.extra-networks flag is deprecated and can be omitted safely.")
}
rollupConfig, err := NewRollupConfig(log, network, rollupConfigPath)
if err != nil {
return nil, err
}
applyOverrides(ctx, rollupConfig)
return rollupConfig, nil
}
func NewRollupConfig(log log.Logger, network string, rollupConfigPath string) (*rollup.Config, error) {
if network != "" {
if rollupConfigPath != "" {
log.Error(`Cannot configure network and rollup-config at the same time.
Startup will proceed to use the network-parameter and ignore the rollup config.
Conflicting configuration is deprecated, and will stop the op-node from starting in the future.
`, "network", network, "rollup_config", rollupConfigPath)
}
rollupConfig, err := chaincfg.GetRollupConfig(network)
if err != nil {
return nil, err
}
return rollupConfig, nil
}
file, err := os.Open(rollupConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to read rollup config: %w", err)
}
defer file.Close()
var rollupConfig rollup.Config
if err := json.NewDecoder(file).Decode(&rollupConfig); err != nil {
return nil, fmt.Errorf("failed to decode rollup config: %w", err)
}
return &rollupConfig, nil
}
func applyOverrides(ctx *cli.Context, rollupConfig *rollup.Config) {
if ctx.IsSet(opflags.CanyonOverrideFlagName) {
canyon := ctx.Uint64(opflags.CanyonOverrideFlagName)
rollupConfig.CanyonTime = &canyon
}
if ctx.IsSet(opflags.DeltaOverrideFlagName) {
delta := ctx.Uint64(opflags.DeltaOverrideFlagName)
rollupConfig.DeltaTime = &delta
}
if ctx.IsSet(opflags.EcotoneOverrideFlagName) {
ecotone := ctx.Uint64(opflags.EcotoneOverrideFlagName)
rollupConfig.EcotoneTime = &ecotone
}
}
func NewSnapshotLogger(ctx *cli.Context) (log.Logger, error) {
snapshotFile := ctx.String(flags.SnapshotLog.Name)
if snapshotFile == "" {
return log.NewLogger(log.DiscardHandler()), nil
}
sf, err := os.OpenFile(snapshotFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
handler := log.JSONHandler(sf)
return log.NewLogger(handler), nil
}
func NewSyncConfig(ctx *cli.Context, log log.Logger) (*sync.Config, error) {
if ctx.IsSet(flags.L2EngineSyncEnabled.Name) && ctx.IsSet(flags.SyncModeFlag.Name) {
return nil, errors.New("cannot set both --l2.engine-sync and --syncmode at the same time.")
} else if ctx.IsSet(flags.L2EngineSyncEnabled.Name) {
log.Error("l2.engine-sync is deprecated and will be removed in a future release. Use --syncmode=execution-layer instead.")
}
mode, err := sync.StringToMode(ctx.String(flags.SyncModeFlag.Name))
if err != nil {
return nil, err
}
cfg := &sync.Config{
SyncMode: mode,
SkipSyncStartCheck: ctx.Bool(flags.SkipSyncStartCheck.Name),
}
if ctx.Bool(flags.L2EngineSyncEnabled.Name) {
cfg.SyncMode = sync.ELSync
}
return cfg, nil
}