-
Notifications
You must be signed in to change notification settings - Fork 43
/
eth_event_watcher.go
243 lines (202 loc) · 7.31 KB
/
eth_event_watcher.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
package orchestrator
import (
"context"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/pkg/errors"
log "github.com/xlab/suplog"
"github.com/InjectiveLabs/metrics"
wrappers "github.com/InjectiveLabs/peggo/solidity/wrappers/Peggy.sol"
)
// Considering blocktime of up to 3 seconds approx on the Injective Chain and an oracle loop duration = 1 minute,
// we broadcast only 20 events in each iteration.
// So better to search only 20 blocks to ensure all the events are broadcast to Injective Chain without misses.
const defaultBlocksToSearch = 20
const ethBlockConfirmationDelay = 12
// CheckForEvents checks for events such as a deposit to the Peggy Ethereum contract or a validator set update
// or a transaction batch update. It then responds to these events by performing actions on the Cosmos chain if required
func (s *peggyOrchestrator) CheckForEvents(
ctx context.Context,
startingBlock uint64,
) (currentBlock uint64, err error) {
metrics.ReportFuncCall(s.svcTags)
doneFn := metrics.ReportFuncTiming(s.svcTags)
defer doneFn()
latestHeader, err := s.ethProvider.HeaderByNumber(ctx, nil)
if err != nil {
metrics.ReportFuncError(s.svcTags)
err = errors.Wrap(err, "failed to get latest header")
return 0, err
}
// add delay to ensure minimum confirmations are received and block is finalised
currentBlock = latestHeader.Number.Uint64() - uint64(ethBlockConfirmationDelay)
if currentBlock < startingBlock {
return currentBlock, nil
}
if (currentBlock - startingBlock) > defaultBlocksToSearch {
currentBlock = startingBlock + defaultBlocksToSearch
}
peggyFilterer, err := wrappers.NewPeggyFilterer(s.peggyContract.Address(), s.ethProvider)
if err != nil {
metrics.ReportFuncError(s.svcTags)
err = errors.Wrap(err, "failed to init Peggy events filterer")
return 0, err
}
var sendToCosmosEvents []*wrappers.PeggySendToInjectiveEvent
{
iter, err := peggyFilterer.FilterSendToInjectiveEvent(&bind.FilterOpts{
Start: startingBlock,
End: ¤tBlock,
}, nil, nil, nil)
if err != nil {
metrics.ReportFuncError(s.svcTags)
log.WithFields(log.Fields{
"start": startingBlock,
"end": currentBlock,
}).Errorln("failed to scan past SendToCosmos events from Ethereum")
if !isUnknownBlockErr(err) {
err = errors.Wrap(err, "failed to scan past SendToCosmos events from Ethereum")
return 0, err
} else if iter == nil {
return 0, errors.New("no iterator returned")
}
}
for iter.Next() {
sendToCosmosEvents = append(sendToCosmosEvents, iter.Event)
}
iter.Close()
}
log.WithFields(log.Fields{
"start": startingBlock,
"end": currentBlock,
"Deposits": sendToCosmosEvents,
}).Debugln("Scanned SendToCosmos events from Ethereum")
var transactionBatchExecutedEvents []*wrappers.PeggyTransactionBatchExecutedEvent
{
iter, err := peggyFilterer.FilterTransactionBatchExecutedEvent(&bind.FilterOpts{
Start: startingBlock,
End: ¤tBlock,
}, nil, nil)
if err != nil {
metrics.ReportFuncError(s.svcTags)
log.WithFields(log.Fields{
"start": startingBlock,
"end": currentBlock,
}).Errorln("failed to scan past TransactionBatchExecuted events from Ethereum")
if !isUnknownBlockErr(err) {
err = errors.Wrap(err, "failed to scan past TransactionBatchExecuted events from Ethereum")
return 0, err
} else if iter == nil {
return 0, errors.New("no iterator returned")
}
}
for iter.Next() {
transactionBatchExecutedEvents = append(transactionBatchExecutedEvents, iter.Event)
}
iter.Close()
}
log.WithFields(log.Fields{
"start": startingBlock,
"end": currentBlock,
"Withdraws": transactionBatchExecutedEvents,
}).Debugln("Scanned TransactionBatchExecuted events from Ethereum")
var valsetUpdatedEvents []*wrappers.PeggyValsetUpdatedEvent
{
iter, err := peggyFilterer.FilterValsetUpdatedEvent(&bind.FilterOpts{
Start: startingBlock,
End: ¤tBlock,
}, nil)
if err != nil {
metrics.ReportFuncError(s.svcTags)
log.WithFields(log.Fields{
"start": startingBlock,
"end": currentBlock,
}).Errorln("failed to scan past ValsetUpdatedEvent events from Ethereum")
if !isUnknownBlockErr(err) {
err = errors.Wrap(err, "failed to scan past ValsetUpdatedEvent events from Ethereum")
return 0, err
} else if iter == nil {
return 0, errors.New("no iterator returned")
}
}
for iter.Next() {
valsetUpdatedEvents = append(valsetUpdatedEvents, iter.Event)
}
iter.Close()
}
log.WithFields(log.Fields{
"start": startingBlock,
"end": currentBlock,
"valsetUpdates": valsetUpdatedEvents,
}).Debugln("Scanned ValsetUpdatedEvents events from Ethereum")
// note that starting block overlaps with our last che cked block, because we have to deal with
// the possibility that the relayer was killed after relaying only one of multiple events in a single
// block, so we also need this routine so make sure we don't send in the first event in this hypothetical
// multi event block again. In theory we only send all events for every block and that will pass of fail
// atomically but lets not take that risk.
lastClaimEvent, err := s.cosmosQueryClient.LastClaimEventByAddr(ctx, s.peggyBroadcastClient.AccFromAddress())
if err != nil {
metrics.ReportFuncError(s.svcTags)
err = errors.New("failed to query last claim event from backend")
return 0, err
}
deposits := filterSendToCosmosEventsByNonce(sendToCosmosEvents, lastClaimEvent.EthereumEventNonce)
withdraws := filterTransactionBatchExecutedEventsByNonce(transactionBatchExecutedEvents, lastClaimEvent.EthereumEventNonce)
valsetUpdates := filterValsetUpdateEventsByNonce(valsetUpdatedEvents, lastClaimEvent.EthereumEventNonce)
if len(deposits) > 0 || len(withdraws) > 0 || len(valsetUpdates) > 0 {
// todo get eth chain id from the chain
if err := s.peggyBroadcastClient.SendEthereumClaims(ctx, lastClaimEvent.EthereumEventNonce, deposits, withdraws, valsetUpdates); err != nil {
metrics.ReportFuncError(s.svcTags)
err = errors.Wrap(err, "failed to send ethereum claims to Cosmos chain")
return 0, err
}
}
return currentBlock, nil
}
func filterSendToCosmosEventsByNonce(
events []*wrappers.PeggySendToInjectiveEvent,
nonce uint64,
) []*wrappers.PeggySendToInjectiveEvent {
res := make([]*wrappers.PeggySendToInjectiveEvent, 0, len(events))
for _, ev := range events {
if ev.EventNonce.Uint64() > nonce {
res = append(res, ev)
}
}
return res
}
func filterTransactionBatchExecutedEventsByNonce(
events []*wrappers.PeggyTransactionBatchExecutedEvent,
nonce uint64,
) []*wrappers.PeggyTransactionBatchExecutedEvent {
res := make([]*wrappers.PeggyTransactionBatchExecutedEvent, 0, len(events))
for _, ev := range events {
if ev.EventNonce.Uint64() > nonce {
res = append(res, ev)
}
}
return res
}
func filterValsetUpdateEventsByNonce(
events []*wrappers.PeggyValsetUpdatedEvent,
nonce uint64,
) []*wrappers.PeggyValsetUpdatedEvent {
res := make([]*wrappers.PeggyValsetUpdatedEvent, 0, len(events))
for _, ev := range events {
if ev.EventNonce.Uint64() > nonce {
res = append(res, ev)
}
}
return res
}
func isUnknownBlockErr(err error) bool {
// Geth error
if strings.Contains(err.Error(), "unknown block") {
return true
}
// Parity error
if strings.Contains(err.Error(), "One of the blocks specified in filter") {
return true
}
return false
}