-
Notifications
You must be signed in to change notification settings - Fork 5
/
task.go
449 lines (399 loc) · 15.1 KB
/
task.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
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
package task
import (
"crypto/ecdsa"
"github.com/axieinfinity/bridge-v2/stats"
"math/big"
"sort"
"strings"
"time"
ethGovernance "github.com/axieinfinity/bridge-contracts/generated_contracts/ethereum/governance"
roninGovernance "github.com/axieinfinity/bridge-contracts/generated_contracts/ronin/governance"
roninValidator "github.com/axieinfinity/bridge-contracts/generated_contracts/ronin/validator"
bridgeCore "github.com/axieinfinity/bridge-core"
"github.com/axieinfinity/bridge-core/metrics"
"github.com/axieinfinity/bridge-core/utils"
"github.com/axieinfinity/bridge-v2/models"
"github.com/axieinfinity/bridge-v2/stores"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/signer/core"
)
const (
ErrSigAlreadySubmitted = "execution reverted: BOsGovernanceRelay: query for outdated period"
ErrOutdatedPeriod = "execution reverted: BOsGovernanceProposal: query for outdated period"
ErrOperatorsAlreadyVoted = "execution reverted: BridgeOperatorsBallot: bridge operator set is already voted"
)
type task struct {
util utils.Utils
task *models.Task
store stores.BridgeStore
validator *ecdsa.PrivateKey
client bind.ContractBackend
ethClient bind.ContractBackend
contracts map[string]string
chainId *big.Int
maxTry int
taskType string
listener bridgeCore.Listener
releaseTasksCh chan int
gasLimitBumpRatio uint64
}
func newTask(
listener bridgeCore.Listener,
client,
ethClient bind.ContractBackend,
store stores.BridgeStore,
chainId *big.Int,
contracts map[string]string,
maxTry int,
taskType string,
releaseTasksCh chan int,
util utils.Utils,
gasLimitBumpRatio uint64,
) *task {
return &task{
util: util,
task: nil,
store: store,
client: client,
ethClient: ethClient,
contracts: contracts,
chainId: chainId,
maxTry: maxTry,
taskType: taskType,
listener: listener,
releaseTasksCh: releaseTasksCh,
gasLimitBumpRatio: gasLimitBumpRatio,
}
}
func (r *task) collectTask(t *models.Task) {
log.Debug("Received new task", "id", t.ID, "status", t.Status, "type", t.Type)
if t.Type == r.taskType {
r.task = t
}
}
func (r *task) send() {
switch r.taskType {
case VOTE_BRIDGE_OPERATORS_TASK:
r.sendTransaction(r.voteBridgeOperatorsBySignature)
case RELAY_BRIDGE_OPERATORS_TASK:
r.sendTransaction(r.relayBridgeOperators)
}
}
func (r *task) sendTransaction(sendTx func(task *models.Task) (doneTasks, processingTasks, failedTasks []*models.Task, tx *ethtypes.Transaction)) {
if r.task == nil {
return
}
var txHash string
doneTasks, processingTasks, failedTasks, transaction := sendTx(r.task)
if transaction != nil {
log.Debug("[task] Transaction", "type", r.taskType, "hash", transaction.Hash().Hex())
go updateTasks(r.store, processingTasks, STATUS_PROCESSING, transaction.Hash().Hex(), time.Now().Unix(), r.releaseTasksCh)
_ = metrics.Pusher.IncrGauge(metrics.ProcessingTaskMetric, len(processingTasks))
}
go updateTasks(r.store, doneTasks, STATUS_DONE, txHash, 0, r.releaseTasksCh)
go updateTasks(r.store, failedTasks, STATUS_FAILED, txHash, 0, r.releaseTasksCh)
_ = metrics.Pusher.IncrCounter(metrics.SuccessTaskMetric, len(doneTasks))
_ = metrics.Pusher.IncrCounter(metrics.FailedTaskMetric, len(failedTasks))
}
func (r *task) voteBridgeOperatorsBySignature(task *models.Task) (doneTasks, processingTasks, failedTasks []*models.Task, tx *ethtypes.Transaction) {
log.Info("[RoninTask][BridgeOperatorSetCallback] Processing task")
event, err := r.unpackBridgeOperatorSetUpdatedEvent(task)
if err != nil {
task.LastError = err.Error()
stats.SendErrorToStats(r.listener, err)
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
sort.Sort(BridgeOperatorsSorter(event.BridgeOperators))
// create caller
roninGovernanceTransactor, err := roninGovernance.NewGovernance(common.HexToAddress(r.contracts[GOVERNANCE_CONTRACT]), r.client)
if err != nil {
task.LastError = err.Error()
stats.SendErrorToStats(r.listener, err)
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
voted, err := roninGovernanceTransactor.BridgeOperatorsVoted(nil, event.Period, event.Epoch, r.listener.GetVoterSign().GetAddress())
if err != nil {
task.LastError = err.Error()
stats.SendErrorToStats(r.listener, err)
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
if voted {
log.Debug("[RoninTask][BridgeOperatorSetCallback] Bridge already voted", "period", event.Period)
doneTasks = append(doneTasks, task)
return
}
syncedInfo, err := roninGovernanceTransactor.LastSyncedBridgeOperatorSetInfo(nil)
if err != nil {
task.LastError = err.Error()
stats.SendErrorToStats(r.listener, err)
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
sort.Sort(BridgeOperatorsSorter(syncedInfo.Operators))
isValidatorSetShouldUpdate := event.Period.Cmp(syncedInfo.Period) >= 0 && event.Epoch.Cmp(syncedInfo.Epoch) > 0
log.Info("[RoninTask][BridgeOperatorSetCallback] Should vote", "value", isValidatorSetShouldUpdate, "event", event, "syncedInfo", syncedInfo)
if !isValidatorSetShouldUpdate {
doneTasks = append(doneTasks, task)
return doneTasks, nil, nil, nil
}
// otherwise add task to processingTasks to adjust after sending transaction
processingTasks = append(processingTasks, task)
var bridgeOperators []interface{}
for _, address := range event.BridgeOperators {
bridgeOperators = append(bridgeOperators, address.Hex())
}
opts := &signDataOpts{
ChainID: r.chainId,
SignTypedDataCallback: func(typedData core.TypedData) (hexutil.Bytes, error) {
return r.util.SignTypedData(typedData, r.listener.GetVoterSign())
},
}
signature, err := signBridgeOperatorsBallot(opts, event.Period.Int64(), event.Epoch.Int64(), bridgeOperators)
if err != nil {
task.LastError = err.Error()
stats.SendErrorToStats(r.listener, err)
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
signatureStruct := parseSignatureAsRsv(signature)
log.Debug("[RoninTask][BridgeOperatorSetCallback] Prepared data", "r", common.Bytes2Hex(signatureStruct.R[:]), "s", common.Bytes2Hex(signatureStruct.S[:]), "v", signatureStruct.V, "period", event.Period.Int64(), "bridgeOperators", bridgeOperators)
tx, err = r.util.SendContractTransaction(r.listener.GetVoterSign(), r.chainId, r.gasLimitBumpRatio, func(opts *bind.TransactOpts) (*ethtypes.Transaction, error) {
return roninGovernanceTransactor.VoteBridgeOperatorsBySignatures(opts, roninGovernance.BridgeOperatorsBallotBridgeOperatorSet{
Period: event.Period,
Epoch: event.Epoch,
Operators: event.BridgeOperators,
}, []roninGovernance.SignatureConsumerSignature{
signatureStruct,
})
})
if err != nil {
stats.SendErrorToStats(r.listener, err)
switch err.Error() {
case ErrOutdatedPeriod:
log.Warn("[RoninTask][BridgeOperatorSetCallback] Bridge operators period outdated")
doneTasks = append(doneTasks, task)
return doneTasks, nil, nil, nil
case ErrOperatorsAlreadyVoted:
log.Warn("[RoninTask][BridgeOperatorSetCallback] Bridge operators have already voted")
doneTasks = append(doneTasks, task)
return doneTasks, nil, nil, nil
default:
log.Error("[RoninTask][BridgeOperatorSetCallback] Send transaction error", "err", err)
task.LastError = err.Error()
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
}
log.Debug("[RoninTask][BridgeOperatorSetCallback] Vote bridge operators", "hash", tx.Hash().Hex())
return
}
func (r *task) relayBridgeOperators(task *models.Task) (doneTasks, processingTasks, failedTasks []*models.Task, tx *ethtypes.Transaction) {
log.Info("[RoninTask][BridgeOperatorsApprovedCallback] Processing task")
// create caller
roninGovernanceCaller, err := roninGovernance.NewGovernanceCaller(common.HexToAddress(r.contracts[GOVERNANCE_CONTRACT]), r.client)
if err != nil {
stats.SendErrorToStats(r.listener, err)
task.LastError = err.Error()
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
ethGovernanceTransactor, err := ethGovernance.NewGovernance(common.HexToAddress(r.contracts[ETH_GOVERNANCE_CONTRACT]), r.ethClient)
if err != nil {
stats.SendErrorToStats(r.listener, err)
task.LastError = err.Error()
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
event, err := r.unpackBridgeOperatorsApprovedEvent(task)
if err != nil {
stats.SendErrorToStats(r.listener, err)
task.LastError = err.Error()
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
log.Debug("[RoninTask][BridgeOperatorsApprovedCallback] Unpacked event", "event", event)
sort.Sort(BridgeOperatorsSorter(event.Operators))
// Ethereum call
ethSyncedInfo, err := ethGovernanceTransactor.LastSyncedBridgeOperatorSetInfo(nil)
if err != nil {
stats.SendErrorToStats(r.listener, err)
task.LastError = err.Error()
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
sort.Sort(BridgeOperatorsSorter(ethSyncedInfo.Operators))
isValidatorSetEquals := EqualOperatorSet(event.Operators, ethSyncedInfo.Operators)
log.Info("[RoninTask][BridgeOperatorsApprovedCallback] Check operators set", "isEqual", isValidatorSetEquals, "event", event, "ethSyncedInfo", ethSyncedInfo)
if isValidatorSetEquals {
doneTasks = append(doneTasks, task)
return doneTasks, nil, nil, nil
}
// otherwise add task to processingTasks to adjust after sending transaction
processingTasks = append(processingTasks, task)
signatures, err := roninGovernanceCaller.GetBridgeOperatorVotingSignatures(nil, event.Period, event.Epoch)
if err != nil {
stats.SendErrorToStats(r.listener, err)
task.LastError = err.Error()
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
log.Info("[RoninTask][BridgeOperatorsApprovedCallback] Bridge operator voting signatures", "signatures", signatures.Signatures, "voters", signatures.Voters)
for i := 0; i < len(signatures.Signatures)-1; i++ {
for j := i + 1; j < len(signatures.Signatures); j++ {
if strings.ToLower(signatures.Voters[j].Hex()) < strings.ToLower(signatures.Voters[i].Hex()) {
signatures.Voters[j], signatures.Voters[i] = signatures.Voters[i], signatures.Voters[j]
signatures.Signatures[j], signatures.Signatures[i] = signatures.Signatures[i], signatures.Signatures[j]
}
}
}
var ethSignatures []ethGovernance.SignatureConsumerSignature
for _, sig := range signatures.Signatures {
if sig.V > 0 {
ethSignatures = append(ethSignatures, ethGovernance.SignatureConsumerSignature{
V: sig.V,
R: sig.R,
S: sig.S,
})
}
}
ethListener := r.listener.GetListener("Ethereum")
ethChainId, err := ethListener.GetChainID()
if err != nil {
task.LastError = err.Error()
stats.SendErrorToStats(r.listener, err)
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
tx, err = r.util.SendContractTransaction(r.listener.GetRelayerSign(), ethChainId, r.gasLimitBumpRatio, func(opts *bind.TransactOpts) (*ethtypes.Transaction, error) {
return ethGovernanceTransactor.RelayBridgeOperators(opts, ethGovernance.BridgeOperatorsBallotBridgeOperatorSet{
Period: event.Period,
Epoch: event.Epoch,
Operators: event.Operators,
}, ethSignatures)
})
if err != nil {
stats.SendErrorToStats(r.listener, err)
// Prevent retry submit signature if the signature was already submitted
switch err.Error() {
case ErrSigAlreadySubmitted:
log.Warn("[RoninTask][BridgeOperatorsApprovedCallback] Bridge operators already submitted")
doneTasks = append(doneTasks, task)
return doneTasks, nil, nil, nil
default:
log.Error("[RoninTask][BridgeOperatorsApprovedCallback] Send transaction error", "err", err)
task.LastError = err.Error()
failedTasks = append(failedTasks, task)
return nil, nil, failedTasks, nil
}
}
log.Debug("[RoninTask][BridgeOperatorsApprovedCallback] Relay bridge operators", "hash", tx.Hash().Hex())
return
}
func (r *task) unpackBridgeOperatorSetUpdatedEvent(task *models.Task) (*roninValidator.ValidatorBridgeOperatorSetUpdated, error) {
roninEvent := new(roninValidator.ValidatorBridgeOperatorSetUpdated)
roninValidatorAbi, err := roninValidator.ValidatorMetaData.GetAbi()
if err != nil {
return nil, err
}
log.Debug("Bridge operator set updated", "data", task.Data)
if err = r.util.UnpackLog(*roninValidatorAbi, roninEvent, "BridgeOperatorSetUpdated", common.Hex2Bytes(task.Data)); err != nil {
return nil, err
}
return roninEvent, nil
}
func (r *task) unpackBridgeOperatorsApprovedEvent(task *models.Task) (*roninGovernance.GovernanceBridgeOperatorsApproved, error) {
roninEvent := new(roninGovernance.GovernanceBridgeOperatorsApproved)
roninGovernanceAbi, err := roninGovernance.GovernanceMetaData.GetAbi()
if err != nil {
return nil, err
}
log.Debug("Bridge operators approved", "data", task.Data)
if err = r.util.UnpackLog(*roninGovernanceAbi, roninEvent, "BridgeOperatorsApproved", common.Hex2Bytes(task.Data)); err != nil {
return nil, err
}
return roninEvent, nil
}
type signDataOpts struct {
ChainID *big.Int
SignTypedDataCallback func(typedData core.TypedData) (hexutil.Bytes, error)
}
func createSalt(chainID *big.Int) (*common.Hash, error) {
uint256ty, err := abi.NewType("uint256", "uint256", nil)
if err != nil {
return nil, err
}
stringTy, err := abi.NewType("string", "string", nil)
if err != nil {
return nil, err
}
args := abi.Arguments{
{
Type: stringTy,
},
{
Type: uint256ty,
},
}
bytes, err := args.Pack("RONIN_GOVERNANCE_ADMIN", chainID)
if err != nil {
return nil, err
}
hash := crypto.Keccak256Hash(bytes)
return &hash, nil
}
func signBridgeOperatorsBallot(opts *signDataOpts, period, epoch int64, bridgeOperators interface{}) ([]byte, error) {
salt, err := createSalt(opts.ChainID)
if err != nil {
log.Error("Can not encode salt key", "error", err)
return nil, err
}
log.Debug("Generated salt key", "salt", salt.Hex(), "chainID", opts.ChainID)
bridgeOperatorsBallotTypes := core.TypedData{
Types: core.Types{
"EIP712Domain": []core.Type{
{
Name: "name", Type: "string",
},
{
Name: "version", Type: "string",
},
{
Name: "salt", Type: "bytes32",
},
},
"BridgeOperatorsBallot": []core.Type{
{
Name: "period", Type: "uint256",
},
{
Name: "epoch", Type: "uint256",
},
{
Name: "operators", Type: "address[]",
},
},
},
PrimaryType: "BridgeOperatorsBallot",
Domain: core.TypedDataDomain{
Name: "GovernanceAdmin",
Version: "2",
Salt: salt.Hex(),
},
Message: core.TypedDataMessage{
"period": math.NewHexOrDecimal256(period),
"epoch": math.NewHexOrDecimal256(epoch),
"operators": bridgeOperators,
},
}
return opts.SignTypedDataCallback(bridgeOperatorsBallotTypes)
}