Skip to content

Commit 155426d

Browse files
authored
Add watchdog command in service (#61)
* Add watchdog command in service * Go
1 parent 0b7f850 commit 155426d

File tree

5 files changed

+67
-68
lines changed

5 files changed

+67
-68
lines changed

internal/components.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ func (o *OpGeth) Run(service *service, ctx *ExContext) {
153153
)
154154
}
155155

156+
var _ ServiceWatchdog = &OpGeth{}
157+
158+
func (o *OpGeth) Watchdog(out io.Writer, service *service, ctx context.Context) error {
159+
rethURL := fmt.Sprintf("http://localhost:%d", service.MustGetPort("http").HostPort)
160+
return watchChainHead(out, rethURL, 2*time.Second)
161+
}
162+
156163
type RethEL struct {
157164
UseRethForValidation bool
158165
UseNativeReth bool
@@ -229,6 +236,13 @@ func (r *RethEL) Run(svc *service, ctx *ExContext) {
229236
}
230237
}
231238

239+
var _ ServiceWatchdog = &RethEL{}
240+
241+
func (r *RethEL) Watchdog(out io.Writer, service *service, ctx context.Context) error {
242+
rethURL := fmt.Sprintf("http://localhost:%d", service.MustGetPort("http").HostPort)
243+
return watchChainHead(out, rethURL, 12*time.Second)
244+
}
245+
232246
type LighthouseBeaconNode struct {
233247
ExecutionNode string
234248
MevBoostNode string
@@ -344,3 +358,19 @@ func (m *MevBoostRelay) Run(service *service, ctx *ExContext) {
344358
service.WithArgs("--validation-server-addr", Connect(m.ValidationServer, "http"))
345359
}
346360
}
361+
362+
var _ ServiceWatchdog = &MevBoostRelay{}
363+
364+
func (m *MevBoostRelay) Watchdog(out io.Writer, service *service, ctx context.Context) error {
365+
beaconNodeURL := fmt.Sprintf("http://localhost:%d", service.MustGetPort("http").HostPort)
366+
367+
watchGroup := newWatchGroup()
368+
watchGroup.watch(func() error {
369+
return watchProposerPayloads(beaconNodeURL)
370+
})
371+
watchGroup.watch(func() error {
372+
return validateProposerPayloads(out, beaconNodeURL)
373+
})
374+
375+
return watchGroup.wait()
376+
}

internal/manifest.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type Recipe interface {
1919
Flags() *flag.FlagSet
2020
Artifacts() *ArtifactsBuilder
2121
Apply(ctx *ExContext, artifacts *Artifacts) *Manifest
22-
Watchdog(manifest *Manifest, out *output) error
2322
}
2423

2524
// Manifest describes a list of services and their dependencies
@@ -114,6 +113,42 @@ func WaitForReady(manifest *Manifest) error {
114113
return nil
115114
}
116115

116+
type ServiceWatchdog interface {
117+
Watchdog(out io.Writer, service *service, ctx context.Context) error
118+
}
119+
120+
func RunWatchdog(manifest *Manifest) error {
121+
var wg sync.WaitGroup
122+
watchdogErr := make(chan error, len(manifest.Services()))
123+
124+
output, err := manifest.out.LogOutput("watchdog")
125+
if err != nil {
126+
return fmt.Errorf("failed to create log output: %w", err)
127+
}
128+
129+
for _, s := range manifest.Services() {
130+
if watchdogFn, ok := s.component.(ServiceWatchdog); ok {
131+
wg.Add(1)
132+
133+
go func() {
134+
defer wg.Done()
135+
if err := watchdogFn.Watchdog(output, s, context.Background()); err != nil {
136+
watchdogErr <- fmt.Errorf("service %s watchdog failed: %w", s.Name, err)
137+
}
138+
}()
139+
}
140+
}
141+
wg.Wait()
142+
143+
close(watchdogErr)
144+
for err := range watchdogErr {
145+
if err != nil {
146+
return err
147+
}
148+
}
149+
return nil
150+
}
151+
117152
func (s *Manifest) Services() []*service {
118153
return s.services
119154
}

