-
Notifications
You must be signed in to change notification settings - Fork 105
/
header.go
494 lines (415 loc) · 18.4 KB
/
header.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package header
import (
"context"
"errors"
"fmt"
"time"
"github.com/snowfork/snowbridge/relayer/chain/parachain"
"github.com/snowfork/snowbridge/relayer/relays/beacon/cache"
"github.com/snowfork/snowbridge/relayer/relays/beacon/config"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/api"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
"github.com/snowfork/snowbridge/relayer/relays/beacon/protocol"
"github.com/snowfork/snowbridge/relayer/relays/beacon/state"
"github.com/snowfork/snowbridge/relayer/relays/beacon/store"
"github.com/ethereum/go-ethereum/common"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
var ErrFinalizedHeaderUnchanged = errors.New("finalized header unchanged")
var ErrFinalizedHeaderNotImported = errors.New("finalized header not imported")
var ErrInterimHeaderNotImported = errors.New("interim finalized header not imported")
var ErrSyncCommitteeNotImported = errors.New("sync committee not imported")
var ErrSyncCommitteeLatency = errors.New("sync committee latency found")
var ErrExecutionHeaderNotImported = errors.New("execution header not imported")
var ErrBeaconHeaderNotFinalized = errors.New("beacon header not finalized")
type Header struct {
cache *cache.BeaconCache
writer parachain.ChainWriter
syncer *syncer.Syncer
protocol *protocol.Protocol
}
func New(writer parachain.ChainWriter, client api.BeaconAPI, setting config.SpecSettings, store store.BeaconStore, protocol *protocol.Protocol) Header {
return Header{
cache: cache.New(setting.SlotsInEpoch, setting.EpochsPerSyncCommitteePeriod),
writer: writer,
syncer: syncer.New(client, store, protocol),
protocol: protocol,
}
}
func (h *Header) Sync(ctx context.Context, eg *errgroup.Group) error {
lastFinalizedHeaderState, err := h.writer.GetLastFinalizedHeaderState()
if err != nil {
return fmt.Errorf("fetch parachain last finalized header state: %w", err)
}
latestSyncedPeriod := h.protocol.ComputeSyncPeriodAtSlot(lastFinalizedHeaderState.BeaconSlot)
log.WithFields(log.Fields{
"last_finalized_hash": lastFinalizedHeaderState.BeaconBlockRoot,
"last_finalized_slot": lastFinalizedHeaderState.BeaconSlot,
"last_finalized_period": latestSyncedPeriod,
}).Info("set cache: Current state")
h.cache.SetLastSyncedFinalizedState(lastFinalizedHeaderState.BeaconBlockRoot, lastFinalizedHeaderState.BeaconSlot)
h.cache.SetInitialCheckpointSlot(lastFinalizedHeaderState.InitialCheckpointSlot)
h.cache.AddCheckPointSlots([]uint64{lastFinalizedHeaderState.BeaconSlot})
// Special handling here for the initial checkpoint to sync the next sync committee which is not included in initial
// checkpoint.
if h.isInitialSyncPeriod() {
log.Info("syncing next sync committee for initial checkpoint")
err = h.SyncCommitteePeriodUpdate(ctx, latestSyncedPeriod)
if err != nil {
return fmt.Errorf("sync next committee for initial sync period: %w", err)
}
}
log.Info("starting to sync finalized headers")
ticker := time.NewTicker(time.Second * 10)
eg.Go(func() error {
for {
err = h.SyncHeaders(ctx)
logFields := log.Fields{
"finalized_header": h.cache.Finalized.LastSyncedHash,
"finalized_slot": h.cache.Finalized.LastSyncedSlot,
}
switch {
case errors.Is(err, ErrFinalizedHeaderUnchanged):
log.WithFields(logFields).Info("not importing unchanged header")
case errors.Is(err, ErrFinalizedHeaderNotImported):
log.WithFields(logFields).WithError(err).Warn("Not importing header this cycle")
case errors.Is(err, ErrSyncCommitteeNotImported):
log.WithFields(logFields).WithError(err).Warn("SyncCommittee not imported")
case errors.Is(err, ErrSyncCommitteeLatency):
log.WithFields(logFields).WithError(err).Warn("SyncCommittee latency found")
case errors.Is(err, ErrExecutionHeaderNotImported):
log.WithFields(logFields).WithError(err).Warn("ExecutionHeader not imported")
case errors.Is(err, syncer.ErrBeaconStateAvailableYet):
log.WithFields(logFields).WithError(err).Warn("beacon state not available for finalized state yet")
case err != nil:
return err
}
select {
case <-ctx.Done():
return nil
case <-ticker.C:
continue
}
}
})
return nil
}
func (h *Header) SyncCommitteePeriodUpdate(ctx context.Context, period uint64) error {
update, err := h.syncer.GetSyncCommitteePeriodUpdate(period, h.cache.Finalized.LastSyncedSlot)
switch {
case errors.Is(err, syncer.ErrCommitteeUpdateHeaderInDifferentSyncPeriod):
{
log.WithField("period", period).Info("committee update and header in different sync periods, skipping")
return err
}
case err != nil:
{
return fmt.Errorf("fetch sync committee period update: %w", err)
}
}
// If the gap between the last two finalized headers is more than the sync committee period, sync an interim
// finalized header
if uint64(update.Payload.FinalizedHeader.Slot) > h.cache.Finalized.LastSyncedSlot {
diff := uint64(update.Payload.FinalizedHeader.Slot) - h.cache.Finalized.LastSyncedSlot
minSlot := h.cache.Finalized.LastSyncedSlot
for diff > h.protocol.Settings.SlotsInEpoch*h.protocol.Settings.EpochsPerSyncCommitteePeriod {
log.WithFields(log.Fields{
"diff": diff,
"last_finalized_slot": h.cache.Finalized.LastSyncedSlot,
"new_finalized_slot": uint64(update.Payload.FinalizedHeader.Slot),
}).Info("interim update required")
interimUpdate, err := h.syncInterimFinalizedUpdate(ctx, minSlot, uint64(update.Payload.FinalizedHeader.Slot))
if err != nil {
return fmt.Errorf("sync interim finalized header update: %w", err)
}
diff = uint64(update.Payload.FinalizedHeader.Slot) - uint64(interimUpdate.Payload.FinalizedHeader.Slot)
minSlot = uint64(update.Payload.FinalizedHeader.Slot) + h.protocol.Settings.SlotsInEpoch
log.WithFields(log.Fields{
"new_diff": diff,
"interim_finalized_slot": uint64(interimUpdate.Payload.FinalizedHeader.Slot),
"new_finalized_slot": uint64(update.Payload.FinalizedHeader.Slot),
}).Info("interim update synced successfully")
}
}
log.WithFields(log.Fields{
"finalized_header_slot": update.Payload.FinalizedHeader.Slot,
"period": period,
}).Info("syncing sync committee for period")
err = h.writer.WriteToParachainAndWatch(ctx, "EthereumBeaconClient.submit", update.Payload)
if err != nil {
return err
}
// Only update cache when SyncCommitteeUpdate import succeeded and period updated as expected
lastFinalizedHeaderState, err := h.writer.GetLastFinalizedHeaderState()
if err != nil {
return fmt.Errorf("fetch last finalized header state: %w", err)
}
lastUpdatedPeriod := h.protocol.ComputeSyncPeriodAtSlot(lastFinalizedHeaderState.BeaconSlot)
if period != lastUpdatedPeriod {
return ErrSyncCommitteeNotImported
}
h.cache.SetLastSyncedFinalizedState(update.FinalizedHeaderBlockRoot, uint64(update.Payload.FinalizedHeader.Slot))
h.cache.AddCheckPoint(update.FinalizedHeaderBlockRoot, update.BlockRootsTree, uint64(update.Payload.FinalizedHeader.Slot))
return nil
}
func (h *Header) SyncFinalizedHeader(ctx context.Context) error {
// When the chain has been processed up until now, keep getting finalized block updates and send that to the parachain
update, err := h.syncer.GetFinalizedUpdate()
if err != nil {
return fmt.Errorf("fetch finalized header update from Ethereum beacon client: %w", err)
}
log.WithFields(log.Fields{
"slot": update.Payload.FinalizedHeader.Slot,
"blockRoot": update.FinalizedHeaderBlockRoot,
}).Info("syncing finalized header from Ethereum beacon client")
currentSyncPeriod := h.protocol.ComputeSyncPeriodAtSlot(uint64(update.Payload.AttestedHeader.Slot))
lastSyncedPeriod := h.protocol.ComputeSyncPeriodAtSlot(h.cache.Finalized.LastSyncedSlot)
if lastSyncedPeriod < currentSyncPeriod {
err = h.syncLaggingSyncCommitteePeriods(ctx, lastSyncedPeriod, currentSyncPeriod)
if err != nil {
return fmt.Errorf("sync lagging sync committee periods: %w", err)
}
}
return h.updateFinalizedHeaderOnchain(ctx, update)
}
// Write the provided finalized header update (possibly containing a sync committee) on-chain and check if it was
// imported successfully. Update the cache if it has and add the finalized header to the checkpoint cache.
func (h *Header) updateFinalizedHeaderOnchain(ctx context.Context, update scale.Update) error {
err := h.writer.WriteToParachainAndWatch(ctx, "EthereumBeaconClient.submit", update.Payload)
if err != nil {
return fmt.Errorf("write to parachain: %w", err)
}
lastFinalizedHeaderState, err := h.writer.GetLastFinalizedHeaderState()
if err != nil {
return fmt.Errorf("fetch last finalized header state: %w", err)
}
lastStoredHeader := lastFinalizedHeaderState.BeaconBlockRoot
if lastStoredHeader != update.FinalizedHeaderBlockRoot {
return ErrFinalizedHeaderNotImported
}
// If the finalized header import succeeded, we add it to this cache.
h.cache.SetLastSyncedFinalizedState(update.FinalizedHeaderBlockRoot, uint64(update.Payload.FinalizedHeader.Slot))
h.cache.AddCheckPoint(update.FinalizedHeaderBlockRoot, update.BlockRootsTree, uint64(update.Payload.FinalizedHeader.Slot))
return nil
}
func (h *Header) SyncHeaders(ctx context.Context) error {
finalizedUpdate, err := h.syncer.Client.GetLatestFinalizedUpdate()
if err != nil {
return fmt.Errorf("fetch finalized update: %w", err)
}
finalizedHeader, err := finalizedUpdate.Data.FinalizedHeader.Beacon.ToScale()
if err != nil {
return fmt.Errorf("convert finalized header to scale: %w", err)
}
hasChanged, err := h.syncer.HasFinalizedHeaderChanged(finalizedHeader, h.cache.Finalized.LastSyncedHash)
if err != nil {
return err
}
if !hasChanged {
return ErrFinalizedHeaderUnchanged
}
err = h.SyncFinalizedHeader(ctx)
if err != nil {
return err
}
return nil
}
func (h *Header) syncInterimFinalizedUpdate(ctx context.Context, lastSyncedSlot, newCheckpointSlot uint64) (scale.Update, error) {
currentPeriod := h.protocol.ComputeSyncPeriodAtSlot(lastSyncedSlot)
// Calculate the range that the interim finalized header update may be in
minSlot := newCheckpointSlot - h.protocol.SlotsPerHistoricalRoot
maxSlot := ((currentPeriod + 1) * h.protocol.SlotsPerHistoricalRoot) - h.protocol.Settings.SlotsInEpoch // just before the new sync committee boundary
finalizedUpdate, err := h.syncer.GetFinalizedUpdateAtAttestedSlot(minSlot, maxSlot, false)
if err != nil {
return scale.Update{}, fmt.Errorf("get interim checkpoint to update chain (last synced slot %d, new slot: %d): %w", lastSyncedSlot, newCheckpointSlot, err)
}
log.WithField("slot", finalizedUpdate.Payload.FinalizedHeader.Slot).Info("syncing an interim update to on-chain")
err = h.updateFinalizedHeaderOnchain(ctx, finalizedUpdate)
switch {
case errors.Is(err, ErrFinalizedHeaderNotImported):
return scale.Update{}, ErrInterimHeaderNotImported
case err != nil:
return scale.Update{}, fmt.Errorf("update interim finalized header on-chain: %w", err)
}
return finalizedUpdate, nil
}
func (h *Header) syncLaggingSyncCommitteePeriods(ctx context.Context, latestSyncedPeriod, currentSyncPeriod uint64) error {
// sync for all missing periods
periodsToSync := []uint64{}
for i := latestSyncedPeriod + 1; i <= currentSyncPeriod; i++ {
periodsToSync = append(periodsToSync, i)
}
log.WithFields(log.Fields{
"periods": periodsToSync,
}).Info("sync committee periods to be synced")
for _, period := range periodsToSync {
err := h.SyncCommitteePeriodUpdate(ctx, period)
if err != nil {
return err
}
}
// If Latency found between LastSyncedSyncCommitteePeriod and currentSyncPeriod in Ethereum beacon client
// just return error so to exit ASAP to allow ExecutionUpdate to catch up
lastSyncedPeriod := h.protocol.ComputeSyncPeriodAtSlot(h.cache.Finalized.LastSyncedSlot)
if lastSyncedPeriod < currentSyncPeriod {
return ErrSyncCommitteeLatency
}
return nil
}
func (h *Header) populateFinalizedCheckpoint(slot uint64) error {
finalizedHeader, err := h.syncer.Client.GetHeaderBySlot(slot)
if err != nil {
return fmt.Errorf("get header by slot: %w", err)
}
scaleHeader, err := finalizedHeader.ToScale()
if err != nil {
return fmt.Errorf("header to scale: %w", err)
}
blockRoot, err := scaleHeader.ToSSZ().HashTreeRoot()
if err != nil {
return fmt.Errorf("header hash root: %w", err)
}
// Always check slot finalized on chain before populating checkpoint
onChainFinalizedHeader, err := h.writer.GetFinalizedHeaderStateByBlockRoot(blockRoot)
if err != nil {
return fmt.Errorf("get finalized header state by block root: %w", err)
}
if onChainFinalizedHeader.BeaconSlot != slot {
return fmt.Errorf("on chain finalized header inconsistent at slot %d", slot)
}
blockRootsProof, err := h.syncer.GetBlockRoots(slot)
if err != nil && !errors.Is(err, syncer.ErrBeaconStateAvailableYet) {
return fmt.Errorf("fetch block roots for slot %d: %w", slot, err)
}
log.Info("populating checkpoint")
h.cache.AddCheckPoint(blockRoot, blockRootsProof.Tree, slot)
return nil
}
// Find the closest finalized checkpoint for a given slot. If a checkpoint cannot be found in the local cache, look
// for a checkpoint that can be used on-chain. There should always be a checkpoint on-chain because on-chain we
// verify that there is not large gap than the sync committee period range.
func (h *Header) populateClosestCheckpoint(slot uint64) (cache.Proof, error) {
var checkpoint cache.Proof
checkpoint, err := h.cache.GetClosestCheckpoint(slot)
switch {
case errors.Is(cache.FinalizedCheckPointNotAvailable, err) || errors.Is(cache.FinalizedCheckPointNotPopulated, err):
err = h.populateCheckPointCacheWithDataFromChain(slot)
if err != nil {
// There should always be a checkpoint onchain with the range of the sync committee period slots
return checkpoint, fmt.Errorf("find checkpoint on-chain for slot %d: %w", slot, err)
}
checkpoint, err = h.cache.GetClosestCheckpoint(slot)
if err != nil {
return checkpoint, fmt.Errorf("get closest checkpoint after populating finalized header: %w", err)
}
log.WithFields(log.Fields{"slot": slot, "checkpoint": checkpoint}).Info("checkpoint after populating finalized header")
return checkpoint, nil
case err != nil:
return checkpoint, fmt.Errorf("get closest checkpoint: %w", err)
}
return checkpoint, nil
}
func (h *Header) getNextHeaderUpdateBySlot(slot uint64) (scale.HeaderUpdatePayload, error) {
slot = slot + 1
return h.getHeaderUpdateBySlot(slot)
}
func (h *Header) populateCheckPointCacheWithDataFromChain(slot uint64) error {
checkpointSlot := h.protocol.CalculateNextCheckpointSlot(slot)
lastFinalizedHeaderState, err := h.writer.GetLastFinalizedHeaderState()
if err != nil {
return fmt.Errorf("get last finalized header for the checkpoint: %w", err)
}
if slot > lastFinalizedHeaderState.BeaconSlot {
return ErrBeaconHeaderNotFinalized
}
if checkpointSlot < lastFinalizedHeaderState.BeaconSlot {
historicState, err := h.findLatestCheckPoint(slot)
if err != nil {
return fmt.Errorf("get history finalized header for the checkpoint: %w", err)
}
checkpointSlot = historicState.BeaconSlot
} else {
// Setting the checkpoint slot to what is the latest finalized header on-chain, since the checkpoint should
// not be after the latest finalized header on-chain
checkpointSlot = lastFinalizedHeaderState.BeaconSlot
}
err = h.populateFinalizedCheckpoint(checkpointSlot)
if err != nil {
return fmt.Errorf("populated local cache with finalized header found on-chain: %w", err)
}
return nil
}
func (h *Header) getHeaderUpdateBySlot(slot uint64) (scale.HeaderUpdatePayload, error) {
header, err := h.syncer.FindBeaconHeaderWithBlockIncluded(slot)
if err != nil {
return scale.HeaderUpdatePayload{}, fmt.Errorf("get next beacon header with block included: %w", err)
}
checkpoint, err := h.populateClosestCheckpoint(header.Slot)
if err != nil {
return scale.HeaderUpdatePayload{}, fmt.Errorf("populate closest checkpoint: %w", err)
}
blockRoot, err := header.HashTreeRoot()
if err != nil {
return scale.HeaderUpdatePayload{}, fmt.Errorf("header hash tree root: %w", err)
}
return h.syncer.GetHeaderUpdate(blockRoot, &checkpoint)
}
func (h *Header) FetchExecutionProof(blockRoot common.Hash) (scale.HeaderUpdatePayload, error) {
var headerUpdate scale.HeaderUpdatePayload
header, err := h.syncer.Client.GetHeaderByBlockRoot(blockRoot)
if err != nil {
return headerUpdate, fmt.Errorf("get beacon header by blockRoot: %w", err)
}
lastFinalizedHeaderState, err := h.writer.GetLastFinalizedHeaderState()
if err != nil {
return headerUpdate, fmt.Errorf("fetch last finalized header state: %w", err)
}
if header.Slot > lastFinalizedHeaderState.BeaconSlot {
return headerUpdate, ErrBeaconHeaderNotFinalized
}
headerUpdate, err = h.getHeaderUpdateBySlot(header.Slot)
if err != nil {
return headerUpdate, fmt.Errorf("get header update by slot with ancestry proof: %w", err)
}
return headerUpdate, nil
}
func (h *Header) isInitialSyncPeriod() bool {
initialPeriod := h.protocol.ComputeSyncPeriodAtSlot(h.cache.InitialCheckpointSlot)
lastFinalizedPeriod := h.protocol.ComputeSyncPeriodAtSlot(h.cache.Finalized.LastSyncedSlot)
return initialPeriod == lastFinalizedPeriod
}
func (h *Header) findLatestCheckPoint(slot uint64) (state.FinalizedHeader, error) {
var beaconState state.FinalizedHeader
lastIndex, err := h.writer.GetLastFinalizedStateIndex()
if err != nil {
return beaconState, fmt.Errorf("GetLastFinalizedStateIndex error: %w", err)
}
startIndex := uint64(lastIndex)
endIndex := uint64(0)
syncCommitteePeriod := h.protocol.Settings.SlotsInEpoch * h.protocol.Settings.EpochsPerSyncCommitteePeriod
slotPeriodIndex := slot / syncCommitteePeriod
for index := startIndex; index >= endIndex; index-- {
beaconRoot, err := h.writer.GetFinalizedBeaconRootByIndex(uint32(index))
if err != nil {
return beaconState, fmt.Errorf("GetFinalizedBeaconRootByIndex %d, error: %w", index, err)
}
beaconState, err = h.writer.GetFinalizedHeaderStateByBlockRoot(beaconRoot)
if err != nil {
return beaconState, fmt.Errorf("GetFinalizedHeaderStateByBlockRoot %s, error: %w", beaconRoot.Hex(), err)
}
statePeriodIndex := beaconState.BeaconSlot / syncCommitteePeriod
if beaconState.BeaconSlot < slot {
break
}
// Found the beaconState
if beaconState.BeaconSlot > slot && beaconState.BeaconSlot < slot+syncCommitteePeriod && slotPeriodIndex == statePeriodIndex {
break
}
}
if beaconState.BeaconSlot > slot && beaconState.BeaconSlot < slot+syncCommitteePeriod {
return beaconState, nil
}
return beaconState, fmt.Errorf("no checkpoint on chain for slot %d", slot)
}