-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
head_tracker.go
346 lines (311 loc) · 10.3 KB
/
head_tracker.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
package headtracker
import (
"context"
"fmt"
"math/big"
"sync"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap/zapcore"
evmclient "github.com/smartcontractkit/chainlink/core/chains/evm/client"
httypes "github.com/smartcontractkit/chainlink/core/chains/evm/headtracker/types"
evmtypes "github.com/smartcontractkit/chainlink/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/core/config"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/utils"
)
var (
promCurrentHead = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "head_tracker_current_head",
Help: "The highest seen head number",
}, []string{"evmChainID"})
promOldHead = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "head_tracker_very_old_head",
Help: "Counter is incremented every time we get a head that is much lower than the highest seen head ('much lower' is defined as a block that is ETH_FINALITY_DEPTH or greater below the highest seen head)",
}, []string{"evmChainID"})
)
// HeadsBufferSize - The buffer is used when heads sampling is disabled, to ensure the callback is run for every head
const HeadsBufferSize = 10
type headTracker struct {
log logger.Logger
headBroadcaster httypes.HeadBroadcaster
headSaver httypes.HeadSaver
ethClient evmclient.Client
chainID big.Int
config Config
backfillMB *utils.Mailbox[*evmtypes.Head]
broadcastMB *utils.Mailbox[*evmtypes.Head]
headListener httypes.HeadListener
chStop chan struct{}
wgDone sync.WaitGroup
utils.StartStopOnce
}
// NewHeadTracker instantiates a new HeadTracker using HeadSaver to persist new block numbers.
func NewHeadTracker(
lggr logger.Logger,
ethClient evmclient.Client,
config Config,
headBroadcaster httypes.HeadBroadcaster,
headSaver httypes.HeadSaver,
) httypes.HeadTracker {
chStop := make(chan struct{})
lggr = lggr.Named("HeadTracker")
return &headTracker{
headBroadcaster: headBroadcaster,
ethClient: ethClient,
chainID: *ethClient.ChainID(),
config: config,
log: lggr,
backfillMB: utils.NewMailbox[*evmtypes.Head](1),
broadcastMB: utils.NewMailbox[*evmtypes.Head](HeadsBufferSize),
chStop: chStop,
headListener: NewHeadListener(lggr, ethClient, config, chStop),
headSaver: headSaver,
}
}
// Start starts HeadTracker service.
func (ht *headTracker) Start(ctx context.Context) error {
return ht.StartOnce("HeadTracker", func() error {
ht.log.Debugf("Starting HeadTracker with chain id: %v", ht.chainID.Int64())
latestChain, err := ht.headSaver.LoadFromDB(ctx)
if err != nil {
return err
}
if latestChain != nil {
ht.log.Debugw(
fmt.Sprintf("HeadTracker: Tracking logs from last block %v with hash %s", config.FriendlyBigInt(latestChain.ToInt()), latestChain.Hash.Hex()),
"blockNumber", latestChain.Number,
"blockHash", latestChain.Hash,
)
}
// NOTE: Always try to start the head tracker off with whatever the
// latest head is, without waiting for the subscription to send us one.
//
// In some cases the subscription will send us the most recent head
// anyway when we connect (but we should not rely on this because it is
// not specced). If it happens this is fine, and the head will be
// ignored as a duplicate.
initialHead, err := ht.getInitialHead(ctx)
if err != nil {
if errors.Is(err, ctx.Err()) {
return nil
}
ht.log.Errorw("Error getting initial head", "err", err)
} else if initialHead != nil {
if err := ht.handleNewHead(ctx, initialHead); err != nil {
return errors.Wrap(err, "error handling initial head")
}
} else {
ht.log.Debug("Got nil initial head")
}
ht.wgDone.Add(3)
go ht.headListener.ListenForNewHeads(ht.handleNewHead, ht.wgDone.Done)
go ht.backfillLoop()
go ht.broadcastLoop()
return nil
})
}
// Close stops HeadTracker service.
func (ht *headTracker) Close() error {
return ht.StopOnce("HeadTracker", func() error {
close(ht.chStop)
ht.wgDone.Wait()
return nil
})
}
func (ht *headTracker) Healthy() error {
if !ht.headListener.ReceivingHeads() {
return errors.New("Listener is not receiving heads")
}
if !ht.headListener.Connected() {
return errors.New("Listener is not connected")
}
return nil
}
func (ht *headTracker) Backfill(ctx context.Context, headWithChain *evmtypes.Head, depth uint) (err error) {
if uint(headWithChain.ChainLength()) >= depth {
return nil
}
baseHeight := headWithChain.Number - int64(depth-1)
if baseHeight < 0 {
baseHeight = 0
}
return ht.backfill(ctx, headWithChain.EarliestInChain(), baseHeight)
}
func (ht *headTracker) getInitialHead(ctx context.Context) (*evmtypes.Head, error) {
head, err := ht.ethClient.HeadByNumber(ctx, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch initial head")
}
loggerFields := []interface{}{"head", head}
if head != nil {
loggerFields = append(loggerFields, "blockNumber", head.Number, "blockHash", head.Hash)
}
ht.log.Debugw("Got initial head", loggerFields...)
return head, nil
}
func (ht *headTracker) handleNewHead(ctx context.Context, head *evmtypes.Head) error {
prevHead := ht.headSaver.LatestChain()
ht.log.Debugw(fmt.Sprintf("Received new head %v", config.FriendlyBigInt(head.ToInt())),
"blockHeight", head.ToInt(),
"blockHash", head.Hash,
"parentHeadHash", head.ParentHash,
)
err := ht.headSaver.Save(ctx, head)
if ctx.Err() != nil {
return nil
} else if err != nil {
return errors.Wrapf(err, "failed to save head: %#v", head)
}
if prevHead == nil || head.Number > prevHead.Number {
promCurrentHead.WithLabelValues(ht.chainID.String()).Set(float64(head.Number))
headWithChain := ht.headSaver.Chain(head.Hash)
if headWithChain == nil {
return errors.Errorf("HeadTracker#handleNewHighestHead headWithChain was unexpectedly nil")
}
ht.backfillMB.Deliver(headWithChain)
ht.broadcastMB.Deliver(headWithChain)
} else if head.Number == prevHead.Number {
if head.Hash != prevHead.Hash {
ht.log.Debugw("Got duplicate head", "blockNum", head.Number, "head", head.Hash.Hex(), "prevHead", prevHead.Hash.Hex())
} else {
ht.log.Debugw("Head already in the database", "head", head.Hash.Hex())
}
} else {
ht.log.Debugw("Got out of order head", "blockNum", head.Number, "head", head.Hash.Hex(), "prevHead", prevHead.Number)
if head.Number < prevHead.Number-int64(ht.config.EvmFinalityDepth()) {
promOldHead.WithLabelValues(ht.chainID.String()).Inc()
ht.log.Criticalf("Got very old block with number %d (highest seen was %d). This is a problem and either means a very deep re-org occurred, one of the RPC nodes has gotten far out of sync, or the chain went backwards in block numbers. This node may not function correctly without manual intervention.", head.Number, prevHead.Number)
}
}
return nil
}
func (ht *headTracker) broadcastLoop() {
defer ht.wgDone.Done()
samplingInterval := ht.config.EvmHeadTrackerSamplingInterval()
if samplingInterval > 0 {
ht.log.Debugf("Head sampling is enabled - sampling interval is set to: %v", samplingInterval)
debounceHead := time.NewTicker(samplingInterval)
defer debounceHead.Stop()
for {
select {
case <-ht.chStop:
return
case <-debounceHead.C:
item := ht.broadcastMB.RetrieveLatestAndClear()
if item == nil {
continue
}
ht.headBroadcaster.BroadcastNewLongestChain(item)
}
}
} else {
ht.log.Info("Head sampling is disabled - callback will be called on every head")
for {
select {
case <-ht.chStop:
return
case <-ht.broadcastMB.Notify():
for {
item, exists := ht.broadcastMB.Retrieve()
if !exists {
break
}
ht.headBroadcaster.BroadcastNewLongestChain(item)
}
}
}
}
}
func (ht *headTracker) backfillLoop() {
defer ht.wgDone.Done()
ctx, cancel := utils.ContextFromChan(ht.chStop)
defer cancel()
for {
select {
case <-ht.chStop:
return
case <-ht.backfillMB.Notify():
for {
head, exists := ht.backfillMB.Retrieve()
if !exists {
break
}
{
err := ht.Backfill(ctx, head, uint(ht.config.EvmFinalityDepth()))
if err != nil {
ht.log.Warnw("Unexpected error while backfilling heads", "err", err)
} else if ctx.Err() != nil {
break
}
}
}
}
}
}
// backfill fetches all missing heads up until the base height
func (ht *headTracker) backfill(ctx context.Context, head *evmtypes.Head, baseHeight int64) (err error) {
if head.Number <= baseHeight {
return nil
}
mark := time.Now()
fetched := 0
l := ht.log.With("blockNumber", head.Number,
"n", head.Number-baseHeight,
"fromBlockHeight", baseHeight,
"toBlockHeight", head.Number-1)
l.Debug("Starting backfill")
defer func() {
if ctx.Err() != nil {
l.Warnw("Backfill context error", "err", ctx.Err())
return
}
l.Debugw("Finished backfill",
"fetched", fetched,
"time", time.Since(mark),
"err", err)
}()
for i := head.Number - 1; i >= baseHeight; i-- {
// NOTE: Sequential requests here mean it's a potential performance bottleneck, be aware!
existingHead := ht.headSaver.Chain(head.ParentHash)
if existingHead != nil {
head = existingHead
continue
}
head, err = ht.fetchAndSaveHead(ctx, i)
fetched++
if ctx.Err() != nil {
ht.log.Debugw("context canceled, aborting backfill", "err", err, "ctx.Err", ctx.Err())
break
} else if err != nil {
return errors.Wrap(err, "fetchAndSaveHead failed")
}
}
return
}
func (ht *headTracker) fetchAndSaveHead(ctx context.Context, n int64) (*evmtypes.Head, error) {
ht.log.Debugw("Fetching head", "blockHeight", n)
head, err := ht.ethClient.HeadByNumber(ctx, big.NewInt(n))
if err != nil {
return nil, err
} else if head == nil {
return nil, errors.New("got nil head")
}
err = ht.headSaver.Save(ctx, head)
if err != nil {
return nil, err
}
return head, nil
}
var NullTracker httypes.HeadTracker = &nullTracker{}
type nullTracker struct{}
func (*nullTracker) Start(context.Context) error { return nil }
func (*nullTracker) Close() error { return nil }
func (*nullTracker) Ready() error { return nil }
func (*nullTracker) Healthy() error { return nil }
func (*nullTracker) SetLogLevel(zapcore.Level) {}
func (*nullTracker) Backfill(ctx context.Context, headWithChain *evmtypes.Head, depth uint) (err error) {
return nil
}