internal/recipe_l1.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package internal
22

33
import (
44
"fmt"
5-
"time"
65

76
flag "github.com/spf13/pflag"
87
)
@@ -89,32 +88,3 @@ func (l *L1Recipe) Apply(ctx *ExContext, artifacts *Artifacts) *Manifest {
8988
})
9089
return svcManager
9190
}
92-
93-
func (l *L1Recipe) Watchdog(manifest *Manifest, out *output) error {
94-
beaconNode := manifest.MustGetService("beacon")
95-
beaconNodeEL := manifest.MustGetService("el")
96-
97-
watchDogOut, err := out.LogOutput("watchdog")
98-
if err != nil {
99-
return err
100-
}
101-
102-
beaconNodeURL := fmt.Sprintf("http://localhost:%d", beaconNode.MustGetPort("http").HostPort)
103-
beaconNodeELURL := fmt.Sprintf("http://localhost:%d", beaconNodeEL.MustGetPort("http").HostPort)
104-
105-
watchGroup := newWatchGroup()
106-
watchGroup.watch(func() error {
107-
return watchProposerPayloads(beaconNodeURL)
108-
})
109-
watchGroup.watch(func() error {
110-
return validateProposerPayloads(watchDogOut, beaconNodeURL)
111-
})
112-
watchGroup.watch(func() error {
113-
return watchChainHead(watchDogOut, beaconNodeELURL, 12*time.Second)
114-
})
115-
116-
if err := watchGroup.wait(); err != nil {
117-
return err
118-
}
119-
return nil
120-
}

internal/recipe_opstack.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package internal
22

33
import (
4-
"fmt"
5-
"time"
6-
74
flag "github.com/spf13/pflag"
85
)
96

@@ -67,36 +64,3 @@ func (o *OpRecipe) Apply(ctx *ExContext, artifacts *Artifacts) *Manifest {
6764
})
6865
return svcManager
6966
}
70-
71-
func (o *OpRecipe) Watchdog(manifest *Manifest, out *output) error {
72-
beaconNode := manifest.MustGetService("beacon")
73-
beaconNodeEL := manifest.MustGetService("el")
74-
opNodeEL := manifest.MustGetService("op-geth")
75-
76-
watchDogOut, err := out.LogOutput("watchdog")
77-
if err != nil {
78-
return err
79-
}
80-
81-
beaconNodeURL := fmt.Sprintf("http://localhost:%d", beaconNode.MustGetPort("http").HostPort)
82-
if err := waitForChainAlive(watchDogOut, beaconNodeURL, 50*time.Second); err != nil {
83-
return err
84-
}
85-
86-
watchGroup := newWatchGroup()
87-
88-
beaconNodeELURL := fmt.Sprintf("http://localhost:%d", beaconNodeEL.MustGetPort("http").HostPort)
89-
watchGroup.watch(func() error {
90-
return watchChainHead(watchDogOut, beaconNodeELURL, 12*time.Second)
91-
})
92-
93-
opNodeELURL := fmt.Sprintf("http://localhost:%d", opNodeEL.MustGetPort("http").HostPort)
94-
watchGroup.watch(func() error {
95-
return watchChainHead(watchDogOut, opNodeELURL, 2*time.Second)
96-
})
97-
98-
if err := watchGroup.wait(); err != nil {
99-
return err
100-
}
101-
return nil
102-
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func runIt(recipe internal.Recipe) error {
147147
watchdogErr := make(chan error, 1)
148148
if watchdog {
149149
go func() {
150-
if err := recipe.Watchdog(svcManager, artifacts.Out); err != nil {
150+
if err := internal.RunWatchdog(svcManager); err != nil {
151151
watchdogErr <- fmt.Errorf("watchdog failed: %w", err)
152152
}
153153
}()

0 commit comments

Comments
 (0)