forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
322 lines (281 loc) · 10.3 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
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
package game
import (
"context"
"errors"
"fmt"
"io"
"sync/atomic"
"github.com/ethereum-optimism/optimism/op-challenger/game/keccak"
"github.com/ethereum-optimism/optimism/op-challenger/game/keccak/fetcher"
"github.com/ethereum-optimism/optimism/op-challenger/sender"
"github.com/ethereum-optimism/optimism/op-service/sources"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum-optimism/optimism/op-challenger/config"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/claims"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/contracts"
"github.com/ethereum-optimism/optimism/op-challenger/game/registry"
"github.com/ethereum-optimism/optimism/op-challenger/game/scheduler"
"github.com/ethereum-optimism/optimism/op-challenger/metrics"
"github.com/ethereum-optimism/optimism/op-challenger/version"
"github.com/ethereum-optimism/optimism/op-service/client"
"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum-optimism/optimism/op-service/dial"
"github.com/ethereum-optimism/optimism/op-service/httputil"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
"github.com/ethereum-optimism/optimism/op-service/oppprof"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
)
type Service struct {
logger log.Logger
metrics metrics.Metricer
monitor *gameMonitor
sched *scheduler.Scheduler
faultGamesCloser fault.CloseFunc
preimages *keccak.LargePreimageScheduler
txMgr *txmgr.SimpleTxManager
txSender *sender.TxSender
systemClock clock.Clock
l1Clock *clock.SimpleClock
claimants []common.Address
claimer *claims.BondClaimScheduler
factoryContract *contracts.DisputeGameFactoryContract
registry *registry.GameTypeRegistry
oracles *registry.OracleRegistry
rollupClient *sources.RollupClient
l1Client *ethclient.Client
pollClient client.RPC
pprofService *oppprof.Service
metricsSrv *httputil.HTTPServer
balanceMetricer io.Closer
stopped atomic.Bool
}
// NewService creates a new Service.
func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*Service, error) {
s := &Service{
systemClock: clock.SystemClock,
l1Clock: clock.NewSimpleClock(),
logger: logger,
metrics: metrics.NewMetrics(),
}
if err := s.initFromConfig(ctx, cfg); err != nil {
// upon initialization error we can try to close any of the service components that may have started already.
return nil, errors.Join(fmt.Errorf("failed to init challenger game service: %w", err), s.Stop(ctx))
}
return s, nil
}
func (s *Service) initFromConfig(ctx context.Context, cfg *config.Config) error {
if err := s.initTxManager(ctx, cfg); err != nil {
return fmt.Errorf("failed to init tx manager: %w", err)
}
s.initClaimants(cfg)
if err := s.initL1Client(ctx, cfg); err != nil {
return fmt.Errorf("failed to init l1 client: %w", err)
}
if err := s.initRollupClient(ctx, cfg); err != nil {
return fmt.Errorf("failed to init rollup client: %w", err)
}
if err := s.initPollClient(ctx, cfg); err != nil {
return fmt.Errorf("failed to init poll client: %w", err)
}
if err := s.initPProf(&cfg.PprofConfig); err != nil {
return fmt.Errorf("failed to init profiling: %w", err)
}
if err := s.initMetricsServer(&cfg.MetricsConfig); err != nil {
return fmt.Errorf("failed to init metrics server: %w", err)
}
if err := s.initFactoryContract(cfg); err != nil {
return fmt.Errorf("failed to create factory contract bindings: %w", err)
}
if err := s.registerGameTypes(ctx, cfg); err != nil {
return fmt.Errorf("failed to register game types: %w", err)
}
if err := s.initBondClaims(); err != nil {
return fmt.Errorf("failed to init bond claiming: %w", err)
}
if err := s.initScheduler(cfg); err != nil {
return fmt.Errorf("failed to init scheduler: %w", err)
}
if err := s.initLargePreimages(); err != nil {
return fmt.Errorf("failed to init large preimage scheduler: %w", err)
}
s.initMonitor(cfg)
s.metrics.RecordInfo(version.SimpleWithMeta)
s.metrics.RecordUp()
return nil
}
func (s *Service) initClaimants(cfg *config.Config) {
claimants := []common.Address{s.txSender.From()}
s.claimants = append(claimants, cfg.AdditionalBondClaimants...)
}
func (s *Service) initTxManager(ctx context.Context, cfg *config.Config) error {
txMgr, err := txmgr.NewSimpleTxManager("challenger", s.logger, s.metrics, cfg.TxMgrConfig)
if err != nil {
return fmt.Errorf("failed to create the transaction manager: %w", err)
}
s.txMgr = txMgr
s.txSender = sender.NewTxSender(ctx, s.logger, txMgr, cfg.MaxPendingTx)
return nil
}
func (s *Service) initL1Client(ctx context.Context, cfg *config.Config) error {
l1Client, err := dial.DialEthClientWithTimeout(ctx, dial.DefaultDialTimeout, s.logger, cfg.L1EthRpc)
if err != nil {
return fmt.Errorf("failed to dial L1: %w", err)
}
s.l1Client = l1Client
return nil
}
func (s *Service) initPollClient(ctx context.Context, cfg *config.Config) error {
pollClient, err := client.NewRPCWithClient(ctx, s.logger, cfg.L1EthRpc, client.NewBaseRPCClient(s.l1Client.Client()), cfg.PollInterval)
if err != nil {
return fmt.Errorf("failed to create RPC client: %w", err)
}
s.pollClient = pollClient
return nil
}
func (s *Service) initPProf(cfg *oppprof.CLIConfig) error {
s.pprofService = oppprof.New(
cfg.ListenEnabled,
cfg.ListenAddr,
cfg.ListenPort,
cfg.ProfileType,
cfg.ProfileDir,
cfg.ProfileFilename,
)
if err := s.pprofService.Start(); err != nil {
return fmt.Errorf("failed to start pprof service: %w", err)
}
return nil
}
func (s *Service) initMetricsServer(cfg *opmetrics.CLIConfig) error {
if !cfg.Enabled {
return nil
}
s.logger.Debug("starting metrics server", "addr", cfg.ListenAddr, "port", cfg.ListenPort)
m, ok := s.metrics.(opmetrics.RegistryMetricer)
if !ok {
return fmt.Errorf("metrics were enabled, but metricer %T does not expose registry for metrics-server", s.metrics)
}
metricsSrv, err := opmetrics.StartServer(m.Registry(), cfg.ListenAddr, cfg.ListenPort)
if err != nil {
return fmt.Errorf("failed to start metrics server: %w", err)
}
s.logger.Info("started metrics server", "addr", metricsSrv.Addr())
s.metricsSrv = metricsSrv
s.balanceMetricer = s.metrics.StartBalanceMetrics(s.logger, s.l1Client, s.txSender.From())
return nil
}
func (s *Service) initFactoryContract(cfg *config.Config) error {
factoryContract, err := contracts.NewDisputeGameFactoryContract(s.metrics, cfg.GameFactoryAddress,
batching.NewMultiCaller(s.l1Client.Client(), batching.DefaultBatchSize))
if err != nil {
return fmt.Errorf("failed to bind the fault dispute game factory contract: %w", err)
}
s.factoryContract = factoryContract
return nil
}
func (s *Service) initBondClaims() error {
claimer := claims.NewBondClaimer(s.logger, s.metrics, s.registry.CreateBondContract, s.txSender, s.claimants...)
s.claimer = claims.NewBondClaimScheduler(s.logger, s.metrics, claimer)
return nil
}
func (s *Service) initRollupClient(ctx context.Context, cfg *config.Config) error {
if cfg.RollupRpc == "" {
return nil
}
rollupClient, err := dial.DialRollupClientWithTimeout(ctx, dial.DefaultDialTimeout, s.logger, cfg.RollupRpc)
if err != nil {
return err
}
s.rollupClient = rollupClient
return nil
}
func (s *Service) registerGameTypes(ctx context.Context, cfg *config.Config) error {
gameTypeRegistry := registry.NewGameTypeRegistry()
oracles := registry.NewOracleRegistry()
caller := batching.NewMultiCaller(s.l1Client.Client(), batching.DefaultBatchSize)
closer, err := fault.RegisterGameTypes(ctx, s.systemClock, s.l1Clock, s.logger, s.metrics, cfg, gameTypeRegistry, oracles, s.rollupClient, s.txSender, s.factoryContract, caller, s.l1Client, cfg.SelectiveClaimResolution, s.claimants)
if err != nil {
return err
}
s.faultGamesCloser = closer
s.registry = gameTypeRegistry
s.oracles = oracles
return nil
}
func (s *Service) initScheduler(cfg *config.Config) error {
disk := newDiskManager(cfg.Datadir)
s.sched = scheduler.NewScheduler(s.logger, s.metrics, disk, cfg.MaxConcurrency, s.registry.CreatePlayer, cfg.AllowInvalidPrestate)
return nil
}
func (s *Service) initLargePreimages() error {
fetcher := fetcher.NewPreimageFetcher(s.logger, s.l1Client)
verifier := keccak.NewPreimageVerifier(s.logger, fetcher)
challenger := keccak.NewPreimageChallenger(s.logger, s.metrics, verifier, s.txSender)
s.preimages = keccak.NewLargePreimageScheduler(s.logger, s.l1Clock, s.oracles, challenger)
return nil
}
func (s *Service) initMonitor(cfg *config.Config) {
s.monitor = newGameMonitor(s.logger, s.l1Clock, s.factoryContract, s.sched, s.preimages, cfg.GameWindow, s.claimer, s.l1Client.BlockNumber, cfg.GameAllowlist, s.pollClient)
}
func (s *Service) Start(ctx context.Context) error {
s.logger.Info("starting scheduler")
s.sched.Start(ctx)
s.claimer.Start(ctx)
s.preimages.Start(ctx)
s.logger.Info("starting monitoring")
s.monitor.StartMonitoring()
s.logger.Info("challenger game service start completed")
return nil
}
func (s *Service) Stopped() bool {
return s.stopped.Load()
}
func (s *Service) Stop(ctx context.Context) error {
s.logger.Info("stopping challenger game service")
var result error
if s.sched != nil {
if err := s.sched.Close(); err != nil {
result = errors.Join(result, fmt.Errorf("failed to close scheduler: %w", err))
}
}
if s.monitor != nil {
s.monitor.StopMonitoring()
}
if s.faultGamesCloser != nil {
s.faultGamesCloser()
}
if s.pprofService != nil {
if err := s.pprofService.Stop(ctx); err != nil {
result = errors.Join(result, fmt.Errorf("failed to close pprof server: %w", err))
}
}
if s.balanceMetricer != nil {
if err := s.balanceMetricer.Close(); err != nil {
result = errors.Join(result, fmt.Errorf("failed to close balance metricer: %w", err))
}
}
if s.txMgr != nil {
s.txMgr.Close()
}
if s.rollupClient != nil {
s.rollupClient.Close()
}
if s.pollClient != nil {
s.pollClient.Close()
}
if s.l1Client != nil {
s.l1Client.Close()
}
if s.metricsSrv != nil {
if err := s.metricsSrv.Stop(ctx); err != nil {
result = errors.Join(result, fmt.Errorf("failed to close metrics server: %w", err))
}
}
s.stopped.Store(true)
s.logger.Info("stopped challenger game service", "err", result)
return result
}