-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchallenger.go
268 lines (224 loc) · 6.67 KB
/
challenger.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
package challenger
import (
"context"
"strconv"
"sync"
"time"
"github.com/pkg/errors"
"github.com/gofiber/fiber/v2"
"github.com/initia-labs/opinit-bots/challenger/child"
"github.com/initia-labs/opinit-bots/challenger/host"
"github.com/initia-labs/opinit-bots/server"
bottypes "github.com/initia-labs/opinit-bots/bot/types"
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
"github.com/initia-labs/opinit-bots/types"
"go.uber.org/zap"
)
var _ bottypes.Bot = &Challenger{}
// 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 Challenger struct {
host *host.Host
child *child.Child
cfg *challengertypes.Config
db types.DB
server *server.Server
logger *zap.Logger
homePath string
challengeCh chan challengertypes.Challenge
challengeChStopped chan struct{}
pendingChallenges []challengertypes.Challenge
// status info
latestChallengesMu *sync.Mutex
latestChallenges []challengertypes.Challenge
}
func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server, logger *zap.Logger, homePath string) *Challenger {
err := cfg.Validate()
if err != nil {
panic(err)
}
challengeCh := make(chan challengertypes.Challenge)
return &Challenger{
host: host.NewHostV1(
cfg.L1NodeConfig(homePath),
db.WithPrefix([]byte(types.HostName)),
logger.Named(types.HostName), cfg.L1Node.Bech32Prefix,
),
child: child.NewChildV1(
cfg.L2NodeConfig(homePath),
db.WithPrefix([]byte(types.ChildName)),
logger.Named(types.ChildName), cfg.L2Node.Bech32Prefix,
),
cfg: cfg,
db: db,
server: sv,
logger: logger,
homePath: homePath,
challengeCh: challengeCh,
challengeChStopped: make(chan struct{}),
pendingChallenges: make([]challengertypes.Challenge, 0),
latestChallengesMu: &sync.Mutex{},
latestChallenges: make([]challengertypes.Challenge, 0),
}
}
func (c *Challenger) Initialize(ctx context.Context) error {
bridgeInfo, err := c.child.QueryBridgeInfo(ctx)
if err != nil {
return err
}
if bridgeInfo.BridgeId == 0 {
return errors.New("bridge info is not set")
}
c.logger.Info(
"bridge info",
zap.Uint64("id", bridgeInfo.BridgeId),
zap.Duration("submission_interval", bridgeInfo.BridgeConfig.SubmissionInterval),
)
hostProcessedHeight, childProcessedHeight, processedOutputIndex, err := c.getProcessedHeights(ctx, bridgeInfo.BridgeId)
if err != nil {
return err
}
var initialBlockTime time.Time
hostInitialBlockTime, err := c.host.Initialize(ctx, hostProcessedHeight, c.child, bridgeInfo, c)
if err != nil {
return err
}
if initialBlockTime.Before(hostInitialBlockTime) {
initialBlockTime = hostInitialBlockTime
}
childInitialBlockTime, err := c.child.Initialize(ctx, childProcessedHeight, processedOutputIndex+1, c.host, bridgeInfo, c)
if err != nil {
return err
}
if initialBlockTime.Before(childInitialBlockTime) {
initialBlockTime = childInitialBlockTime
}
// only called when `ResetHeight` was executed.
if !initialBlockTime.IsZero() {
// The db state is reset to a specific height, so we also
// need to delete future challenges which are not applicable anymore.
err := c.DeleteFutureChallenges(initialBlockTime)
if err != nil {
return err
}
}
c.RegisterQuerier()
c.pendingChallenges, err = c.loadPendingChallenges()
if err != nil {
return err
}
c.latestChallenges, err = c.loadChallenges()
if err != nil {
return err
}
return nil
}
func (c *Challenger) Start(ctx context.Context) error {
defer c.Close()
errGrp := types.ErrGrp(ctx)
errGrp.Go(func() (err error) {
<-ctx.Done()
return c.server.Shutdown()
})
errGrp.Go(func() (err error) {
defer func() {
c.logger.Info("api server stopped")
}()
return c.server.Start(c.cfg.ListenAddress)
})
errGrp.Go(func() error {
for _, ch := range c.pendingChallenges {
c.challengeCh <- ch
}
return nil
})
errGrp.Go(func() (err error) {
defer func() {
c.logger.Info("challenge handler stopped")
}()
return c.challengeHandler(ctx)
})
c.host.Start(ctx)
c.child.Start(ctx)
return errGrp.Wait()
}
func (c *Challenger) Close() {
c.db.Close()
}
func (c *Challenger) RegisterQuerier() {
c.server.RegisterQuerier("/status", func(ctx *fiber.Ctx) error {
return ctx.JSON(c.GetStatus())
})
c.server.RegisterQuerier("/challenges/:page", func(ctx *fiber.Ctx) error {
pageStr := ctx.Params("page")
if pageStr == "" {
pageStr = "1"
}
page, err := strconv.ParseUint(pageStr, 10, 64)
if err != nil {
return err
}
res, err := c.QueryChallenges(page)
if err != nil {
return err
}
return ctx.JSON(res)
})
c.server.RegisterQuerier("/pending_events/host", func(ctx *fiber.Ctx) error {
return ctx.JSON(c.host.GetAllPendingEvents())
})
c.server.RegisterQuerier("/pending_events/child", func(ctx *fiber.Ctx) error {
return ctx.JSON(c.child.GetAllPendingEvents())
})
}
func (c *Challenger) getProcessedHeights(ctx context.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, err error) {
var outputL1BlockNumber int64
// get the last submitted output height before the start height from the host
if c.cfg.L2StartHeight != 0 {
output, err := c.host.QueryLastFinalizedOutput(ctx, bridgeId)
if err != nil {
return 0, 0, 0, err
} else if output != nil {
outputL1BlockNumber = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber)
l2ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber)
processedOutputIndex = output.OutputIndex
}
}
if c.cfg.DisableAutoSetL1Height {
l1ProcessedHeight = c.cfg.L1StartHeight
} else {
// get the bridge start height from the host
l1ProcessedHeight, err = c.host.QueryCreateBridgeHeight(ctx, bridgeId)
if err != nil {
return 0, 0, 0, err
}
if l2ProcessedHeight > 0 {
l1Sequence, err := c.child.QueryNextL1Sequence(ctx, l2ProcessedHeight-1)
if err != nil {
return 0, 0, 0, err
}
// query l1Sequence tx height
depositTxHeight, err := c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence)
if err != nil {
return 0, 0, 0, err
} else if depositTxHeight == 0 && l1Sequence > 1 {
// query l1Sequence - 1 tx height
depositTxHeight, err = c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1)
if err != nil {
return 0, 0, 0, err
}
}
if depositTxHeight > l1ProcessedHeight {
l1ProcessedHeight = depositTxHeight
}
if outputL1BlockNumber != 0 && outputL1BlockNumber < l1ProcessedHeight {
l1ProcessedHeight = outputL1BlockNumber
}
}
}
if l1ProcessedHeight > 0 {
l1ProcessedHeight--
}
return l1ProcessedHeight, l2ProcessedHeight, processedOutputIndex, err
}