-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathheader.go
367 lines (310 loc) · 12.8 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
package header
import (
"context"
"errors"
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/snowfork/snowbridge/relayer/relays/beacon/config"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
log "github.com/sirupsen/logrus"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
"github.com/snowfork/snowbridge/relayer/chain/parachain"
"github.com/snowfork/snowbridge/relayer/relays/beacon/cache"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer"
"golang.org/x/sync/errgroup"
)
var ErrFinalizedHeaderUnchanged = errors.New("finalized header unchanged")
var ErrFinalizedHeaderNotImported = errors.New("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")
type Header struct {
cache *cache.BeaconCache
writer *parachain.ParachainWriter
syncer *syncer.Syncer
}
func New(writer *parachain.ParachainWriter, beaconEndpoint string, setting config.SpecSettings) Header {
return Header{
cache: cache.New(setting.SlotsInEpoch, setting.EpochsPerSyncCommitteePeriod),
writer: writer,
syncer: syncer.New(beaconEndpoint, setting),
}
}
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.syncer.ComputeSyncPeriodAtSlot(lastFinalizedHeaderState.BeaconSlot)
executionHeaderState, err := h.writer.GetLastExecutionHeaderState()
if err != nil {
return fmt.Errorf("fetch last execution hash: %w", err)
}
log.WithFields(log.Fields{
"last_finalized_hash": lastFinalizedHeaderState.BeaconBlockRoot,
"last_finalized_slot": lastFinalizedHeaderState.BeaconSlot,
"last_finalized_period": latestSyncedPeriod,
"last_execution_hash": executionHeaderState.BeaconBlockRoot,
"last_execution_slot": executionHeaderState.BeaconSlot,
}).Info("set cache: Current state")
h.cache.SetLastSyncedFinalizedState(lastFinalizedHeaderState.BeaconBlockRoot, lastFinalizedHeaderState.BeaconSlot)
h.cache.SetInitialCheckpointSlot(lastFinalizedHeaderState.InitialCheckpointSlot)
h.cache.AddCheckPointSlots([]uint64{lastFinalizedHeaderState.BeaconSlot})
h.cache.SetLastSyncedExecutionSlot(executionHeaderState.BeaconSlot)
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)
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)
}
}
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.syncer.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.syncer.ComputeSyncPeriodAtSlot(uint64(update.Payload.AttestedHeader.Slot))
lastSyncedPeriod := h.syncer.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)
}
}
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 {
hasChanged, err := h.syncer.HasFinalizedHeaderChanged(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) 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)
}
// For initialPeriod special handling here to sync it again for nextSyncCommittee which is not included in InitCheckpoint
if h.isInitialSyncPeriod() {
periodsToSync = append([]uint64{latestSyncedPeriod}, periodsToSync...)
}
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.syncer.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 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: %w", err)
}
log.Info("populating checkpoint")
h.cache.AddCheckPoint(blockRoot, blockRootsProof.Tree, slot)
return nil
}
func (h *Header) populateClosestCheckpoint(slot uint64) (cache.Proof, error) {
checkpoint, err := h.cache.GetClosestCheckpoint(slot)
switch {
case errors.Is(cache.FinalizedCheckPointNotAvailable, err) || errors.Is(cache.FinalizedCheckPointNotPopulated, err):
checkpointSlot := checkpoint.Slot
if checkpointSlot == 0 {
checkpointSlot = h.syncer.CalculateNextCheckpointSlot(slot)
lastFinalizedHeaderState, err := h.writer.GetLastFinalizedHeaderState()
if err != nil {
return cache.Proof{}, fmt.Errorf("fetch parachain last finalized header state: %w", err)
}
if checkpointSlot > lastFinalizedHeaderState.BeaconSlot {
checkpointSlot = lastFinalizedHeaderState.BeaconSlot
}
log.WithFields(log.Fields{"calculatedCheckpointSlot": checkpointSlot}).Info("checkpoint slot not available, try with slot in next sync period instead")
}
err := h.populateFinalizedCheckpoint(checkpointSlot)
if err != nil {
return cache.Proof{}, fmt.Errorf("populate closest checkpoint: %w", err)
}
log.Info("populated finalized checkpoint")
checkpoint, err = h.cache.GetClosestCheckpoint(slot)
if err != nil {
return cache.Proof{}, 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 cache.Proof{}, fmt.Errorf("get closest checkpoint: %w", err)
}
return checkpoint, 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) SyncExecutionHeader(ctx context.Context, blockRoot common.Hash) error {
header, err := h.syncer.Client.GetHeader(blockRoot)
if err != nil {
return fmt.Errorf("get beacon header by blockRoot: %w", err)
}
headerUpdate, err := h.getHeaderUpdateBySlot(header.Slot)
if err != nil {
return fmt.Errorf("get header update by slot with ancestry proof: %w", err)
}
err = h.writer.WriteToParachainAndWatch(ctx, "EthereumBeaconClient.submit_execution_header", headerUpdate)
if err != nil {
return fmt.Errorf("submit_execution_header: %w", err)
}
var blockHash types.H256
if headerUpdate.ExecutionHeader.Deneb != nil {
blockHash = headerUpdate.ExecutionHeader.Deneb.BlockHash
} else if headerUpdate.ExecutionHeader.Capella != nil {
blockHash = headerUpdate.ExecutionHeader.Capella.BlockHash
} else {
return fmt.Errorf("invalid blockHash in headerUpdate")
}
compactExecutionHeaderState, err := h.writer.GetCompactExecutionHeaderStateByBlockHash(blockHash)
if err != nil {
return fmt.Errorf("get compactExecutionHeaderState by blockHash: %w", err)
}
if compactExecutionHeaderState.BlockNumber == 0 {
return fmt.Errorf("invalid compactExecutionHeaderState")
}
return nil
}
func (h *Header) isInitialSyncPeriod() bool {
initialPeriod := h.syncer.ComputeSyncPeriodAtSlot(h.cache.InitialCheckpointSlot)
lastFinalizedPeriod := h.syncer.ComputeSyncPeriodAtSlot(h.cache.Finalized.LastSyncedSlot)
return initialPeriod == lastFinalizedPeriod
}