-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
registry_interface.go
387 lines (358 loc) · 13.1 KB
/
registry_interface.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
package keeper
import (
"context"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
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"
type_and_version "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/type_and_version_interface_wrapper"
)
type RegistryVersion int32
const (
RegistryVersion_1_0 RegistryVersion = iota
RegistryVersion_1_1
RegistryVersion_1_2
RegistryVersion_1_3
RegistryVersion_2_0
RegistryVersion_2_1
)
func (rv RegistryVersion) String() string {
switch rv {
case RegistryVersion_1_0, RegistryVersion_1_1, RegistryVersion_1_2, RegistryVersion_1_3:
return fmt.Sprintf("v1.%d", rv)
case RegistryVersion_2_0:
return "v2.0"
default:
return "unknown registry version"
}
}
const ActiveUpkeepIDBatchSize int64 = 10000
// upkeepGetter is declared as a private interface as it is only needed
// internally to the keeper package for now
type upkeepGetter interface {
GetUpkeep(*bind.CallOpts, *big.Int) (*UpkeepConfig, error)
}
// RegistryWrapper implements a layer on top of different versions of registry wrappers
// to provide a unified layer to rest of the codebase
type RegistryWrapper struct {
Address evmtypes.EIP55Address
Version RegistryVersion
contract1_1 *registry1_1.KeeperRegistry
contract1_2 *registry1_2.KeeperRegistry
contract1_3 *registry1_3.KeeperRegistry
evmClient evmclient.Client
}
func NewRegistryWrapper(address evmtypes.EIP55Address, evmClient evmclient.Client) (*RegistryWrapper, error) {
interface_wrapper, err := type_and_version.NewTypeAndVersionInterface(
address.Address(),
evmClient,
)
if err != nil {
return nil, errors.Wrap(err, "unable to create type and interface wrapper")
}
version, err := getRegistryVersion(interface_wrapper)
if err != nil {
return nil, errors.Wrap(err, "unable to determine version of keeper registry contract")
}
contract1_1, err := registry1_1.NewKeeperRegistry(
address.Address(),
evmClient,
)
if err != nil {
return nil, errors.Wrap(err, "unable to create keeper registry 1_1 contract wrapper")
}
contract1_2, err := registry1_2.NewKeeperRegistry(
address.Address(),
evmClient,
)
if err != nil {
return nil, errors.Wrap(err, "unable to create keeper registry 1_2 contract wrapper")
}
contract1_3, err := registry1_3.NewKeeperRegistry(
address.Address(),
evmClient,
)
if err != nil {
return nil, errors.Wrap(err, "unable to create keeper registry 1_3 contract wrapper")
}
return &RegistryWrapper{
Address: address,
Version: *version,
contract1_1: contract1_1,
contract1_2: contract1_2,
contract1_3: contract1_3,
evmClient: evmClient,
}, nil
}
func getRegistryVersion(contract *type_and_version.TypeAndVersionInterface) (*RegistryVersion, error) {
typeAndVersion, err := contract.TypeAndVersion(nil)
if err != nil {
jsonErr := evmclient.ExtractRPCErrorOrNil(err)
if jsonErr != nil {
// Version 1.0 does not support typeAndVersion interface, hence gives a json error on this call
version := RegistryVersion_1_0
return &version, nil
}
return nil, errors.Wrap(err, "unable to fetch version of registry")
}
switch {
case strings.HasPrefix(typeAndVersion, "KeeperRegistry 1.1"):
version := RegistryVersion_1_1
return &version, nil
case strings.HasPrefix(typeAndVersion, "KeeperRegistry 1.2"):
version := RegistryVersion_1_2
return &version, nil
case strings.HasPrefix(typeAndVersion, "KeeperRegistry 1.3"):
version := RegistryVersion_1_3
return &version, nil
default:
return nil, errors.Errorf("Registry type and version %s not supported", typeAndVersion)
}
}
func newUnsupportedVersionError(functionName string, version RegistryVersion) error {
return errors.Errorf("Registry version %d does not support %s", version, functionName)
}
// getUpkeepCount retrieves the number of upkeeps
func (rw *RegistryWrapper) getUpkeepCount(opts *bind.CallOpts) (*big.Int, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
upkeepCount, err := rw.contract1_1.GetUpkeepCount(opts)
if err != nil {
return nil, errors.Wrap(err, "failed to get upkeep count")
}
return upkeepCount, nil
case RegistryVersion_1_2:
state, err := rw.contract1_2.GetState(opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to get contract state at block number %d", opts.BlockNumber.Int64())
}
return state.State.NumUpkeeps, nil
case RegistryVersion_1_3:
state, err := rw.contract1_3.GetState(opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to get contract state at block number %d", opts.BlockNumber.Int64())
}
return state.State.NumUpkeeps, nil
default:
return nil, newUnsupportedVersionError("getUpkeepCount", rw.Version)
}
}
func (rw *RegistryWrapper) GetActiveUpkeepIDs(opts *bind.CallOpts) ([]*big.Int, error) {
if opts == nil || opts.BlockNumber.Int64() == 0 {
var head *evmtypes.Head
// fetch the current block number so batched GetActiveUpkeepIDs calls can be performed on the same block
head, err := rw.evmClient.HeadByNumber(context.Background(), nil)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch EVM block header")
}
if opts != nil {
opts.BlockNumber = big.NewInt(head.Number)
} else {
opts = &bind.CallOpts{
BlockNumber: big.NewInt(head.Number),
}
}
}
upkeepCount, err := rw.getUpkeepCount(opts)
if err != nil {
return nil, errors.Wrap(err, "failed to get upkeep count")
}
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
cancelledUpkeeps, err2 := rw.contract1_1.GetCanceledUpkeepList(opts)
if err2 != nil {
return nil, errors.Wrap(err2, "failed to get cancelled upkeeps")
}
cancelledSet := make(map[int64]bool)
for _, upkeepID := range cancelledUpkeeps {
cancelledSet[upkeepID.Int64()] = true
}
// Active upkeep IDs are 0,1 ... upkeepCount-1, removing the cancelled ones
activeUpkeeps := make([]*big.Int, 0)
for i := int64(0); i < upkeepCount.Int64(); i++ {
if _, found := cancelledSet[i]; !found {
activeUpkeeps = append(activeUpkeeps, big.NewInt(i))
}
}
return activeUpkeeps, nil
case RegistryVersion_1_2, RegistryVersion_1_3:
activeUpkeepIDs := make([]*big.Int, 0)
var activeUpkeepIDBatch []*big.Int
for int64(len(activeUpkeepIDs)) < upkeepCount.Int64() {
startIndex := int64(len(activeUpkeepIDs))
maxCount := upkeepCount.Int64() - int64(len(activeUpkeepIDs))
if maxCount > ActiveUpkeepIDBatchSize {
maxCount = ActiveUpkeepIDBatchSize
}
if rw.Version == RegistryVersion_1_2 {
activeUpkeepIDBatch, err = rw.contract1_2.GetActiveUpkeepIDs(opts, big.NewInt(startIndex), big.NewInt(maxCount))
} else {
activeUpkeepIDBatch, err = rw.contract1_3.GetActiveUpkeepIDs(opts, big.NewInt(startIndex), big.NewInt(maxCount))
}
if err != nil {
return nil, errors.Wrapf(err, "failed to get active upkeep IDs from index %d to %d (both inclusive)", startIndex, startIndex+maxCount-1)
}
activeUpkeepIDs = append(activeUpkeepIDs, activeUpkeepIDBatch...)
}
return activeUpkeepIDs, nil
default:
return nil, newUnsupportedVersionError("GetActiveUpkeepIDs", rw.Version)
}
}
type UpkeepConfig struct {
ExecuteGas uint32
CheckData []byte
LastKeeper common.Address
}
func (rw *RegistryWrapper) GetUpkeep(opts *bind.CallOpts, id *big.Int) (*UpkeepConfig, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
upkeep, err := rw.contract1_1.GetUpkeep(opts, id)
if err != nil {
return nil, errors.Wrap(err, "failed to get upkeep config")
}
return &UpkeepConfig{
ExecuteGas: upkeep.ExecuteGas,
CheckData: upkeep.CheckData,
LastKeeper: upkeep.LastKeeper,
}, nil
case RegistryVersion_1_2:
upkeep, err := rw.contract1_2.GetUpkeep(opts, id)
if err != nil {
return nil, errors.Wrap(err, "failed to get upkeep config")
}
return &UpkeepConfig{
ExecuteGas: upkeep.ExecuteGas,
CheckData: upkeep.CheckData,
LastKeeper: upkeep.LastKeeper,
}, nil
case RegistryVersion_1_3:
upkeep, err := rw.contract1_3.GetUpkeep(opts, id)
if err != nil {
return nil, errors.Wrap(err, "failed to get upkeep config")
}
return &UpkeepConfig{
ExecuteGas: upkeep.ExecuteGas,
CheckData: upkeep.CheckData,
LastKeeper: upkeep.LastKeeper,
}, nil
default:
return nil, newUnsupportedVersionError("GetUpkeep", rw.Version)
}
}
type RegistryConfig struct {
BlockCountPerTurn int32
CheckGas uint32
KeeperAddresses []common.Address
}
func (rw *RegistryWrapper) GetConfig(opts *bind.CallOpts) (*RegistryConfig, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
config, err := rw.contract1_1.GetConfig(opts)
if err != nil {
// TODO: error wrapping with %w should be done here to preserve the error type as it bubbles up
// pkg/errors doesn't support the native errors.Is/As capabilities
// using pkg/errors produces a stack trace in the logs and this behavior is too valuable to let go
return nil, errors.Errorf("%s [%s]: getConfig %s", ErrContractCallFailure, err, rw.Version)
}
keeperAddresses, err := rw.contract1_1.GetKeeperList(opts)
if err != nil {
return nil, errors.Errorf("%s [%s]: getKeeperList %s", ErrContractCallFailure, err, rw.Version)
}
return &RegistryConfig{
BlockCountPerTurn: int32(config.BlockCountPerTurn.Int64()),
CheckGas: config.CheckGasLimit,
KeeperAddresses: keeperAddresses,
}, nil
case RegistryVersion_1_2:
state, err := rw.contract1_2.GetState(opts)
if err != nil {
return nil, errors.Errorf("%s [%s]: getState %s", ErrContractCallFailure, err, rw.Version)
}
return &RegistryConfig{
BlockCountPerTurn: int32(state.Config.BlockCountPerTurn.Int64()),
CheckGas: state.Config.CheckGasLimit,
KeeperAddresses: state.Keepers,
}, nil
case RegistryVersion_1_3:
state, err := rw.contract1_3.GetState(opts)
if err != nil {
return nil, errors.Errorf("%s [%s]: getState %s", ErrContractCallFailure, err, rw.Version)
}
return &RegistryConfig{
BlockCountPerTurn: int32(state.Config.BlockCountPerTurn.Int64()),
CheckGas: state.Config.CheckGasLimit,
KeeperAddresses: state.Keepers,
}, nil
default:
return nil, newUnsupportedVersionError("GetConfig", rw.Version)
}
}
func (rw *RegistryWrapper) SetKeepers(opts *bind.TransactOpts, keepers []common.Address, payees []common.Address) (*types.Transaction, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
return rw.contract1_1.SetKeepers(opts, keepers, payees)
case RegistryVersion_1_2:
return rw.contract1_2.SetKeepers(opts, keepers, payees)
case RegistryVersion_1_3:
return rw.contract1_3.SetKeepers(opts, keepers, payees)
default:
return nil, newUnsupportedVersionError("SetKeepers", rw.Version)
}
}
func (rw *RegistryWrapper) RegisterUpkeep(opts *bind.TransactOpts, target common.Address, gasLimit uint32, admin common.Address, checkData []byte) (*types.Transaction, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
return rw.contract1_1.RegisterUpkeep(opts, target, gasLimit, admin, checkData)
case RegistryVersion_1_2:
return rw.contract1_2.RegisterUpkeep(opts, target, gasLimit, admin, checkData)
case RegistryVersion_1_3:
return rw.contract1_3.RegisterUpkeep(opts, target, gasLimit, admin, checkData)
default:
return nil, newUnsupportedVersionError("RegisterUpkeep", rw.Version)
}
}
func (rw *RegistryWrapper) AddFunds(opts *bind.TransactOpts, id *big.Int, amount *big.Int) (*types.Transaction, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
return rw.contract1_1.AddFunds(opts, id, amount)
case RegistryVersion_1_2:
return rw.contract1_2.AddFunds(opts, id, amount)
case RegistryVersion_1_3:
return rw.contract1_3.AddFunds(opts, id, amount)
default:
return nil, newUnsupportedVersionError("AddFunds", rw.Version)
}
}
func (rw *RegistryWrapper) PerformUpkeep(opts *bind.TransactOpts, id *big.Int, performData []byte) (*types.Transaction, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
return rw.contract1_1.PerformUpkeep(opts, id, performData)
case RegistryVersion_1_2:
return rw.contract1_2.PerformUpkeep(opts, id, performData)
case RegistryVersion_1_3:
return rw.contract1_3.PerformUpkeep(opts, id, performData)
default:
return nil, newUnsupportedVersionError("PerformUpkeep", rw.Version)
}
}
func (rw *RegistryWrapper) CancelUpkeep(opts *bind.TransactOpts, id *big.Int) (*types.Transaction, error) {
switch rw.Version {
case RegistryVersion_1_0, RegistryVersion_1_1:
return rw.contract1_1.CancelUpkeep(opts, id)
case RegistryVersion_1_2:
return rw.contract1_2.CancelUpkeep(opts, id)
case RegistryVersion_1_3:
return rw.contract1_3.CancelUpkeep(opts, id)
default:
return nil, newUnsupportedVersionError("CancelUpkeep", rw.Version)
}
}