-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathnode.go
More file actions
834 lines (727 loc) · 28.5 KB
/
Copy pathnode.go
File metadata and controls
834 lines (727 loc) · 28.5 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
package node
import (
"context"
"errors"
"fmt"
"io"
"sync/atomic"
"time"
"github.com/ethereum-optimism/optimism/op-node/node/safedb"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
plasma "github.com/ethereum-optimism/optimism/op-plasma"
"github.com/ethereum-optimism/optimism/op-service/httputil"
"github.com/hashicorp/go-multierror"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum-optimism/optimism/op-node/heartbeat"
"github.com/ethereum-optimism/optimism/op-node/metrics"
"github.com/ethereum-optimism/optimism/op-node/p2p"
"github.com/ethereum-optimism/optimism/op-node/rollup/conductor"
"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
"github.com/ethereum-optimism/optimism/op-node/rollup/sync"
"github.com/ethereum-optimism/optimism/op-node/version"
"github.com/ethereum-optimism/optimism/op-service/client"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/retry"
"github.com/ethereum-optimism/optimism/op-service/sources"
)
var ErrAlreadyClosed = errors.New("node is already closed")
type closableSafeDB interface {
derive.SafeHeadListener
SafeDBReader
io.Closer
}
type OpNode struct {
log log.Logger
appVersion string
metrics *metrics.Metrics
l1HeadsSub ethereum.Subscription // Subscription to get L1 heads (automatically re-subscribes on error)
l1SafeSub ethereum.Subscription // Subscription to get L1 safe blocks, a.k.a. justified data (polling)
l1FinalizedSub ethereum.Subscription // Subscription to get L1 safe blocks, a.k.a. justified data (polling)
l1Source *sources.L1Client // L1 Client to fetch data from
l2Driver *driver.Driver // L2 Engine to Sync
l2Source *sources.EngineClient // L2 Execution Engine RPC bindings
server *rpcServer // RPC server hosting the rollup-node API
p2pNode *p2p.NodeP2P // P2P node functionality
p2pSigner p2p.Signer // p2p gossip application messages will be signed with this signer
tracer Tracer // tracer to get events for testing/debugging
runCfg *RuntimeConfig // runtime configurables
safeDB closableSafeDB
rollupHalt string // when to halt the rollup, disabled if empty
pprofService *oppprof.Service
metricsSrv *httputil.HTTPServer
l1Blob *sources.BSCBlobClient // L1 Blob Client to fetch blobs
// some resources cannot be stopped directly, like the p2p gossipsub router (not our design),
// and depend on this ctx to be closed.
resourcesCtx context.Context
resourcesClose context.CancelFunc
// Indicates when it's safe to close data sources used by the runtimeConfig bg loader
runtimeConfigReloaderDone chan struct{}
closed atomic.Bool
// deferGossipEnabled mirrors cfg.StartupDeferGossip; controls whether Start() runs the pre-gossip catch-up phase.
deferGossipEnabled bool
// gossipReady gates incoming gossip payloads during the startup catch-up phase.
// While false, gossip payloads received via OnUnsafeL2Payload are silently dropped
// to prevent the clSync queue from accumulating orphan payloads (parent != op-geth.UnsafeL2Head)
// while op-geth's unsafe head is still being advanced via L1 derivation.
// Set to true once catch-up completes (or is disabled / times out).
gossipReady atomic.Bool
// firstPayloadAllowed lets exactly one gossip payload pass through OnUnsafeL2Payload
// while gossipReady is still false. This is required when running in ELSync mode:
// the engineController initial state is syncStatusWillStartEL, which causes
// IsEngineSyncing() to return true and prevents the driver eventLoop from running
// derivation (the stepReqCh handler short-circuits with `continue`). Until at least
// one payload reaches Driver.OnUnsafeL2Payload -> InsertUnsafePayload, the engine's
// "Skipping EL sync" finalized-block check never fires and syncStatus is stuck.
// Allowing exactly one payload through unblocks that transition and lets derivation
// drive op-geth forward during the catch-up phase. After this single payload,
// subsequent payloads continue to be dropped until gossipReady is flipped to true.
firstPayloadAllowed atomic.Bool
// cancels execution prematurely, e.g. to halt. This may be nil.
cancel context.CancelCauseFunc
halted atomic.Bool
}
const (
// catchupLagThreshold is how close op-geth's unsafe head timestamp must be
// to the current wall-clock time before gossip is enabled.
catchupLagThreshold = 30 * time.Second
// catchupMaxWait is the absolute maximum time we are willing to defer gossip.
// If catch-up does not complete within this window (e.g. L1 derivation is unhealthy),
// gossip is enabled regardless and the system degrades to the pre-fix behavior
// rather than blocking forever.
catchupMaxWait = 10 * time.Minute
// catchupPollInterval is how often we re-check op-geth's unsafe head during catch-up.
catchupPollInterval = 5 * time.Second
)
// The OpNode handles incoming gossip
var _ p2p.GossipIn = (*OpNode)(nil)
// New creates a new OpNode instance.
// The provided ctx argument is for the span of initialization only;
// the node will immediately Stop(ctx) before finishing initialization if the context is canceled during initialization.
func New(ctx context.Context, cfg *Config, log log.Logger, snapshotLog log.Logger, appVersion string, m *metrics.Metrics) (*OpNode, error) {
if err := cfg.Check(); err != nil {
return nil, err
}
n := &OpNode{
log: log,
appVersion: appVersion,
metrics: m,
rollupHalt: cfg.RollupHalt,
cancel: cfg.Cancel,
deferGossipEnabled: cfg.StartupDeferGossip,
}
// If defer gossip is disabled, gossip should be processed immediately as before.
// Set the gate to "ready" up front so OnUnsafeL2Payload behaves identically to the pre-fix code path.
if !n.deferGossipEnabled {
n.gossipReady.Store(true)
}
// not a context leak, gossipsub is closed with a context.
n.resourcesCtx, n.resourcesClose = context.WithCancel(context.Background())
err := n.init(ctx, cfg, snapshotLog)
if err != nil {
log.Error("Error initializing the rollup node", "err", err)
// ensure we always close the node resources if we fail to initialize the node.
if closeErr := n.Stop(ctx); closeErr != nil {
return nil, multierror.Append(err, closeErr)
}
return nil, err
}
return n, nil
}
// waitForOpGethCatchUp blocks until op-geth's unsafe head timestamp is within
// catchUpLagThreshold of the current time, or until catchUpMaxWait elapses.
//
// Background:
// On RPC node restart, op-geth's unsafe head is frozen at the pre-restart height for
// the duration of the pod outage. When op-node comes back up and immediately subscribes
// to gossip, incoming gossip payloads have a parent that does not match op-geth's
// stale unsafe head; the clSync queue accumulates orphan payloads. The driver's
// checkForGapInUnsafeQueue then triggers alt-sync via an unbuffered rangeRequests
// channel, while alt-sync's mainLoop -- when promoting results back via receivePayload
// -- itself blocks on the driver's unsafeL2Payloads channel (buf=10). The two goroutines
// form a livelock that only releases through ctx timeouts, leaving the unsafe head
// stalled for some time.
//
// This function defers gossip subscription (via the gossipReady gate) until the L1
// derivation pipeline has advanced op-geth's unsafe head close enough to the live tip
// that no significant gap exists when gossip is finally enabled, eliminating the
// activity loop's preconditions at the source.
//
// Returns nil on successful catch-up; returns an error on context cancellation or timeout.
// In case of timeout, the caller should still enable gossip and degrade gracefully.
func (n *OpNode) waitForOpGethCatchUp(ctx context.Context) error {
n.log.Info("Starting op-geth catch-up phase before enabling gossip", "lag_threshold", catchupLagThreshold,
"max_wait", catchupMaxWait)
deadline := time.Now().Add(catchupMaxWait)
ticker := time.NewTicker(catchupPollInterval)
defer ticker.Stop()
for {
// Query op-geth's current unsafe head.
queryCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
unsafeHead, err := n.l2Source.L2BlockRefByLabel(queryCtx, eth.Unsafe)
cancel()
if err != nil {
n.log.Warn("Failed to query op-geth unsafe head during catch-up, will retry", "error", err)
} else {
headTime := time.Unix(int64(unsafeHead.Time), 0)
lag := time.Since(headTime)
// Treat negative lag (clock skew or future-timestamp head) as caught up.
if lag < catchupLagThreshold {
n.log.Info("op-geth caught up; enabling gossip", "unsafe_head", unsafeHead.Number, "lag", lag)
return nil
}
n.log.Info("op-geth still catching up via L1 derivation", "unsafe_head", unsafeHead.Number,
"lag", lag, "deadline_in", time.Until(deadline))
}
if time.Now().After(deadline) {
return fmt.Errorf("startup catch-up timeout after %v", catchupMaxWait)
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
continue
}
}
}
func (n *OpNode) init(ctx context.Context, cfg *Config, snapshotLog log.Logger) error {
n.log.Info("Initializing rollup node", "version", n.appVersion)
if err := n.initTracer(ctx, cfg); err != nil {
return fmt.Errorf("failed to init the trace: %w", err)
}
if err := n.initL1(ctx, cfg); err != nil {
return fmt.Errorf("failed to init L1: %w", err)
}
if err := n.initL1Blob(ctx, cfg); err != nil {
return fmt.Errorf("failed to init L1 blob: %w", err)
}
if err := n.initL2(ctx, cfg, snapshotLog); err != nil {
return fmt.Errorf("failed to init L2: %w", err)
}
if err := n.initRuntimeConfig(ctx, cfg); err != nil { // depends on L2, to signal initial runtime values to
return fmt.Errorf("failed to init the runtime config: %w", err)
}
if err := n.initP2PSigner(ctx, cfg); err != nil {
return fmt.Errorf("failed to init the P2P signer: %w", err)
}
if err := n.initP2P(ctx, cfg); err != nil {
return fmt.Errorf("failed to init the P2P stack: %w", err)
}
// Only expose the server at the end, ensuring all RPC backend components are initialized.
if err := n.initRPCServer(cfg); err != nil {
return fmt.Errorf("failed to init the RPC server: %w", err)
}
if err := n.initMetricsServer(cfg); err != nil {
return fmt.Errorf("failed to init the metrics server: %w", err)
}
n.metrics.RecordInfo(n.appVersion)
n.metrics.RecordUp()
n.initHeartbeat(cfg)
if err := n.initPProf(cfg); err != nil {
return fmt.Errorf("failed to init profiling: %w", err)
}
return nil
}
func (n *OpNode) initTracer(ctx context.Context, cfg *Config) error {
if cfg.Tracer != nil {
n.tracer = cfg.Tracer
} else {
n.tracer = new(noOpTracer)
}
return nil
}
func (n *OpNode) initL1(ctx context.Context, cfg *Config) error {
l1Node, rpcCfg, err := cfg.L1.Setup(ctx, n.log, &cfg.Rollup)
if err != nil {
return fmt.Errorf("failed to get L1 RPC client: %w", err)
}
// Set the RethDB path in the EthClientConfig, if there is one configured.
rpcCfg.EthClientConfig.RethDBPath = cfg.RethDBPath
n.l1Source, err = sources.NewL1Client(
client.NewInstrumentedRPC(l1Node, &n.metrics.RPCMetrics.RPCClientMetrics), n.log, n.metrics.L1SourceCache, rpcCfg)
if err != nil {
return fmt.Errorf("failed to create L1 source: %w", err)
}
if err := cfg.Rollup.ValidateL1Config(ctx, n.l1Source); err != nil {
return fmt.Errorf("failed to validate the L1 config: %w", err)
}
// Keep subscribed to the L1 heads, which keeps the L1 maintainer pointing to the best headers to sync
n.l1HeadsSub = event.ResubscribeErr(time.Second*10, func(ctx context.Context, err error) (event.Subscription, error) {
if err != nil {
n.log.Warn("resubscribing after failed L1 subscription", "err", err)
}
return eth.WatchHeadChanges(ctx, n.l1Source, n.OnNewL1Head)
})
if fallbackClient, ok := l1Node.(*sources.FallbackClient); ok {
fallbackClient.RegisterSubscribeFunc(func() (event.Subscription, error) {
return eth.WatchHeadChanges(n.resourcesCtx, n.l1Source, n.OnNewL1Head)
}, &n.l1HeadsSub)
fallbackClient.RegisterMetrics(n.metrics)
}
go func() {
err, ok := <-n.l1HeadsSub.Err()
if !ok {
return
}
n.log.Error("l1 heads subscription error", "err", err)
}()
// Poll for the safe L1 block and finalized block,
// which only change once per epoch at most and may be delayed.
n.l1SafeSub = eth.PollBlockChanges(n.log, n.l1Source, n.OnNewL1Safe, eth.Safe,
cfg.L1EpochPollInterval, time.Second*10)
n.l1FinalizedSub = eth.PollBlockChanges(n.log, n.l1Source, n.OnNewL1Finalized, eth.Finalized,
cfg.L1EpochPollInterval, time.Second*10)
return nil
}
func (n *OpNode) initRuntimeConfig(ctx context.Context, cfg *Config) error {
// attempt to load runtime config, repeat N times
n.runCfg = NewRuntimeConfig(n.log, n.l1Source, &cfg.Rollup)
confDepth := cfg.Driver.VerifierConfDepth
reload := func(ctx context.Context) (eth.L1BlockRef, error) {
fetchCtx, fetchCancel := context.WithTimeout(ctx, time.Second*10)
l1Head, err := n.l1Source.L1BlockRefByLabel(fetchCtx, eth.Unsafe)
fetchCancel()
if err != nil {
n.log.Error("failed to fetch L1 head for runtime config initialization", "err", err)
return eth.L1BlockRef{}, err
}
// Apply confirmation-distance
blNum := l1Head.Number
if blNum >= confDepth {
blNum -= confDepth
}
fetchCtx, fetchCancel = context.WithTimeout(ctx, time.Second*10)
confirmed, err := n.l1Source.L1BlockRefByNumber(fetchCtx, blNum)
fetchCancel()
if err != nil {
n.log.Error("failed to fetch confirmed L1 block for runtime config loading", "err", err, "number", blNum)
return eth.L1BlockRef{}, err
}
fetchCtx, fetchCancel = context.WithTimeout(ctx, time.Second*10)
err = n.runCfg.Load(fetchCtx, confirmed)
fetchCancel()
if err != nil {
n.log.Error("failed to fetch runtime config data", "err", err)
return l1Head, err
}
err = n.handleProtocolVersionsUpdate(ctx)
return l1Head, err
}
// initialize the runtime config before unblocking
if _, err := retry.Do(ctx, 5, retry.Fixed(time.Second*10), func() (eth.L1BlockRef, error) {
ref, err := reload(ctx)
if errors.Is(err, errNodeHalt) { // don't retry on halt error
err = nil
}
return ref, err
}); err != nil {
return fmt.Errorf("failed to load runtime configuration repeatedly, last error: %w", err)
}
// start a background loop, to keep reloading it at the configured reload interval
reloader := func(ctx context.Context, reloadInterval time.Duration) {
if reloadInterval <= 0 {
n.log.Debug("not running runtime-config reloading background loop")
return
}
ticker := time.NewTicker(reloadInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// If the reload fails, we will try again the next interval.
// Missing a runtime-config update is not critical, and we do not want to overwhelm the L1 RPC.
l1Head, err := reload(ctx)
if err != nil {
if errors.Is(err, errNodeHalt) {
n.halted.Store(true)
if n.cancel != nil { // node cancellation is always available when started as CLI app
n.cancel(errNodeHalt)
return
} else {
n.log.Debug("opted to halt, but cannot halt node", "l1_head", l1Head)
}
} else {
n.log.Warn("failed to reload runtime config", "err", err)
}
} else {
n.log.Debug("reloaded runtime config", "l1_head", l1Head)
}
case <-ctx.Done():
return
}
}
}
n.runtimeConfigReloaderDone = make(chan struct{})
// Manages the lifetime of reloader. In order to safely Close the OpNode
go func(ctx context.Context, reloadInterval time.Duration) {
reloader(ctx, reloadInterval)
close(n.runtimeConfigReloaderDone)
}(n.resourcesCtx, cfg.RuntimeConfigReloadInterval) // this keeps running after initialization
return nil
}
func (n *OpNode) initL1Blob(ctx context.Context, cfg *Config) error {
// If Ecotone upgrade is not scheduled yet, then there is no need for a Blob API.
if cfg.Rollup.EcotoneTime == nil {
return nil
}
// Once the Ecotone upgrade is scheduled, we must have initialized the Blob API settings.
if cfg.L1Blob == nil {
return fmt.Errorf("missing L1 Blob Endpoint configuration: this API is mandatory for Ecotone upgrade at t=%d", *cfg.Rollup.EcotoneTime)
}
rpcClients, err := cfg.L1Blob.Setup(ctx, n.log)
if err != nil {
return fmt.Errorf("failed to setup L1 blob client: %w", err)
}
instrumentedClients := make([]client.RPC, 0)
for _, rpc := range rpcClients {
instrumentedClients = append(instrumentedClients, client.NewInstrumentedRPC(rpc, &n.metrics.RPCClientMetrics))
}
n.l1Blob = sources.NewBSCBlobClient(instrumentedClients)
return nil
}
func (n *OpNode) initL2(ctx context.Context, cfg *Config, snapshotLog log.Logger) error {
rpcClient, rpcCfg, err := cfg.L2.Setup(ctx, n.log, &cfg.Rollup)
if err != nil {
return fmt.Errorf("failed to setup L2 execution-engine RPC client: %w", err)
}
n.l2Source, err = sources.NewEngineClient(
client.NewInstrumentedRPC(rpcClient, &n.metrics.RPCClientMetrics), n.log, n.metrics.L2SourceCache, rpcCfg,
)
if err != nil {
return fmt.Errorf("failed to create Engine client: %w", err)
}
if err := cfg.Rollup.ValidateL2Config(ctx, n.l2Source, cfg.Sync.SyncMode == sync.ELSync); err != nil {
return err
}
var sequencerConductor conductor.SequencerConductor = &conductor.NoOpConductor{}
if cfg.ConductorEnabled {
sequencerConductor = NewConductorClient(cfg, n.log, n.metrics)
}
// if plasma is not explicitly activated in the node CLI, the config + any error will be ignored.
rpCfg, err := cfg.Rollup.GetOPPlasmaConfig()
if cfg.Plasma.Enabled && err != nil {
return fmt.Errorf("failed to get plasma config: %w", err)
}
plasmaDA := plasma.NewPlasmaDA(n.log, cfg.Plasma, rpCfg, n.metrics.PlasmaMetrics)
if cfg.SafeDBPath != "" {
n.log.Info("Safe head database enabled", "path", cfg.SafeDBPath)
safeDB, err := safedb.NewSafeDB(n.log, cfg.SafeDBPath)
if err != nil {
return fmt.Errorf("failed to create safe head database at %v: %w", cfg.SafeDBPath, err)
}
n.safeDB = safeDB
} else {
n.safeDB = safedb.Disabled
}
n.l2Driver = driver.NewDriver(&cfg.Driver, &cfg.Rollup, n.l2Source, n.l1Source, n.l1Blob, n, n, n.log, snapshotLog, n.metrics, cfg.ConfigPersistence, n.safeDB, &cfg.Sync, sequencerConductor, plasmaDA)
return nil
}
func (n *OpNode) initRPCServer(cfg *Config) error {
server, err := newRPCServer(&cfg.RPC, &cfg.Rollup, n.l2Source.L2Client, n.l2Driver, n.safeDB, n.log, n.appVersion, n.metrics)
if err != nil {
return err
}
if n.p2pNode != nil {
server.EnableP2P(p2p.NewP2PAPIBackend(n.p2pNode, n.log, n.metrics))
}
if cfg.RPC.EnableAdmin {
server.EnableAdminAPI(NewAdminAPI(n.l2Driver, n.metrics, n.log))
n.log.Info("Admin RPC enabled")
}
n.log.Info("Starting JSON-RPC server")
if err := server.Start(); err != nil {
return fmt.Errorf("unable to start RPC server: %w", err)
}
n.server = server
return nil
}
func (n *OpNode) initMetricsServer(cfg *Config) error {
if !cfg.Metrics.Enabled {
n.log.Info("metrics disabled")
return nil
}
n.log.Debug("starting metrics server", "addr", cfg.Metrics.ListenAddr, "port", cfg.Metrics.ListenPort)
metricsSrv, err := n.metrics.StartServer(cfg.Metrics.ListenAddr, cfg.Metrics.ListenPort)
if err != nil {
return fmt.Errorf("failed to start metrics server: %w", err)
}
n.log.Info("started metrics server", "addr", metricsSrv.Addr())
n.metricsSrv = metricsSrv
return nil
}
func (n *OpNode) initHeartbeat(cfg *Config) {
if !cfg.Heartbeat.Enabled {
return
}
var peerID string
if cfg.P2P.Disabled() {
peerID = "disabled"
} else {
peerID = n.P2P().Host().ID().String()
}
payload := &heartbeat.Payload{
Version: version.Version,
Meta: version.Meta,
Moniker: cfg.Heartbeat.Moniker,
PeerID: peerID,
ChainID: cfg.Rollup.L2ChainID.Uint64(),
}
go func(url string) {
if err := heartbeat.Beat(n.resourcesCtx, n.log, url, payload); err != nil {
log.Error("heartbeat goroutine crashed", "err", err)
}
}(cfg.Heartbeat.URL)
}
func (n *OpNode) initPProf(cfg *Config) error {
n.pprofService = oppprof.New(
cfg.Pprof.ListenEnabled,
cfg.Pprof.ListenAddr,
cfg.Pprof.ListenPort,
cfg.Pprof.ProfileType,
cfg.Pprof.ProfileDir,
cfg.Pprof.ProfileFilename,
)
if err := n.pprofService.Start(); err != nil {
return fmt.Errorf("failed to start pprof service: %w", err)
}
return nil
}
func (n *OpNode) initP2P(ctx context.Context, cfg *Config) error {
if cfg.P2P != nil {
// TODO(protocol-quest/97): Use EL Sync instead of CL Alt sync for fetching missing blocks in the payload queue.
p2pNode, err := p2p.NewNodeP2P(n.resourcesCtx, &cfg.Rollup, n.log, cfg.P2P, n, n.l2Source, n.runCfg, n.metrics, false)
if err != nil || p2pNode == nil {
return err
}
n.p2pNode = p2pNode
if n.p2pNode.Dv5Udp() != nil {
go n.p2pNode.DiscoveryProcess(n.resourcesCtx, n.log, &cfg.Rollup, cfg.P2P.TargetPeers())
}
}
return nil
}
func (n *OpNode) initP2PSigner(ctx context.Context, cfg *Config) error {
// the p2p signer setup is optional
if cfg.P2PSigner == nil {
return nil
}
// p2pSigner may still be nil, the signer setup may not create any signer, the signer is optional
var err error
n.p2pSigner, err = cfg.P2PSigner.SetupSigner(ctx)
return err
}
func (n *OpNode) Start(ctx context.Context) error {
n.log.Info("Starting execution engine driver")
// start driving engine: sync blocks by deriving them from L1 and driving them into the engine
if err := n.l2Driver.Start(); err != nil {
n.log.Error("Could not start a rollup node", "err", err)
return err
}
if n.deferGossipEnabled {
if err := n.waitForOpGethCatchUp(ctx); err != nil {
// Catch-up failed (e.g. timeout, L1 derivation unhealthy). Enable gossip anyway
// to avoid blocking the node forever; the system degrades to the pre-fix behavior.
n.log.Warn("Startup catch-up did not complete cleanly; enabling gossip anyway", "err", err)
}
n.gossipReady.Store(true)
n.log.Info("Gossip enabled; op-node fully active")
}
// If defer gossip is disabled, gossipReady was already set to true in New(),
// so OnUnsafeL2Payload behaves identically to the pre-fix code path.
log.Info("Rollup node started")
return nil
}
func (n *OpNode) OnNewL1Head(ctx context.Context, sig eth.L1BlockRef) {
n.tracer.OnNewL1Head(ctx, sig)
if n.l2Driver == nil {
return
}
// Pass on the event to the L2 Engine
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
if err := n.l2Driver.OnL1Head(ctx, sig); err != nil {
n.log.Warn("failed to notify engine driver of L1 head change", "err", err)
}
}
func (n *OpNode) OnNewL1Safe(ctx context.Context, sig eth.L1BlockRef) {
if n.l2Driver == nil {
return
}
// Pass on the event to the L2 Engine
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
if err := n.l2Driver.OnL1Safe(ctx, sig); err != nil {
n.log.Warn("failed to notify engine driver of L1 safe block change", "err", err)
}
}
func (n *OpNode) OnNewL1Finalized(ctx context.Context, sig eth.L1BlockRef) {
if n.l2Driver == nil {
return
}
// Pass on the event to the L2 Engine
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
if err := n.l2Driver.OnL1Finalized(ctx, sig); err != nil {
n.log.Warn("failed to notify engine driver of L1 finalized block change", "err", err)
}
}
func (n *OpNode) PublishL2Payload(ctx context.Context, envelope *eth.ExecutionPayloadEnvelope) error {
n.tracer.OnPublishL2Payload(ctx, envelope)
// publish to p2p, if we are running p2p at all
if n.p2pNode != nil {
payload := envelope.ExecutionPayload
if n.p2pSigner == nil {
return fmt.Errorf("node has no p2p signer, payload %s cannot be published", payload.ID())
}
n.log.Info("Publishing signed execution payload on p2p", "id", payload.ID())
return n.p2pNode.GossipOut().PublishL2Payload(ctx, envelope, n.p2pSigner)
}
// if p2p is not enabled then we just don't publish the payload
return nil
}
func (n *OpNode) OnUnsafeL2Payload(ctx context.Context, from peer.ID, envelope *eth.ExecutionPayloadEnvelope) error {
// If defer gossip is enabled, drop gossip payloads received during the startup catch-up phase.
if !n.gossipReady.Load() {
if !n.firstPayloadAllowed.Swap(true) {
n.log.Info("Allowing first gossip payload through during catch-up to unblock engine sync state",
"peer", from, "id", envelope.ExecutionPayload.ID())
// fall through to the regular processing path below
} else {
n.log.Debug("Dropping gossip payload during startup catch-up phase", "peer", from,
"id", envelope.ExecutionPayload.ID())
return nil
}
}
// ignore if it's from ourselves
if n.p2pNode != nil && from == n.p2pNode.Host().ID() {
return nil
}
n.tracer.OnUnsafeL2Payload(ctx, from, envelope)
n.log.Info("Received signed execution payload from p2p", "id", envelope.ExecutionPayload.ID(), "peer", from)
// Pass on the event to the L2 Engine
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
if err := n.l2Driver.OnUnsafeL2Payload(ctx, envelope); err != nil {
n.log.Warn("failed to notify engine driver of new L2 payload", "err", err, "id", envelope.ExecutionPayload.ID())
}
return nil
}
func (n *OpNode) RequestL2Range(ctx context.Context, start, end eth.L2BlockRef) error {
if n.p2pNode != nil && n.p2pNode.AltSyncEnabled() {
if unixTimeStale(start.Time, 12*time.Hour) {
n.log.Debug("ignoring request to sync L2 range, timestamp is too old for p2p", "start", start, "end", end, "start_time", start.Time)
return nil
}
return n.p2pNode.RequestL2Range(ctx, start, end)
}
n.log.Debug("ignoring request to sync L2 range, no sync method available", "start", start, "end", end)
return nil
}
// unixTimeStale returns true if the unix timestamp is before the current time minus the supplied duration.
func unixTimeStale(timestamp uint64, duration time.Duration) bool {
return time.Unix(int64(timestamp), 0).Before(time.Now().Add(-1 * duration))
}
func (n *OpNode) P2P() p2p.Node {
return n.p2pNode
}
func (n *OpNode) RuntimeConfig() ReadonlyRuntimeConfig {
return n.runCfg
}
// Stop stops the node and closes all resources.
// If the provided ctx is expired, the node will accelerate the stop where possible, but still fully close.
func (n *OpNode) Stop(ctx context.Context) error {
if n.closed.Load() {
return ErrAlreadyClosed
}
var result *multierror.Error
if n.server != nil {
if err := n.server.Stop(ctx); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close RPC server: %w", err))
}
}
if n.p2pNode != nil {
if err := n.p2pNode.Close(); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close p2p node: %w", err))
}
}
if n.p2pSigner != nil {
if err := n.p2pSigner.Close(); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close p2p signer: %w", err))
}
}
if n.resourcesClose != nil {
n.resourcesClose()
}
// stop L1 heads feed
if n.l1HeadsSub != nil {
n.l1HeadsSub.Unsubscribe()
}
// stop polling for L1 safe-head changes
if n.l1SafeSub != nil {
n.l1SafeSub.Unsubscribe()
}
// stop polling for L1 finalized-head changes
if n.l1FinalizedSub != nil {
n.l1FinalizedSub.Unsubscribe()
}
// close L2 driver
if n.l2Driver != nil {
if err := n.l2Driver.Close(); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close L2 engine driver cleanly: %w", err))
}
}
if n.safeDB != nil {
if err := n.safeDB.Close(); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close safe head db: %w", err))
}
}
// Wait for the runtime config loader to be done using the data sources before closing them
if n.runtimeConfigReloaderDone != nil {
<-n.runtimeConfigReloaderDone
}
// close L2 engine RPC client
if n.l2Source != nil {
n.l2Source.Close()
}
// close L1 data source
if n.l1Source != nil {
n.l1Source.Close()
}
if result == nil { // mark as closed if we successfully fully closed
n.closed.Store(true)
}
if n.halted.Load() {
// if we had a halt upon initialization, idle for a while, with open metrics, to prevent a rapid restart-loop
tim := time.NewTimer(time.Minute * 5)
n.log.Warn("halted, idling to avoid immediate shutdown repeats")
defer tim.Stop()
select {
case <-tim.C:
case <-ctx.Done():
}
}
// Close metrics and pprof only after we are done idling
if n.pprofService != nil {
if err := n.pprofService.Stop(ctx); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close pprof server: %w", err))
}
}
if n.metricsSrv != nil {
if err := n.metricsSrv.Stop(ctx); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close metrics server: %w", err))
}
}
return result.ErrorOrNil()
}
func (n *OpNode) Stopped() bool {
return n.closed.Load()
}
func (n *OpNode) HTTPEndpoint() string {
if n.server == nil {
return ""
}
return fmt.Sprintf("http://%s", n.server.Addr().String())
}