-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecutor.go
More file actions
331 lines (283 loc) · 10.8 KB
/
Copy pathexecutor.go
File metadata and controls
331 lines (283 loc) · 10.8 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
package executor
import (
"fmt"
"github.com/pkg/errors"
"github.com/initia-labs/opinit-bots/executor/batchsubmitter"
"github.com/initia-labs/opinit-bots/executor/celestia"
"github.com/initia-labs/opinit-bots/executor/child"
"github.com/initia-labs/opinit-bots/executor/host"
"github.com/initia-labs/opinit-bots/executor/oraclerelay"
"github.com/initia-labs/opinit-bots/sentry_integration"
"github.com/initia-labs/opinit-bots/server"
"github.com/initia-labs/opinit-bots/server/metrics"
bottypes "github.com/initia-labs/opinit-bots/bot/types"
executortypes "github.com/initia-labs/opinit-bots/executor/types"
btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
"github.com/initia-labs/opinit-bots/types"
"go.uber.org/zap"
)
var _ bottypes.Bot = &Executor{}
// Executor charges the execution of the bridge between the host and the child chain
// - relay l1 deposit messages to l2
// - generate l2 output root and submit to l1
type Executor struct {
host *host.Host
child *child.Child
batchSubmitter *batchsubmitter.BatchSubmitter
oracleRelay *oraclerelay.OracleRelay
cfg *executortypes.Config
db types.DB
server *server.Server
}
func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server) *Executor {
err := sentry_integration.Init("opinit-bots", string(bottypes.BotTypeExecutor))
if err != nil {
panic(err)
}
err = cfg.Validate()
if err != nil {
panic(err)
}
return &Executor{
host: host.NewHostV1(
cfg.L1NodeConfig(),
db.WithPrefix([]byte(types.HostName)),
),
child: child.NewChildV1(
cfg.L2NodeConfig(),
db.WithPrefix([]byte(types.ChildName)),
),
batchSubmitter: batchsubmitter.NewBatchSubmitterV1(
cfg.L2NodeConfig(),
cfg.BatchConfig(),
db.WithPrefix([]byte(types.BatchName)),
cfg.L2Node.ChainID,
),
oracleRelay: oraclerelay.NewOracleRelayV1(cfg.OracleRelay),
cfg: cfg,
db: db,
server: sv,
}
}
func (ex *Executor) Initialize(ctx types.Context) error {
childBridgeInfo, err := ex.child.QueryBridgeInfo(ctx)
if err != nil {
return err
}
if childBridgeInfo.BridgeId == 0 {
return errors.New("bridge info is not set")
}
bridgeInfo, err := ex.host.QueryBridgeConfig(ctx, childBridgeInfo.BridgeId)
if err != nil {
return err
}
ctx.Logger().Info(
"bridge info",
zap.Uint64("id", bridgeInfo.BridgeId),
zap.Duration("submission_interval", bridgeInfo.BridgeConfig.SubmissionInterval),
)
childBridgeInfo.BridgeConfig = bridgeInfo.BridgeConfig
l1StartHeight, l2StartHeight, startOutputIndex, batchStartHeight, err := ex.getNodeStartHeights(ctx, bridgeInfo.BridgeId)
if err != nil {
return errors.Wrap(err, "failed to get processed heights")
}
hostKeyringConfig, childKeyringConfig, childOracleKeyringConfig, daKeyringConfig := ex.getKeyringConfigs(*bridgeInfo)
err = ex.host.Initialize(ctx.WithLogger(ctx.Logger().Named("host")), l1StartHeight-1, ex.child, ex.batchSubmitter, *bridgeInfo, hostKeyringConfig, ex.cfg.OracleRelay.Enable)
if err != nil {
return errors.Wrap(err, "failed to initialize host")
}
err = ex.child.Initialize(ctx.WithLogger(ctx.Logger().Named("child")), l2StartHeight-1, startOutputIndex, ex.host, childBridgeInfo, childKeyringConfig, childOracleKeyringConfig, ex.cfg.DisableDeleteFutureWithdrawal)
if err != nil {
return errors.Wrap(err, "failed to initialize child")
}
err = ex.batchSubmitter.Initialize(ctx.WithLogger(ctx.Logger().Named("batchSubmitter")), batchStartHeight-1, ex.host, *bridgeInfo)
if err != nil {
return errors.Wrap(err, "failed to initialize batch")
}
da, err := ex.makeDANode(ctx.WithLogger(ctx.Logger().Named("da")), *bridgeInfo, daKeyringConfig)
if err != nil {
return errors.Wrap(err, "failed to make DA node")
}
ex.batchSubmitter.SetDANode(da)
if ex.cfg.OracleRelay.Enable {
oracleExecutorAddr, err := ex.child.OracleAccountAddressString()
if err != nil {
ctx.Logger().Warn("failed to get oracle executor address, oracle relay disabled", zap.Error(err))
ex.oracleRelay = nil
} else {
err = ex.oracleRelay.Initialize(ex.host, ex.child, oracleExecutorAddr)
if err != nil {
return errors.Wrap(err, "failed to initialize oracle relay")
}
ctx.Logger().Info("oracle relay initialized",
zap.String("sender", oracleExecutorAddr),
zap.Int64("interval", ex.cfg.OracleRelay.Interval),
)
}
}
ex.RegisterQuerier(ctx)
return nil
}
func (ex *Executor) Start(ctx types.Context) error {
defer ex.Close(ctx)
ctx.ErrGrp().Go(func() (err error) {
<-ctx.Done()
// close batch submitter eagerly on context cancellation to flush
// the gzip writer before the process is killed.
ex.batchSubmitter.Close(ctx)
return ex.server.Shutdown()
})
ctx.ErrGrp().Go(func() (err error) {
defer func() {
ctx.Logger().Info("api server stopped")
}()
return ex.server.Start()
})
ctx.ErrGrp().Go(func() error {
return metrics.StartMetricsUpdater(ctx, ex)
})
ex.host.Start(ctx.WithLogger(ctx.Logger().Named("host")))
ex.child.Start(ctx.WithLogger(ctx.Logger().Named("child")))
ex.batchSubmitter.Start(ctx.WithLogger(ctx.Logger().Named("batchSubmitter")))
ex.batchSubmitter.DA().Start(ctx.WithLogger(ctx.Logger().Named("da")))
if ex.oracleRelay != nil {
ctx.ErrGrp().Go(func() error {
return ex.oracleRelay.Start(ctx.WithLogger(ctx.Logger().Named("oracleRelay")))
})
}
return ctx.ErrGrp().Wait()
}
func (ex *Executor) Close(ctx types.Context) {
ex.batchSubmitter.Close(ctx)
}
// makeDANode creates a DA node based on the bridge info
// - if the bridge chain type is INITIA and the host address is the same as the submitter, it returns the existing host node
// - if the bridge chain type is INITIA and the host address is different from the submitter, it returns a new host node
// - if the bridge chain type is CELESTIA, it returns a new celestia node
func (ex *Executor) makeDANode(ctx types.Context, bridgeInfo ophosttypes.QueryBridgeResponse, daKeyringConfig *btypes.KeyringConfig) (executortypes.DANode, error) {
if ex.cfg.DisableBatchSubmitter {
return batchsubmitter.NewNoopDA(), nil
}
batchInfo := ex.batchSubmitter.BatchInfo()
if batchInfo == nil {
return nil, errors.New("batch info is not set")
}
switch batchInfo.BatchInfo.ChainType {
case ophosttypes.BatchInfo_INITIA:
// might not exist
hostAddrStr, err := ex.host.BaseAccountAddressString()
if err != nil && !errors.Is(err, types.ErrKeyNotSet) {
return nil, errors.Wrap(err, "failed to get host address")
} else if err == nil && hostAddrStr == batchInfo.BatchInfo.Submitter {
return ex.host, nil
}
hostda := host.NewHostV1(
ex.cfg.DANodeConfig(),
ex.db.WithPrefix([]byte(types.DAName)),
)
err = hostda.InitializeDA(ctx, bridgeInfo, daKeyringConfig)
return hostda, errors.Wrap(err, "failed to initialize host DA")
case ophosttypes.BatchInfo_CELESTIA:
celestiada := celestia.NewDACelestia(
ex.cfg.Version,
ex.cfg.DANodeConfig(),
ex.db.WithPrefix([]byte(types.DAName)),
)
err := celestiada.Initialize(ctx, ex.batchSubmitter, bridgeInfo.BridgeId, daKeyringConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize celestia DA")
}
celestiada.RegisterDAHandlers()
return celestiada, nil
}
return nil, fmt.Errorf("unsupported chain id for DA: %s", ophosttypes.BatchInfo_ChainType_name[int32(batchInfo.BatchInfo.ChainType)])
}
// getNodeStartHeights returns the start heights of the host, the child node, and the batch submitter, and the start output index
func (ex *Executor) getNodeStartHeights(ctx types.Context, bridgeId uint64) (l1StartHeight int64, l2StartHeight int64, startOutputIndex uint64, batchStartHeight int64, err error) {
var outputL1Height, outputL2Height int64
var outputIndex uint64
if ex.host.Node().GetSyncedHeight() == 0 || ex.child.Node().GetSyncedHeight() == 0 {
// get the last submitted output height before the start height from the host
output, err := ex.host.QueryOutputByL2BlockNumber(ctx, bridgeId, ex.cfg.L2StartHeight)
if err != nil {
return 0, 0, 0, 0, errors.Wrap(err, "failed to query output by l2 block number")
} else if output != nil {
outputL1Height = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber)
outputL2Height = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber)
outputIndex = output.OutputIndex
}
l2StartHeight = outputL2Height + 1
startOutputIndex = outputIndex + 1
}
if ex.host.Node().GetSyncedHeight() == 0 {
// use l1 start height from the config if auto set is disabled
if ex.cfg.DisableAutoSetL1Height {
l1StartHeight = ex.cfg.L1StartHeight
} else {
childNextL1Sequence, err := ex.child.QueryNextL1Sequence(ctx, 0)
if err != nil {
return 0, 0, 0, 0, errors.Wrap(err, "failed to query next l1 sequence")
}
// query last NextL1Sequence tx height
depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, childNextL1Sequence)
if err != nil {
return 0, 0, 0, 0, errors.Wrap(err, "failed to query deposit tx height")
} else if depositTxHeight == 0 && childNextL1Sequence > 1 {
// if the deposit tx with next_l1_sequence is not found
// query deposit tx with next_l1_sequence-1 tx
depositTxHeight, err = ex.host.QueryDepositTxHeight(ctx, bridgeId, childNextL1Sequence-1)
if err != nil {
return 0, 0, 0, 0, errors.Wrap(err, "failed to query deposit tx height")
}
}
if l1StartHeight < depositTxHeight {
l1StartHeight = depositTxHeight
}
if outputL1Height != 0 && outputL1Height+1 < l1StartHeight {
l1StartHeight = outputL1Height + 1
}
// if l1 start height is not set, get the bridge start height from the host
if l1StartHeight == 0 {
l1StartHeight, err = ex.host.QueryCreateBridgeHeight(ctx, bridgeId)
if err != nil {
return 0, 0, 0, 0, errors.Wrap(err, "failed to query create bridge height")
}
}
}
}
if ex.batchSubmitter.Node().GetSyncedHeight() == 0 {
batchStartHeight = ex.cfg.BatchStartHeight
}
return
}
// getKeyringConfigs returns the keyring configs for the host, the child node, the child oracle node, and the DA node
func (ex *Executor) getKeyringConfigs(bridgeInfo ophosttypes.QueryBridgeResponse) (
hostKeyringConfig *btypes.KeyringConfig,
childKeyringConfig *btypes.KeyringConfig,
childOracleKeyringConfig *btypes.KeyringConfig,
daKeyringConfig *btypes.KeyringConfig,
) {
if !ex.cfg.DisableOutputSubmitter {
hostKeyringConfig = &btypes.KeyringConfig{
Address: bridgeInfo.BridgeConfig.Proposer,
}
}
if ex.cfg.BridgeExecutor != "" {
childKeyringConfig = &btypes.KeyringConfig{
Name: ex.cfg.BridgeExecutor,
}
if bridgeInfo.BridgeConfig.OracleEnabled && ex.cfg.OracleBridgeExecutor != "" {
childOracleKeyringConfig = &btypes.KeyringConfig{
Name: ex.cfg.OracleBridgeExecutor,
FeeGranter: childKeyringConfig,
}
}
}
if !ex.cfg.DisableBatchSubmitter {
daKeyringConfig = &btypes.KeyringConfig{
Address: bridgeInfo.BridgeConfig.BatchInfo.Submitter,
}
}
return
}