/
listener_v2_log_listener.go
452 lines (417 loc) · 15.9 KB
/
listener_v2_log_listener.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package v2
import (
"bytes"
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"go.uber.org/multierr"
"github.com/O1MaGnUmO1/chainlink-common/pkg/utils/mathutil"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/chains/evm/logpoller"
evmtypes "github.com/O1MaGnUmO1/erinaceus-vrf/core/chains/evm/types"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/logger"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/services/pg"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/services/vrf/vrfcommon"
)
func (lsn *listenerV2) runLogListener(
pollPeriod time.Duration,
minConfs uint32,
) {
lsn.l.Infow("Listening for run requests via log poller",
"minConfs", minConfs)
ticker := time.NewTicker(pollPeriod)
defer ticker.Stop()
var (
lastProcessedBlock int64
startingUp = true
)
ctx, cancel := lsn.chStop.NewCtx()
defer cancel()
for {
select {
case <-lsn.chStop:
return
case <-ticker.C:
start := time.Now()
lsn.l.Debugw("log listener loop")
// Filter registration is idempotent, so we can just call it every time
// and retry on errors using the ticker.
err := lsn.chain.LogPoller().RegisterFilter(logpoller.Filter{
Name: logpoller.FilterName(
"VRFListener",
"version", lsn.coordinator.Version(),
"keyhash", lsn.job.VRFSpec.PublicKey.MustHash(),
"coordinatorAddress", lsn.coordinator.Address()),
EventSigs: evmtypes.HashArray{
lsn.coordinator.RandomWordsFulfilledTopic(),
lsn.coordinator.RandomWordsRequestedTopic(),
},
Addresses: evmtypes.AddressArray{
lsn.coordinator.Address(),
},
})
if err != nil {
lsn.l.Errorw("error registering filter in log poller, retrying",
"err", err,
"elapsed", time.Since(start))
// continue
}
// on startup we want to initialize the last processed block
if startingUp {
lsn.l.Debugw("initializing last processed block on startup")
lastProcessedBlock, err = lsn.initializeLastProcessedBlock(ctx)
if err != nil {
lsn.l.Errorw("error initializing last processed block, retrying",
"err", err,
"elapsed", time.Since(start))
continue
}
startingUp = false
lsn.l.Debugw("initialized last processed block", "lastProcessedBlock", lastProcessedBlock)
lsn.chain.LogBroadcaster().Start(ctx)
}
// lastProcessedBlock = int64(lsn.getLatestHead())
pending, err := lsn.pollLogs(ctx, minConfs, lastProcessedBlock)
if err != nil {
lsn.l.Errorw("error polling vrf logs, retrying",
"err", err,
"elapsed", time.Since(start))
continue
}
// process pending requests and insert any fulfillments into the inflight cache
lsn.processPendingVRFRequests(ctx, pending)
lastProcessedBlock, err = lsn.updateLastProcessedBlock(ctx, lastProcessedBlock)
if err != nil {
lsn.l.Errorw("error updating last processed block, continuing anyway", "err", err)
} else {
lsn.l.Debugw("updated last processed block", "lastProcessedBlock", lastProcessedBlock)
}
lsn.l.Debugw("log listener loop done", "elapsed", time.Since(start))
}
}
}
// initializeLastProcessedBlock returns the earliest block number that we need to
// process requests for. This is the block number of the earliest unfulfilled request
// or the latest finalized block, if there are no unfulfilled requests.
// TODO: add tests
func (lsn *listenerV2) initializeLastProcessedBlock(ctx context.Context) (lastProcessedBlock int64, err error) {
lp := lsn.chain.LogPoller()
start := time.Now()
// will retry on error in the runLogListener loop
latestBlock, err := lp.LatestBlock()
if err != nil {
return 0, fmt.Errorf("LogPoller.LatestBlock(): %w", err)
}
fromTimestamp := time.Now().UTC().Add(-lsn.job.VRFSpec.RequestTimeout)
ll := lsn.l.With(
"latestFinalizedBlock", latestBlock.FinalizedBlockNumber,
"latestBlock", latestBlock.BlockNumber,
"fromTimestamp", fromTimestamp)
ll.Debugw("Initializing last processed block")
defer func() {
ll.Debugw("Done initializing last processed block", "elapsed", time.Since(start))
}()
numBlocksToReplay := numReplayBlocks(lsn.job.VRFSpec.RequestTimeout, lsn.chain.ID())
ll.Debugw("running replay on log poller")
err = lp.Replay(ctx, mathutil.Max(latestBlock.FinalizedBlockNumber-numBlocksToReplay, 1))
if err != nil {
return 0, fmt.Errorf("LogPoller.Replay: %w", err)
}
// get randomness requested logs with the appropriate keyhash
// keyhash is specified in topic1
requests, err := lp.IndexedLogsCreatedAfter(
lsn.coordinator.RandomWordsRequestedTopic(), // event sig
lsn.coordinator.Address(), // address
1, // topic index
[]common.Hash{lsn.job.VRFSpec.PublicKey.MustHash()}, // topic values
fromTimestamp, // from time
logpoller.Finalized, // confs
)
if err != nil {
return 0, fmt.Errorf("LogPoller.LogsCreatedAfter RandomWordsRequested logs: %w", err)
}
// fulfillments don't have keyhash indexed, we'll have to get all of them
// TODO: can we instead write a single query that joins on request id's somehow?
fulfillments, err := lp.LogsCreatedAfter(
lsn.coordinator.RandomWordsFulfilledTopic(), // event sig
lsn.coordinator.Address(), // address
fromTimestamp, // from time
logpoller.Finalized, // confs
)
if err != nil {
return 0, fmt.Errorf("LogPoller.LogsCreatedAfter RandomWordsFulfilled logs: %w", err)
}
unfulfilled, _, _ := lsn.getUnfulfilled(append(requests, fulfillments...), ll)
// find request block of earliest unfulfilled request
// even if this block is > latest finalized, we use latest finalized as earliest unprocessed
// because re-orgs can occur on any unfinalized block.
var earliestUnfulfilledBlock = latestBlock.FinalizedBlockNumber
for _, req := range unfulfilled {
if req.Raw().BlockNumber < uint64(earliestUnfulfilledBlock) {
earliestUnfulfilledBlock = int64(req.Raw().BlockNumber)
}
}
return earliestUnfulfilledBlock, nil
}
func (lsn *listenerV2) updateLastProcessedBlock(ctx context.Context, currLastProcessedBlock int64) (lastProcessedBlock int64, err error) {
lp := lsn.chain.LogPoller()
start := time.Now()
latestBlock, err := lp.LatestBlock(pg.WithParentCtx(ctx))
if err != nil {
lsn.l.Errorw("error getting latest block", "err", err)
return 0, fmt.Errorf("LogPoller.LatestBlock(): %w", err)
}
ll := lsn.l.With(
"currLastProcessedBlock", currLastProcessedBlock,
"latestBlock", latestBlock.BlockNumber,
"latestFinalizedBlock", latestBlock.FinalizedBlockNumber)
ll.Debugw("updating last processed block")
defer func() {
ll.Debugw("done updating last processed block", "elapsed", time.Since(start))
}()
logs, err := lp.LogsWithSigs(
currLastProcessedBlock,
latestBlock.FinalizedBlockNumber,
[]common.Hash{lsn.coordinator.RandomWordsFulfilledTopic(), lsn.coordinator.RandomWordsRequestedTopic()},
lsn.coordinator.Address(),
pg.WithParentCtx(ctx),
)
if err != nil {
return currLastProcessedBlock, fmt.Errorf("LogPoller.LogsWithSigs: %w", err)
}
unfulfilled, unfulfilledLP, _ := lsn.getUnfulfilled(logs, ll)
// find request block of earliest unfulfilled request
// even if this block is > latest finalized, we use latest finalized as earliest unprocessed
// because re-orgs can occur on any unfinalized block.
var earliestUnprocessedRequestBlock = latestBlock.FinalizedBlockNumber
for i, req := range unfulfilled {
// need to drop requests that have timed out otherwise the earliestUnprocessedRequestBlock
// will be unnecessarily far back and our queries will be slower.
if unfulfilledLP[i].CreatedAt.Before(time.Now().UTC().Add(-lsn.job.VRFSpec.RequestTimeout)) {
// request timed out, don't process
lsn.l.Debugw("request timed out, skipping",
"reqID", req.RequestID(),
)
continue
}
if req.Raw().BlockNumber < uint64(earliestUnprocessedRequestBlock) {
earliestUnprocessedRequestBlock = int64(req.Raw().BlockNumber)
}
}
return earliestUnprocessedRequestBlock, nil
}
// pollLogs uses the log poller to poll for the latest VRF logs
func (lsn *listenerV2) pollLogs(ctx context.Context, minConfs uint32, lastProcessedBlock int64) (pending []pendingRequest, err error) {
start := time.Now()
lp := lsn.chain.LogPoller()
// latest unfinalized block used on purpose to get bleeding edge logs
// we don't really have the luxury to wait for finalization on most chains
// if we want to fulfill on time.
latestBlock, err := lp.LatestBlock()
if err != nil {
return nil, fmt.Errorf("LogPoller.LatestBlock(): %w", err)
}
lsn.setLatestHead(latestBlock)
ll := lsn.l.With(
"lastProcessedBlock", lastProcessedBlock,
"minConfs", minConfs,
"latestBlock", latestBlock.BlockNumber,
"latestFinalizedBlock", latestBlock.FinalizedBlockNumber)
ll.Debugw("polling for logs")
defer func() {
ll.Debugw("done polling for logs", "elapsed", time.Since(start))
}()
// We don't specify confs because each request can have a different conf above
// the minimum. So we do all conf handling in getConfirmedAt.
logs, err := lp.LogsWithSigs(
lastProcessedBlock,
latestBlock.BlockNumber,
[]common.Hash{lsn.coordinator.RandomWordsFulfilledTopic(), lsn.coordinator.RandomWordsRequestedTopic()},
lsn.coordinator.Address(),
pg.WithParentCtx(ctx),
)
if err != nil {
return nil, fmt.Errorf("LogPoller.LogsWithSigs: %w", err)
}
unfulfilled, unfulfilledLP, fulfilled := lsn.getUnfulfilled(logs, ll)
if len(unfulfilled) > 0 {
ll.Debugw("found unfulfilled logs", "unfulfilled", len(unfulfilled))
} else {
ll.Debugw("no unfulfilled logs found")
}
lsn.handleFulfilled(fulfilled)
return lsn.handleRequested(unfulfilled, unfulfilledLP, minConfs), nil
}
func (lsn *listenerV2) getUnfulfilled(logs []logpoller.Log, ll logger.Logger) (unfulfilled []RandomWordsRequested, unfulfilledLP []logpoller.Log, fulfilled map[string]RandomWordsFulfilled) {
var (
requested = make(map[string]RandomWordsRequested)
requestedLP = make(map[string]logpoller.Log)
errs error
expectedKeyHash = lsn.job.VRFSpec.PublicKey.MustHash()
)
fulfilled = make(map[string]RandomWordsFulfilled)
for _, l := range logs {
if l.EventSig == lsn.coordinator.RandomWordsFulfilledTopic() {
parsed, err2 := lsn.coordinator.ParseRandomWordsFulfilled(l.ToGethLog())
if err2 != nil {
// should never happen
errs = multierr.Append(errs, err2)
continue
}
fulfilled[parsed.RequestID().String()] = parsed
} else if l.EventSig == lsn.coordinator.RandomWordsRequestedTopic() {
parsed, err2 := lsn.coordinator.ParseRandomWordsRequested(l.ToGethLog())
if err2 != nil {
// should never happen
errs = multierr.Append(errs, err2)
continue
}
keyHash := parsed.KeyHash()
if !bytes.Equal(keyHash[:], expectedKeyHash[:]) {
// wrong keyhash, can ignore
continue
}
requested[parsed.RequestID().String()] = parsed
requestedLP[parsed.RequestID().String()] = l
}
}
// should never happen, unsure if recoverable
// may be worth a panic
if errs != nil {
ll.Errorw("encountered parse errors", "err", errs)
}
if len(fulfilled) > 0 || len(requested) > 0 {
ll.Infow("found logs", "fulfilled", len(fulfilled), "requested", len(requested))
} else {
ll.Debugw("no logs found")
}
// find unfulfilled requests by comparing requested events with the fulfilled events
for reqID, req := range requested {
if _, isFulfilled := fulfilled[reqID]; !isFulfilled {
unfulfilled = append(unfulfilled, req)
unfulfilledLP = append(unfulfilledLP, requestedLP[reqID])
}
}
return unfulfilled, unfulfilledLP, fulfilled
}
func (lsn *listenerV2) getConfirmedAt(req RandomWordsRequested, nodeMinConfs uint32) uint64 {
// Take the max(nodeMinConfs, requestedConfs + requestedConfsDelay).
// Add the requested confs delay if provided in the jobspec so that we avoid an edge case
// where the primary and backup VRF v2 nodes submit a proof at the same time.
minConfs := nodeMinConfs
if uint32(req.MinimumRequestConfirmations())+uint32(lsn.job.VRFSpec.RequestedConfsDelay) > nodeMinConfs {
minConfs = uint32(req.MinimumRequestConfirmations()) + uint32(lsn.job.VRFSpec.RequestedConfsDelay)
}
newConfs := uint64(minConfs) * (1 << lsn.respCount[req.RequestID().String()])
// We cap this at 200 because solidity only supports the most recent 256 blocks
// in the contract so if it was older than that, fulfillments would start failing
// without the blockhash store feeder. We use 200 to give the node plenty of time
// to fulfill even on fast chains.
if newConfs > 200 {
newConfs = 200
}
if lsn.respCount[req.RequestID().String()] > 0 {
lsn.l.Warnw("Duplicate request found after fulfillment, doubling incoming confirmations",
"txHash", req.Raw().TxHash,
"blockNumber", req.Raw().BlockNumber,
"blockHash", req.Raw().BlockHash,
"reqID", req.RequestID().String(),
"newConfs", newConfs)
vrfcommon.IncDupeReqs(lsn.job.Name.ValueOrZero(), lsn.job.ExternalJobID, lsn.coordinator.Version())
}
return req.Raw().BlockNumber + newConfs
}
func (lsn *listenerV2) handleFulfilled(fulfilled map[string]RandomWordsFulfilled) {
for _, v := range fulfilled {
// don't process same log over again
// log key includes block number and blockhash, so on re-orgs it would return true
// and we would re-process the re-orged request.
if !lsn.fulfillmentLogDeduper.ShouldDeliver(v.Raw()) {
continue
}
lsn.l.Debugw("Received fulfilled log", "reqID", v.RequestID(), "success", v.Success())
lsn.respCount[v.RequestID().String()]++
lsn.blockNumberToReqID.Insert(fulfilledReqV2{
blockNumber: v.Raw().BlockNumber,
reqID: v.RequestID().String(),
})
}
}
func (lsn *listenerV2) handleRequested(requested []RandomWordsRequested, requestedLP []logpoller.Log, minConfs uint32) (pendingRequests []pendingRequest) {
for i, req := range requested {
// don't process same log over again
// log key includes block number and blockhash, so on re-orgs it would return true
// and we would re-process the re-orged request.
if lsn.inflightCache.Contains(req.Raw()) {
continue
}
confirmedAt := lsn.getConfirmedAt(req, minConfs)
lsn.l.Debugw("VRFListenerV2: Received log request",
"reqID", req.RequestID(),
"reqBlockNumber", req.Raw().BlockNumber,
"reqBlockHash", req.Raw().BlockHash,
"reqTxHash", req.Raw().TxHash,
"confirmedAt", confirmedAt,
"subID", req.SubID(),
"sender", req.Sender())
pendingRequests = append(pendingRequests, pendingRequest{
confirmedAtBlock: confirmedAt,
req: req,
utcTimestamp: requestedLP[i].CreatedAt.UTC(),
})
lsn.reqAdded()
}
return pendingRequests
}
// numReplayBlocks returns the number of blocks to replay on startup
// given the request timeout and the chain ID.
// if the chain ID is not recognized it assumes a block time of 1 second
// and returns the number of blocks in a day.
func numReplayBlocks(requestTimeout time.Duration, chainID *big.Int) int64 {
var timeoutSeconds = int64(requestTimeout.Seconds())
switch chainID.String() {
case "1": // eth mainnet
case "3": // eth ropsten
case "4": // eth rinkeby
case "5": // eth goerli
case "11155111": // eth sepolia
// block time is 12s
return timeoutSeconds / 12
case "137": // polygon mainnet
case "80001": // polygon mumbai
// block time is 2s
return timeoutSeconds / 2
case "56": // bsc mainnet
case "97": // bsc testnet
// block time is 2s
return timeoutSeconds / 2
case "43114": // avalanche mainnet
case "43113": // avalanche fuji
// block time is 1s
return timeoutSeconds
case "250": // fantom mainnet
case "4002": // fantom testnet
// block time is 1s
return timeoutSeconds
case "42161": // arbitrum mainnet
case "421613": // arbitrum goerli
case "421614": // arbitrum sepolia
// block time is 0.25s in the worst case
return timeoutSeconds * 4
case "10": // optimism mainnet
case "69": // optimism kovan
case "420": // optimism goerli
case "11155420": // optimism sepolia
case "8453": // base mainnet
case "84531": // base goerli
case "84532": // base sepolia
// block time is 2s
return timeoutSeconds / 2
default:
// assume block time of 1s
return timeoutSeconds
}
// assume block time of 1s
return timeoutSeconds
}