-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
coordinators.go
335 lines (299 loc) · 9.3 KB
/
coordinators.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
package blockhashstore
import (
"context"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
v1 "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/solidity_vrf_coordinator_interface"
v2 "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/vrf_coordinator_v2"
v2plus "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/vrf_coordinator_v2plus_interface"
)
var (
_ Coordinator = MultiCoordinator{}
_ Coordinator = &V1Coordinator{}
_ Coordinator = &V2Coordinator{}
_ Coordinator = &V2PlusCoordinator{}
)
// MultiCoordinator combines the data from multiple coordinators.
type MultiCoordinator []Coordinator
// NewMultiCoordinator creates a new Coordinator that combines the results of the given
// coordinators.
func NewMultiCoordinator(coordinators ...Coordinator) Coordinator {
if len(coordinators) == 1 {
return coordinators[0]
}
return MultiCoordinator(coordinators)
}
// Requests satisfies the Coordinator interface.
func (m MultiCoordinator) Requests(
ctx context.Context,
fromBlock uint64,
toBlock uint64,
) ([]Event, error) {
var reqs []Event
for _, c := range m {
r, err := c.Requests(ctx, fromBlock, toBlock)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
reqs = append(reqs, r...)
}
return reqs, nil
}
// Fulfillments satisfies the Coordinator interface.
func (m MultiCoordinator) Fulfillments(ctx context.Context, fromBlock uint64) ([]Event, error) {
var fuls []Event
for _, c := range m {
f, err := c.Fulfillments(ctx, fromBlock)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
fuls = append(fuls, f...)
}
return fuls, nil
}
// V1Coordinator fetches request and fulfillment logs from a VRF V1 coordinator contract.
type V1Coordinator struct {
c v1.VRFCoordinatorInterface
lp logpoller.LogPoller
}
// NewV1Coordinator creates a new V1Coordinator from the given contract.
func NewV1Coordinator(ctx context.Context, c v1.VRFCoordinatorInterface, lp logpoller.LogPoller) (*V1Coordinator, error) {
err := lp.RegisterFilter(ctx, logpoller.Filter{
Name: logpoller.FilterName("VRFv1CoordinatorFeeder", c.Address()),
EventSigs: []common.Hash{
v1.VRFCoordinatorRandomnessRequest{}.Topic(),
v1.VRFCoordinatorRandomnessRequestFulfilled{}.Topic(),
}, Addresses: []common.Address{c.Address()},
})
if err != nil {
return nil, err
}
return &V1Coordinator{c, lp}, nil
}
// Requests satisfies the Coordinator interface.
func (v *V1Coordinator) Requests(
ctx context.Context,
fromBlock uint64,
toBlock uint64,
) ([]Event, error) {
logs, err := v.lp.LogsWithSigs(
ctx,
int64(fromBlock),
int64(toBlock),
[]common.Hash{
v1.VRFCoordinatorRandomnessRequest{}.Topic(),
},
v.c.Address())
if err != nil {
return nil, errors.Wrap(err, "filter v1 requests")
}
var reqs []Event
for _, l := range logs {
requestLog, err := v.c.ParseLog(l.ToGethLog())
if err != nil {
continue // malformed log should not break flow
}
request, ok := requestLog.(*v1.VRFCoordinatorRandomnessRequest)
if !ok {
continue // malformed log should not break flow
}
reqs = append(reqs, Event{ID: hex.EncodeToString(request.RequestID[:]), Block: request.Raw.BlockNumber})
}
return reqs, nil
}
// Fulfillments satisfies the Coordinator interface.
func (v *V1Coordinator) Fulfillments(ctx context.Context, fromBlock uint64) ([]Event, error) {
toBlock, err := v.lp.LatestBlock(ctx)
if err != nil {
return nil, errors.Wrap(err, "fetching latest block")
}
logs, err := v.lp.LogsWithSigs(
ctx,
int64(fromBlock),
toBlock.BlockNumber,
[]common.Hash{
v1.VRFCoordinatorRandomnessRequestFulfilled{}.Topic(),
},
v.c.Address())
if err != nil {
return nil, errors.Wrap(err, "filter v1 fulfillments")
}
var fuls []Event
for _, l := range logs {
requestLog, err := v.c.ParseLog(l.ToGethLog())
if err != nil {
continue // malformed log should not break flow
}
request, ok := requestLog.(*v1.VRFCoordinatorRandomnessRequestFulfilled)
if !ok {
continue // malformed log should not break flow
}
fuls = append(fuls, Event{ID: hex.EncodeToString(request.RequestId[:]), Block: request.Raw.BlockNumber})
}
return fuls, nil
}
// V2Coordinator fetches request and fulfillment logs from a VRF V2 coordinator contract.
type V2Coordinator struct {
c v2.VRFCoordinatorV2Interface
lp logpoller.LogPoller
}
// NewV2Coordinator creates a new V2Coordinator from the given contract.
func NewV2Coordinator(ctx context.Context, c v2.VRFCoordinatorV2Interface, lp logpoller.LogPoller) (*V2Coordinator, error) {
err := lp.RegisterFilter(ctx, logpoller.Filter{
Name: logpoller.FilterName("VRFv2CoordinatorFeeder", c.Address()),
EventSigs: []common.Hash{
v2.VRFCoordinatorV2RandomWordsRequested{}.Topic(),
v2.VRFCoordinatorV2RandomWordsFulfilled{}.Topic(),
}, Addresses: []common.Address{c.Address()},
})
if err != nil {
return nil, err
}
return &V2Coordinator{c, lp}, err
}
// Requests satisfies the Coordinator interface.
func (v *V2Coordinator) Requests(
ctx context.Context,
fromBlock uint64,
toBlock uint64,
) ([]Event, error) {
logs, err := v.lp.LogsWithSigs(
ctx,
int64(fromBlock),
int64(toBlock),
[]common.Hash{
v2.VRFCoordinatorV2RandomWordsRequested{}.Topic(),
},
v.c.Address())
if err != nil {
return nil, errors.Wrap(err, "filter v2 requests")
}
var reqs []Event
for _, l := range logs {
requestLog, err := v.c.ParseLog(l.ToGethLog())
if err != nil {
continue // malformed log should not break flow
}
request, ok := requestLog.(*v2.VRFCoordinatorV2RandomWordsRequested)
if !ok {
continue // malformed log should not break flow
}
reqs = append(reqs, Event{ID: request.RequestId.String(), Block: request.Raw.BlockNumber})
}
return reqs, nil
}
// Fulfillments satisfies the Coordinator interface.
func (v *V2Coordinator) Fulfillments(ctx context.Context, fromBlock uint64) ([]Event, error) {
toBlock, err := v.lp.LatestBlock(ctx)
if err != nil {
return nil, errors.Wrap(err, "fetching latest block")
}
logs, err := v.lp.LogsWithSigs(
ctx,
int64(fromBlock),
toBlock.BlockNumber,
[]common.Hash{
v2.VRFCoordinatorV2RandomWordsFulfilled{}.Topic(),
},
v.c.Address())
if err != nil {
return nil, errors.Wrap(err, "filter v2 fulfillments")
}
var fuls []Event
for _, l := range logs {
requestLog, err := v.c.ParseLog(l.ToGethLog())
if err != nil {
continue // malformed log should not break flow
}
request, ok := requestLog.(*v2.VRFCoordinatorV2RandomWordsFulfilled)
if !ok {
continue // malformed log should not break flow
}
fuls = append(fuls, Event{ID: request.RequestId.String(), Block: request.Raw.BlockNumber})
}
return fuls, nil
}
// V2PlusCoordinator fetches request and fulfillment logs from a VRF V2Plus coordinator contract.
type V2PlusCoordinator struct {
c v2plus.IVRFCoordinatorV2PlusInternalInterface
lp logpoller.LogPoller
}
// NewV2Coordinator creates a new V2Coordinator from the given contract.
func NewV2PlusCoordinator(ctx context.Context, c v2plus.IVRFCoordinatorV2PlusInternalInterface, lp logpoller.LogPoller) (*V2PlusCoordinator, error) {
err := lp.RegisterFilter(ctx, logpoller.Filter{
Name: logpoller.FilterName("VRFv2PlusCoordinatorFeeder", c.Address()),
EventSigs: []common.Hash{
v2plus.IVRFCoordinatorV2PlusInternalRandomWordsRequested{}.Topic(),
v2plus.IVRFCoordinatorV2PlusInternalRandomWordsFulfilled{}.Topic(),
}, Addresses: []common.Address{c.Address()},
})
if err != nil {
return nil, err
}
return &V2PlusCoordinator{c, lp}, err
}
// Requests satisfies the Coordinator interface.
func (v *V2PlusCoordinator) Requests(
ctx context.Context,
fromBlock uint64,
toBlock uint64,
) ([]Event, error) {
logs, err := v.lp.LogsWithSigs(
ctx,
int64(fromBlock),
int64(toBlock),
[]common.Hash{
v2plus.IVRFCoordinatorV2PlusInternalRandomWordsRequested{}.Topic(),
},
v.c.Address())
if err != nil {
return nil, errors.Wrap(err, "filter v2 requests")
}
var reqs []Event
for _, l := range logs {
requestLog, err := v.c.ParseLog(l.ToGethLog())
if err != nil {
continue // malformed log should not break flow
}
request, ok := requestLog.(*v2plus.IVRFCoordinatorV2PlusInternalRandomWordsRequested)
if !ok {
continue // malformed log should not break flow
}
reqs = append(reqs, Event{ID: request.RequestId.String(), Block: request.Raw.BlockNumber})
}
return reqs, nil
}
// Fulfillments satisfies the Coordinator interface.
func (v *V2PlusCoordinator) Fulfillments(ctx context.Context, fromBlock uint64) ([]Event, error) {
toBlock, err := v.lp.LatestBlock(ctx)
if err != nil {
return nil, errors.Wrap(err, "fetching latest block")
}
logs, err := v.lp.LogsWithSigs(
ctx,
int64(fromBlock),
toBlock.BlockNumber,
[]common.Hash{
v2plus.IVRFCoordinatorV2PlusInternalRandomWordsFulfilled{}.Topic(),
},
v.c.Address())
if err != nil {
return nil, errors.Wrap(err, "filter v2 fulfillments")
}
var fuls []Event
for _, l := range logs {
requestLog, err := v.c.ParseLog(l.ToGethLog())
if err != nil {
continue // malformed log should not break flow
}
request, ok := requestLog.(*v2plus.IVRFCoordinatorV2PlusInternalRandomWordsFulfilled)
if !ok {
continue // malformed log should not break flow
}
fuls = append(fuls, Event{ID: request.RequestId.String(), Block: request.Raw.BlockNumber})
}
return fuls, nil
}