-
Notifications
You must be signed in to change notification settings - Fork 43
/
main_loops.go
397 lines (343 loc) · 13.9 KB
/
main_loops.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
package orchestrator
import (
"context"
"errors"
"math"
"math/big"
"time"
"github.com/avast/retry-go"
"github.com/ethereum/go-ethereum/common"
"github.com/shopspring/decimal"
log "github.com/xlab/suplog"
"github.com/InjectiveLabs/sdk-go/chain/peggy/types"
"github.com/InjectiveLabs/peggo/orchestrator/cosmos"
"github.com/InjectiveLabs/peggo/orchestrator/loops"
cosmtypes "github.com/cosmos/cosmos-sdk/types"
ethcmn "github.com/ethereum/go-ethereum/common"
)
const defaultLoopDur = 60 * time.Second
// Start combines the all major roles required to make
// up the Orchestrator, all of these are async loops.
func (s *peggyOrchestrator) Start(ctx context.Context) error {
var pg loops.ParanoidGroup
pg.Go(func() error {
return s.EthOracleMainLoop(ctx)
})
pg.Go(func() error {
return s.BatchRequesterLoop(ctx)
})
pg.Go(func() error {
return s.EthSignerMainLoop(ctx)
})
pg.Go(func() error {
return s.RelayerMainLoop(ctx)
})
return pg.Wait()
}
// EthOracleMainLoop is responsible for making sure that Ethereum events are retrieved from the Ethereum blockchain
// and ferried over to Cosmos where they will be used to issue tokens or process batches.
//
// TODO this loop requires a method to bootstrap back to the correct event nonce when restarted
func (s *peggyOrchestrator) EthOracleMainLoop(ctx context.Context) (err error) {
logger := log.WithField("loop", "EthOracleMainLoop")
lastResync := time.Now()
var lastCheckedBlock uint64
if err := retry.Do(func() (err error) {
lastCheckedBlock, err = s.GetLastCheckedBlock(ctx)
if lastCheckedBlock == 0 {
peggyParams, err := s.cosmosQueryClient.PeggyParams(ctx)
if err != nil {
log.WithError(err).Fatalln("failed to query peggy params, is injectived running?")
}
lastCheckedBlock = peggyParams.BridgeContractStartHeight
}
return
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to get last checked block, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
logger.WithField("lastCheckedBlock", lastCheckedBlock).Infoln("Start scanning for events")
return loops.RunLoop(ctx, defaultLoopDur, func() error {
// Relays events from Ethereum -> Cosmos
var currentBlock uint64
if err := retry.Do(func() (err error) {
currentBlock, err = s.CheckForEvents(ctx, lastCheckedBlock)
return
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("error during Eth event checking, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
lastCheckedBlock = currentBlock
/*
Auto re-sync to catch up the nonce. Reasons why event nonce fall behind.
1. It takes some time for events to be indexed on Ethereum. So if peggo queried events immediately as block produced, there is a chance the event is missed.
we need to re-scan this block to ensure events are not missed due to indexing delay.
2. if validator was in UnBonding state, the claims broadcasted in last iteration are failed.
3. if infura call failed while filtering events, the peggo missed to broadcast claim events occured in last iteration.
**/
if time.Since(lastResync) >= 48*time.Hour {
if err := retry.Do(func() (err error) {
lastCheckedBlock, err = s.GetLastCheckedBlock(ctx)
return
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to get last checked block, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
lastResync = time.Now()
logger.WithFields(log.Fields{"lastResync": lastResync, "lastCheckedBlock": lastCheckedBlock}).Infoln("Auto resync")
}
return nil
})
}
// EthSignerMainLoop simply signs off on any batches or validator sets provided by the validator
// since these are provided directly by a trusted Cosmsos node they can simply be assumed to be
// valid and signed off on.
func (s *peggyOrchestrator) EthSignerMainLoop(ctx context.Context) (err error) {
logger := log.WithField("loop", "EthSignerMainLoop")
var peggyID common.Hash
if err := retry.Do(func() (err error) {
peggyID, err = s.peggyContract.GetPeggyID(ctx, s.peggyContract.FromAddress())
return
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to get PeggyID from Ethereum contract, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
logger.Debugf("received peggyID %s", peggyID.Hex())
return loops.RunLoop(ctx, defaultLoopDur, func() error {
var oldestUnsignedValsets []*types.Valset
if err := retry.Do(func() error {
oldestValsets, err := s.cosmosQueryClient.OldestUnsignedValsets(ctx, s.peggyBroadcastClient.AccFromAddress())
if err != nil {
if err == cosmos.ErrNotFound || oldestValsets == nil {
logger.Debugln("no Valset waiting to be signed")
return nil
}
return err
}
oldestUnsignedValsets = oldestValsets
return nil
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to get unsigned Valset for signing, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
for _, oldestValset := range oldestUnsignedValsets {
logger.Infoln("Sending Valset confirm for %d", oldestValset.Nonce)
if err := retry.Do(func() error {
return s.peggyBroadcastClient.SendValsetConfirm(ctx, s.ethFrom, peggyID, oldestValset)
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to sign and send Valset confirmation to Cosmos, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
}
var oldestUnsignedTransactionBatch *types.OutgoingTxBatch
if err := retry.Do(func() error {
// sign the last unsigned batch, TODO check if we already have signed this
txBatch, err := s.cosmosQueryClient.OldestUnsignedTransactionBatch(ctx, s.peggyBroadcastClient.AccFromAddress())
if err != nil {
if err == cosmos.ErrNotFound || txBatch == nil {
logger.Debugln("no TransactionBatch waiting to be signed")
return nil
}
return err
}
oldestUnsignedTransactionBatch = txBatch
return nil
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to get unsigned TransactionBatch for signing, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
if oldestUnsignedTransactionBatch != nil {
logger.Infoln("Sending TransactionBatch confirm for BatchNonce %d", oldestUnsignedTransactionBatch.BatchNonce)
if err := retry.Do(func() error {
return s.peggyBroadcastClient.SendBatchConfirm(ctx, s.ethFrom, peggyID, oldestUnsignedTransactionBatch)
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to sign and send TransactionBatch confirmation to Cosmos, will retry (%d)", n)
})); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
}
return nil
})
}
// This loop doesn't have a formal role per say, anyone can request a valset
// but there does need to be some strategy to ensure requests are made. Having it
// be a function of the orchestrator makes a lot of sense as they are already online
// and have all the required funds, keys, and rpc servers setup
//
// Exactly how to balance optimizing this versus testing is an interesting discussion
// in testing we want to make sure requests are made without any powers changing on the chain
// just to simplify the test environment. But in production that's somewhat wasteful. What this
// routine does it check the current valset versus the last requested valset, if power has changed
// significantly we send in a request.
/*
Not required any more. The valset request are generated in endblocker of peggy module automatically. Also MsgSendValsetRequest is removed on peggy module.
func (s *peggyOrchestrator) ValsetRequesterLoop(ctx context.Context) (err error) {
logger := log.WithField("loop", "ValsetRequesterLoop")
return loops.RunLoop(ctx, defaultLoopDur, func() error {
var latestValsets []*types.Valset
var currentValset *types.Valset
var pg loops.ParanoidGroup
pg.Go(func() error {
return retry.Do(func() (err error) {
latestValsets, err = s.cosmosQueryClient.LatestValsets(ctx)
return
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to get latest valsets, will retry (%d)", n)
}))
})
pg.Go(func() error {
return retry.Do(func() (err error) {
currentValset, err = s.cosmosQueryClient.CurrentValset(ctx)
return
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to get current valset, will retry (%d)", n)
}))
})
if err := pg.Wait(); err != nil {
logger.WithError(err).Errorln("got error, loop exits")
return err
}
if len(latestValsets) == 0 {
retry.Do(func() error {
return s.peggyBroadcastClient.SendValsetRequest(ctx)
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to request Valset to be formed, will retry (%d)", n)
}))
} else {
// if the power difference is more than 1% different than the last valset
if valPowerDiff(latestValsets[0], currentValset) > 0.01 {
log.Debugln("power difference is more than 1%% different than the last valset. Sending valset request")
retry.Do(func() error {
return s.peggyBroadcastClient.SendValsetRequest(ctx)
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Warningf("failed to request Valset to be formed, will retry (%d)", n)
}))
}
}
return nil
})
}
**/
func (s *peggyOrchestrator) BatchRequesterLoop(ctx context.Context) (err error) {
logger := log.WithField("loop", "BatchRequesterLoop")
return loops.RunLoop(ctx, defaultLoopDur, func() error {
// get All the denominations
// check if threshold is met
// broadcast Request batch
var pg loops.ParanoidGroup
pg.Go(func() error {
var unbatchedTokensWithFees []*types.BatchFees
if err := retry.Do(func() (err error) {
unbatchedTokensWithFees, err = s.cosmosQueryClient.UnbatchedTokensWithFees(ctx)
return
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Errorf("failed to get UnbatchedTokensWithFees, will retry (%d)", n)
})); err != nil {
// non-fatal, just alert
logger.Warningln("unable to get UnbatchedTokensWithFees for the token")
return nil
}
if len(unbatchedTokensWithFees) > 0 {
logger.WithField("unbatchedTokensWithFees", unbatchedTokensWithFees).Debugln("Check if token fees meets set threshold amount and send batch request")
for _, unbatchedToken := range unbatchedTokensWithFees {
return retry.Do(func() (err error) {
// check if the token is present in cosmos denom. if so, send batch request with cosmosDenom
tokenAddr := ethcmn.HexToAddress(unbatchedToken.Token)
var denom string
if cosmosDenom, ok := s.erc20ContractMapping[tokenAddr]; ok {
// cosmos denom
denom = cosmosDenom
} else {
// peggy denom
denom = types.PeggyDenomString(tokenAddr)
}
// send batch request only if fee threshold is met.
if s.CheckFeeThreshod(tokenAddr, unbatchedToken.TotalFees, s.minBatchFeeUSD) {
logger.WithFields(log.Fields{"tokenContract": tokenAddr, "denom": denom}).Infoln("sending batch request")
_ = s.peggyBroadcastClient.SendRequestBatch(ctx, denom)
}
return nil
}, retry.Context(ctx), retry.OnRetry(func(n uint, err error) {
logger.WithError(err).Errorf("failed to get LatestUnbatchOutgoingTx, will retry (%d)", n)
}))
}
} else {
logger.Debugln("No outgoing withdraw tx or Unbatched token fee less than threshold")
}
return nil
})
return pg.Wait()
})
}
func (s *peggyOrchestrator) CheckFeeThreshod(erc20Contract common.Address, totalFee cosmtypes.Int, minFeeInUSD float64) bool {
if minFeeInUSD == 0 {
return true
}
tokenPriceInUSD, err := s.priceFeeder.QueryUSDPrice(erc20Contract)
if err != nil {
return false
}
tokenPriceInUSDDec := decimal.NewFromFloat(tokenPriceInUSD)
totalFeeInUSDDec := decimal.NewFromBigInt(totalFee.BigInt(), -18).Mul(tokenPriceInUSDDec)
minFeeInUSDDec := decimal.NewFromFloat(minFeeInUSD)
if totalFeeInUSDDec.GreaterThan(minFeeInUSDDec) {
return true
}
return false
}
func (s *peggyOrchestrator) RelayerMainLoop(ctx context.Context) (err error) {
if s.relayer != nil {
return s.relayer.Start(ctx)
} else {
return errors.New("relayer is nil")
}
}
// valPowerDiff returns the difference in power between two bridge validator sets
// TODO: this needs to be potentially refactored
func valPowerDiff(old *types.Valset, new *types.Valset) float64 {
powers := map[string]int64{}
var totalB int64
// loop over b and initialize the map with their powers
for _, bv := range old.GetMembers() {
powers[bv.EthereumAddress] = int64(bv.Power)
totalB += int64(bv.Power)
}
// subtract c powers from powers in the map, initializing
// uninitialized keys with negative numbers
for _, bv := range new.GetMembers() {
if val, ok := powers[bv.EthereumAddress]; ok {
powers[bv.EthereumAddress] = val - int64(bv.Power)
} else {
powers[bv.EthereumAddress] = -int64(bv.Power)
}
}
var delta float64
for _, v := range powers {
// NOTE: we care about the absolute value of the changes
delta += math.Abs(float64(v))
}
return math.Abs(delta / float64(totalB))
}
func calculateTotalValsetPower(valset *types.Valset) *big.Int {
totalValsetPower := new(big.Int)
for _, m := range valset.Members {
mPower := big.NewInt(0).SetUint64(m.Power)
totalValsetPower.Add(totalValsetPower, mPower)
}
return totalValsetPower
}