-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
registry_synchronizer_process_logs.go
274 lines (222 loc) · 9.83 KB
/
registry_synchronizer_process_logs.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
package keeper
import (
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/log"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
registry1_1 "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/keeper_registry_wrapper1_1"
registry1_2 "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/keeper_registry_wrapper1_2"
registry1_3 "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/keeper_registry_wrapper1_3"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
)
func (rs *RegistrySynchronizer) processLogs() {
for _, broadcast := range rs.mbLogs.RetrieveAll() {
eventLog := broadcast.DecodedLog()
if eventLog == nil || reflect.ValueOf(eventLog).IsNil() {
rs.logger.Panicf("processLogs: ignoring nil value, type: %T", eventLog)
continue
}
was, err := rs.logBroadcaster.WasAlreadyConsumed(broadcast)
if err != nil {
rs.logger.Warn(errors.Wrap(err, "unable to check if log was consumed"))
continue
} else if was {
continue
}
switch eventLog.(type) {
case *registry1_1.KeeperRegistryKeepersUpdated,
*registry1_1.KeeperRegistryConfigSet,
*registry1_2.KeeperRegistryKeepersUpdated,
*registry1_2.KeeperRegistryConfigSet,
*registry1_3.KeeperRegistryKeepersUpdated,
*registry1_3.KeeperRegistryConfigSet:
err = rs.handleSyncRegistryLog(broadcast)
case *registry1_1.KeeperRegistryUpkeepCanceled,
*registry1_2.KeeperRegistryUpkeepCanceled,
*registry1_3.KeeperRegistryUpkeepCanceled:
err = rs.handleUpkeepCancelled(broadcast)
case *registry1_1.KeeperRegistryUpkeepRegistered,
*registry1_2.KeeperRegistryUpkeepRegistered,
*registry1_3.KeeperRegistryUpkeepRegistered:
err = rs.handleUpkeepRegistered(broadcast)
case *registry1_1.KeeperRegistryUpkeepPerformed,
*registry1_2.KeeperRegistryUpkeepPerformed,
*registry1_3.KeeperRegistryUpkeepPerformed:
err = rs.handleUpkeepPerformed(broadcast)
case *registry1_2.KeeperRegistryUpkeepGasLimitSet,
*registry1_3.KeeperRegistryUpkeepGasLimitSet:
err = rs.handleUpkeepGasLimitSet(broadcast)
case *registry1_2.KeeperRegistryUpkeepReceived,
*registry1_3.KeeperRegistryUpkeepReceived:
err = rs.handleUpkeepReceived(broadcast)
case *registry1_2.KeeperRegistryUpkeepMigrated,
*registry1_3.KeeperRegistryUpkeepMigrated:
err = rs.handleUpkeepMigrated(broadcast)
case *registry1_3.KeeperRegistryUpkeepPaused:
err = rs.handleUpkeepPaused(broadcast)
case *registry1_3.KeeperRegistryUpkeepUnpaused:
err = rs.handleUpkeepUnpaused(broadcast)
case *registry1_3.KeeperRegistryUpkeepCheckDataUpdated:
err = rs.handleUpkeepCheckDataUpdated(broadcast)
default:
rs.logger.Warn("unexpected log type")
// Don't `continue` -- we still want to mark this log as consumed
}
if err != nil {
rs.logger.Error(err)
}
err = rs.logBroadcaster.MarkConsumed(broadcast)
if err != nil {
rs.logger.Error(errors.Wrapf(err, "unable to mark %T log as consumed, log: %v", broadcast.RawLog(), broadcast.String()))
}
}
}
func (rs *RegistrySynchronizer) handleSyncRegistryLog(broadcast log.Broadcast) error {
rs.logger.Debugw("processing SyncRegistry log", "txHash", broadcast.RawLog().TxHash.Hex())
_, err := rs.syncRegistry()
if err != nil {
return errors.Wrap(err, "unable to sync registry")
}
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepCancelled(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepCanceled log", "txHash", broadcast.RawLog().TxHash.Hex())
cancelledID, err := rs.registryWrapper.GetCancelledUpkeepIDFromLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch cancelled upkeep ID from log")
}
affected, err := rs.orm.BatchDeleteUpkeepsForJob(rs.job.ID, []big.Big{*big.New(cancelledID)})
if err != nil {
return errors.Wrap(err, "unable to batch delete upkeeps")
}
rs.logger.Debugw(fmt.Sprintf("deleted %v upkeep registrations", affected), "txHash", broadcast.RawLog().TxHash.Hex())
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepRegistered(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepRegistered log", "txHash", broadcast.RawLog().TxHash.Hex())
registry, err := rs.orm.RegistryForJob(rs.job.ID)
if err != nil {
return errors.Wrap(err, "unable to find registry for job")
}
upkeepID, err := rs.registryWrapper.GetUpkeepIdFromRegistrationLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch upkeep ID from registration log")
}
err = rs.syncUpkeep(&rs.registryWrapper, registry, big.New(upkeepID))
if err != nil {
return errors.Wrapf(err, "failed to sync upkeep, log: %v", broadcast.String())
}
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepPerformed(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepPerformed log", "jobID", rs.job.ID, "txHash", broadcast.RawLog().TxHash.Hex())
log, err := rs.registryWrapper.ParseUpkeepPerformedLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch upkeep ID from performed log")
}
rowsAffected, err := rs.orm.SetLastRunInfoForUpkeepOnJob(rs.job.ID, big.New(log.UpkeepID), int64(broadcast.RawLog().BlockNumber), ethkey.EIP55AddressFromAddress(log.FromKeeper))
if err != nil {
return errors.Wrap(err, "failed to set last run to 0")
}
rs.logger.Debugw("updated db for UpkeepPerformed log",
"jobID", rs.job.ID,
"upkeepID", log.UpkeepID.String(),
"blockNumber", int64(broadcast.RawLog().BlockNumber),
"fromAddr", ethkey.EIP55AddressFromAddress(log.FromKeeper),
"rowsAffected", rowsAffected,
)
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepGasLimitSet(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepGasLimitSet log", "jobID", rs.job.ID, "txHash", broadcast.RawLog().TxHash.Hex())
registry, err := rs.orm.RegistryForJob(rs.job.ID)
if err != nil {
return errors.Wrap(err, "unable to find registry for job")
}
upkeepID, err := rs.registryWrapper.GetIDFromGasLimitSetLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch upkeep ID from gas limit set log")
}
err = rs.syncUpkeep(&rs.registryWrapper, registry, big.New(upkeepID))
if err != nil {
return errors.Wrapf(err, "failed to sync upkeep, log: %v", broadcast.String())
}
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepReceived(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepReceived log", "txHash", broadcast.RawLog().TxHash.Hex())
registry, err := rs.orm.RegistryForJob(rs.job.ID)
if err != nil {
return errors.Wrap(err, "unable to find registry for job")
}
upkeepID, err := rs.registryWrapper.GetUpkeepIdFromReceivedLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch upkeep ID from received log")
}
err = rs.syncUpkeep(&rs.registryWrapper, registry, big.New(upkeepID))
if err != nil {
return errors.Wrapf(err, "failed to sync upkeep, log: %v", broadcast.String())
}
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepMigrated(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepMigrated log", "txHash", broadcast.RawLog().TxHash.Hex())
migratedID, err := rs.registryWrapper.GetUpkeepIdFromMigratedLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch migrated upkeep ID from log")
}
affected, err := rs.orm.BatchDeleteUpkeepsForJob(rs.job.ID, []big.Big{*big.New(migratedID)})
if err != nil {
return errors.Wrap(err, "unable to batch delete upkeeps")
}
rs.logger.Debugw(fmt.Sprintf("deleted %v upkeep registrations", affected), "txHash", broadcast.RawLog().TxHash.Hex())
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepPaused(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepPaused log", "txHash", broadcast.RawLog().TxHash.Hex())
pausedUpkeepId, err := rs.registryWrapper.GetUpkeepIdFromUpkeepPausedLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch upkeep ID from upkeep paused log")
}
_, err = rs.orm.BatchDeleteUpkeepsForJob(rs.job.ID, []big.Big{*big.New(pausedUpkeepId)})
if err != nil {
return errors.Wrap(err, "unable to batch delete upkeeps")
}
rs.logger.Debugw(fmt.Sprintf("paused upkeep %s", pausedUpkeepId.String()), "txHash", broadcast.RawLog().TxHash.Hex())
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepUnpaused(broadcast log.Broadcast) error {
rs.logger.Debugw("processing UpkeepUnpaused log", "txHash", broadcast.RawLog().TxHash.Hex())
registry, err := rs.orm.RegistryForJob(rs.job.ID)
if err != nil {
return errors.Wrap(err, "unable to find registry for job")
}
unpausedUpkeepId, err := rs.registryWrapper.GetUpkeepIdFromUpkeepUnpausedLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to fetch upkeep ID from upkeep unpaused log")
}
err = rs.syncUpkeep(&rs.registryWrapper, registry, big.New(unpausedUpkeepId))
if err != nil {
return errors.Wrapf(err, "failed to sync upkeep, log: %s", broadcast.String())
}
rs.logger.Debugw(fmt.Sprintf("unpaused upkeep %s", unpausedUpkeepId.String()), "txHash", broadcast.RawLog().TxHash.Hex())
return nil
}
func (rs *RegistrySynchronizer) handleUpkeepCheckDataUpdated(broadcast log.Broadcast) error {
rs.logger.Debugw("processing Upkeep check data updated log", "txHash", broadcast.RawLog().TxHash.Hex())
registry, err := rs.orm.RegistryForJob(rs.job.ID)
if err != nil {
return errors.Wrap(err, "unable to find registry for job")
}
updateLog, err := rs.registryWrapper.ParseUpkeepCheckDataUpdatedLog(broadcast)
if err != nil {
return errors.Wrap(err, "Unable to parse update log from upkeep check data updated log")
}
err = rs.syncUpkeep(&rs.registryWrapper, registry, big.New(updateLog.UpkeepID))
if err != nil {
return errors.Wrapf(err, "unable to update check data for upkeep %s", updateLog.UpkeepID.String())
}
rs.logger.Debugw(fmt.Sprintf("updated check data for upkeep %s", updateLog.UpkeepID.String()), "txHash", broadcast.RawLog().TxHash.Hex())
return nil
}