forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_headers.go
612 lines (564 loc) · 17.8 KB
/
stage_headers.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package stagedsync
import (
"context"
"encoding/binary"
"fmt"
"math/big"
"runtime"
"time"
"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/rawdb/blockio"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/engineapi/engine_helpers"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/erigon/turbo/shards"
"github.com/ledgerwatch/erigon/turbo/stages/bodydownload"
"github.com/ledgerwatch/erigon/turbo/stages/headerdownload"
)
// The number of blocks we should be able to re-org sub-second on commodity hardware.
// See https://hackmd.io/TdJtNs0dS56q-In8h-ShSg
const ShortPoSReorgThresholdBlocks = 10
type HeadersCfg struct {
db kv.RwDB
hd *headerdownload.HeaderDownload
bodyDownload *bodydownload.BodyDownload
chainConfig chain.Config
headerReqSend func(context.Context, *headerdownload.HeaderRequest) ([64]byte, bool)
announceNewHashes func(context.Context, []headerdownload.Announce)
penalize func(context.Context, []headerdownload.PenaltyItem)
batchSize datasize.ByteSize
noP2PDiscovery bool
tmpdir string
blockReader services.FullBlockReader
blockWriter *blockio.BlockWriter
forkValidator *engine_helpers.ForkValidator
notifications *shards.Notifications
syncConfig ethconfig.Sync
loopBreakCheck func(int) bool
}
func StageHeadersCfg(
db kv.RwDB,
headerDownload *headerdownload.HeaderDownload,
bodyDownload *bodydownload.BodyDownload,
chainConfig chain.Config,
syncConfig ethconfig.Sync,
headerReqSend func(context.Context, *headerdownload.HeaderRequest) ([64]byte, bool),
announceNewHashes func(context.Context, []headerdownload.Announce),
penalize func(context.Context, []headerdownload.PenaltyItem),
batchSize datasize.ByteSize,
noP2PDiscovery bool,
blockReader services.FullBlockReader,
blockWriter *blockio.BlockWriter,
tmpdir string,
notifications *shards.Notifications,
forkValidator *engine_helpers.ForkValidator,
loopBreakCheck func(int) bool) HeadersCfg {
return HeadersCfg{
db: db,
hd: headerDownload,
bodyDownload: bodyDownload,
chainConfig: chainConfig,
syncConfig: syncConfig,
headerReqSend: headerReqSend,
announceNewHashes: announceNewHashes,
penalize: penalize,
batchSize: batchSize,
tmpdir: tmpdir,
noP2PDiscovery: noP2PDiscovery,
blockReader: blockReader,
blockWriter: blockWriter,
forkValidator: forkValidator,
notifications: notifications,
loopBreakCheck: loopBreakCheck,
}
}
func SpawnStageHeaders(
s *StageState,
u Unwinder,
ctx context.Context,
tx kv.RwTx,
cfg HeadersCfg,
initialCycle bool,
test bool, // Returns true to allow the stage to stop rather than wait indefinitely
logger log.Logger,
) error {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
if initialCycle && cfg.blockReader.FreezingCfg().Enabled {
if err := cfg.hd.AddHeadersFromSnapshot(tx, cfg.blockReader); err != nil {
return err
}
}
return HeadersPOW(s, u, ctx, tx, cfg, initialCycle, test, useExternalTx, logger)
}
// HeadersPOW progresses Headers stage for Proof-of-Work headers
func HeadersPOW(
s *StageState,
u Unwinder,
ctx context.Context,
tx kv.RwTx,
cfg HeadersCfg,
initialCycle bool,
test bool, // Returns true to allow the stage to stop rather than wait indefinitely
useExternalTx bool,
logger log.Logger,
) error {
var err error
startTime := time.Now()
if err = cfg.hd.ReadProgressFromDb(tx); err != nil {
return err
}
cfg.hd.SetPOSSync(false)
cfg.hd.SetFetchingNew(true)
defer cfg.hd.SetFetchingNew(false)
startProgress := cfg.hd.Progress()
logPrefix := s.LogPrefix()
// Check if this is called straight after the unwinds, which means we need to create new canonical markings
hash, err := cfg.blockReader.CanonicalHash(ctx, tx, startProgress)
if err != nil {
return err
}
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
if hash == (libcommon.Hash{}) {
headHash := rawdb.ReadHeadHeaderHash(tx)
if err = fixCanonicalChain(logPrefix, logEvery, startProgress, headHash, tx, cfg.blockReader, logger); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
// Allow other stages to run 1 cycle if no network available
if initialCycle && cfg.noP2PDiscovery {
return nil
}
logger.Info(fmt.Sprintf("[%s] Waiting for headers...", logPrefix), "from", startProgress)
localTd, err := rawdb.ReadTd(tx, hash, startProgress)
if err != nil {
return err
}
/* TEMP TESTING
if localTd == nil {
return fmt.Errorf("localTD is nil: %d, %x", startProgress, hash)
}
TEMP TESTING */
headerInserter := headerdownload.NewHeaderInserter(logPrefix, localTd, startProgress, cfg.blockReader)
cfg.hd.SetHeaderReader(&ChainReaderImpl{
config: &cfg.chainConfig,
tx: tx,
blockReader: cfg.blockReader,
logger: logger,
})
stopped := false
var noProgressCounter uint = 0
prevProgress := startProgress
var wasProgress bool
var lastSkeletonTime time.Time
var peer [64]byte
var sentToPeer bool
Loop:
for !stopped {
transitionedToPoS, err := rawdb.Transitioned(tx, startProgress, cfg.chainConfig.TerminalTotalDifficulty)
if err != nil {
return err
}
if transitionedToPoS {
if err := s.Update(tx, startProgress); err != nil {
return err
}
s.state.posTransition = &startProgress
break
}
sentToPeer = false
currentTime := time.Now()
req, penalties := cfg.hd.RequestMoreHeaders(currentTime)
if req != nil {
peer, sentToPeer = cfg.headerReqSend(ctx, req)
if sentToPeer {
logger.Debug(fmt.Sprintf("[%s] Requested header", logPrefix), "from", req.Number, "length", req.Length)
cfg.hd.UpdateStats(req, false /* skeleton */, peer)
cfg.hd.UpdateRetryTime(req, currentTime, 5*time.Second /* timeout */)
}
}
if len(penalties) > 0 {
cfg.penalize(ctx, penalties)
}
maxRequests := 64 // Limit number of requests sent per round to let some headers to be inserted into the database
for req != nil && sentToPeer && maxRequests > 0 {
req, penalties = cfg.hd.RequestMoreHeaders(currentTime)
if req != nil {
peer, sentToPeer = cfg.headerReqSend(ctx, req)
if sentToPeer {
cfg.hd.UpdateStats(req, false /* skeleton */, peer)
cfg.hd.UpdateRetryTime(req, currentTime, 5*time.Second /* timeout */)
}
}
if len(penalties) > 0 {
cfg.penalize(ctx, penalties)
}
maxRequests--
}
// Send skeleton request if required
if time.Since(lastSkeletonTime) > 1*time.Second {
req = cfg.hd.RequestSkeleton()
if req != nil {
peer, sentToPeer = cfg.headerReqSend(ctx, req)
if sentToPeer {
logger.Debug(fmt.Sprintf("[%s] Requested skeleton", logPrefix), "from", req.Number, "length", req.Length)
cfg.hd.UpdateStats(req, true /* skeleton */, peer)
lastSkeletonTime = time.Now()
}
}
}
// Load headers into the database
inSync, err := cfg.hd.InsertHeaders(headerInserter.NewFeedHeaderFunc(tx, cfg.blockReader), cfg.syncConfig.LoopBlockLimit, cfg.chainConfig.TerminalTotalDifficulty, logPrefix, logEvery.C, uint64(currentTime.Unix()))
if err != nil {
return err
}
if headerInserter.BestHeaderChanged() { // We do not break unless there best header changed
noProgressCounter = 0
wasProgress = true
// if this is initial cycle, we want to make sure we insert all known headers (inSync)
if inSync {
break
}
}
if cfg.syncConfig.LoopBlockLimit > 0 {
if bodyProgress, err := stages.GetStageProgress(tx, stages.Bodies); err == nil {
if cfg.hd.Progress() > bodyProgress && cfg.hd.Progress()-bodyProgress > uint64(cfg.syncConfig.LoopBlockLimit*2) {
break
}
}
}
if cfg.loopBreakCheck != nil && cfg.loopBreakCheck(int(cfg.hd.Progress()-startProgress)) {
break
}
if test {
announces := cfg.hd.GrabAnnounces()
if len(announces) > 0 {
cfg.announceNewHashes(ctx, announces)
}
break
}
timer := time.NewTimer(1 * time.Second)
select {
case <-ctx.Done():
stopped = true
case <-logEvery.C:
progress := cfg.hd.Progress()
stats := cfg.hd.ExtractStats()
logProgressHeaders(logPrefix, prevProgress, progress, stats, logger)
if prevProgress == progress {
noProgressCounter++
} else {
noProgressCounter = 0 // Reset, there was progress
}
if noProgressCounter >= 5 {
logger.Info("Req/resp stats", "req", stats.Requests, "reqMin", stats.ReqMinBlock, "reqMax", stats.ReqMaxBlock,
"skel", stats.SkeletonRequests, "skelMin", stats.SkeletonReqMinBlock, "skelMax", stats.SkeletonReqMaxBlock,
"resp", stats.Responses, "respMin", stats.RespMinBlock, "respMax", stats.RespMaxBlock, "dups", stats.Duplicates)
cfg.hd.LogAnchorState()
if wasProgress {
logger.Warn("Looks like chain is not progressing, moving to the next stage")
break Loop
}
}
prevProgress = progress
case <-timer.C:
logger.Trace("RequestQueueTime (header) ticked")
case <-cfg.hd.DeliveryNotify:
logger.Trace("headerLoop woken up by the incoming request")
}
timer.Stop()
}
if headerInserter.Unwind() {
u.UnwindTo(headerInserter.UnwindPoint(), StagedUnwind)
}
if headerInserter.GetHighest() != 0 {
if !headerInserter.Unwind() {
if err := fixCanonicalChain(logPrefix, logEvery, headerInserter.GetHighest(), headerInserter.GetHighestHash(), tx, cfg.blockReader, logger); err != nil {
return fmt.Errorf("fix canonical chain: %w", err)
}
}
if err = rawdb.WriteHeadHeaderHash(tx, headerInserter.GetHighestHash()); err != nil {
return fmt.Errorf("[%s] marking head header hash as %x: %w", logPrefix, headerInserter.GetHighestHash(), err)
}
if err = s.Update(tx, headerInserter.GetHighest()); err != nil {
return fmt.Errorf("[%s] saving Headers progress: %w", logPrefix, err)
}
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
if stopped {
return libcommon.ErrStopped
}
// We do not print the following line if the stage was interrupted
if s.state.posTransition != nil {
logger.Info(fmt.Sprintf("[%s] Transitioned to POS", logPrefix), "block", *s.state.posTransition)
} else {
headers := headerInserter.GetHighest() - startProgress
secs := time.Since(startTime).Seconds()
logger.Info(fmt.Sprintf("[%s] Processed", logPrefix),
"highest", headerInserter.GetHighest(), "age", common.PrettyAge(time.Unix(int64(headerInserter.GetHighestTimestamp()), 0)),
"headers", headers, "in", secs, "blk/sec", uint64(float64(headers)/secs))
}
return nil
}
func fixCanonicalChain(logPrefix string, logEvery *time.Ticker, height uint64, hash libcommon.Hash, tx kv.StatelessRwTx, headerReader services.FullBlockReader, logger log.Logger) error {
if height == 0 {
return nil
}
ancestorHash := hash
ancestorHeight := height
var ch libcommon.Hash
var err error
for ch, err = headerReader.CanonicalHash(context.Background(), tx, ancestorHeight); err == nil && ch != ancestorHash; ch, err = headerReader.CanonicalHash(context.Background(), tx, ancestorHeight) {
if err = rawdb.WriteCanonicalHash(tx, ancestorHash, ancestorHeight); err != nil {
return fmt.Errorf("marking canonical header %d %x: %w", ancestorHeight, ancestorHash, err)
}
ancestor, err := headerReader.Header(context.Background(), tx, ancestorHash, ancestorHeight)
if err != nil {
return err
}
if ancestor == nil {
return fmt.Errorf("ancestor is nil. height %d, hash %x", ancestorHeight, ancestorHash)
}
select {
case <-logEvery.C:
logger.Info(fmt.Sprintf("[%s] write canonical markers", logPrefix), "ancestor", ancestorHeight, "hash", ancestorHash)
default:
}
ancestorHash = ancestor.ParentHash
ancestorHeight--
}
if err != nil {
return fmt.Errorf("reading canonical hash for %d: %w", ancestorHeight, err)
}
return nil
}
func HeadersUnwind(u *UnwindState, s *StageState, tx kv.RwTx, cfg HeadersCfg, test bool) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
}
// Delete canonical hashes that are being unwound
unwindBlock := (u.Reason.Block != nil)
if unwindBlock {
if u.Reason.IsBadBlock() {
cfg.hd.ReportBadHeader(*u.Reason.Block)
}
cfg.hd.UnlinkHeader(*u.Reason.Block)
// Mark all descendants of bad block as bad too
headerCursor, cErr := tx.Cursor(kv.Headers)
if cErr != nil {
return cErr
}
defer headerCursor.Close()
var k, v []byte
for k, v, err = headerCursor.Seek(hexutility.EncodeTs(u.UnwindPoint + 1)); err == nil && k != nil; k, v, err = headerCursor.Next() {
var h types.Header
if err = rlp.DecodeBytes(v, &h); err != nil {
return err
}
if cfg.hd.IsBadHeader(h.ParentHash) {
cfg.hd.ReportBadHeader(h.Hash())
}
}
if err != nil {
return fmt.Errorf("iterate over headers to mark bad headers: %w", err)
}
}
if err := rawdb.TruncateCanonicalHash(tx, u.UnwindPoint+1, unwindBlock); err != nil {
return err
}
if unwindBlock {
var maxTd big.Int
var maxHash libcommon.Hash
var maxNum uint64 = 0
if test { // If we are not in the test, we can do searching for the heaviest chain in the next cycle
// Find header with biggest TD
tdCursor, cErr := tx.Cursor(kv.HeaderTD)
if cErr != nil {
return cErr
}
defer tdCursor.Close()
var k, v []byte
k, v, err = tdCursor.Last()
if err != nil {
return err
}
for ; err == nil && k != nil; k, v, err = tdCursor.Prev() {
if len(k) != 40 {
return fmt.Errorf("key in TD table has to be 40 bytes long: %x", k)
}
var hash libcommon.Hash
copy(hash[:], k[8:])
if cfg.hd.IsBadHeader(hash) {
continue
}
var td big.Int
if err = rlp.DecodeBytes(v, &td); err != nil {
return err
}
if td.Cmp(&maxTd) > 0 {
maxTd.Set(&td)
copy(maxHash[:], k[8:])
maxNum = binary.BigEndian.Uint64(k[:8])
}
}
if err != nil {
return err
}
}
/* TODO(yperbasis): Is it safe?
if err := rawdb.TruncateTd(tx, u.UnwindPoint+1); err != nil {
return err
}
*/
if maxNum == 0 {
maxNum = u.UnwindPoint
if maxHash, err = rawdb.ReadCanonicalHash(tx, maxNum); err != nil {
return err
}
}
if err = rawdb.WriteHeadHeaderHash(tx, maxHash); err != nil {
return err
}
if err = u.Done(tx); err != nil {
return err
}
if err = s.Update(tx, maxNum); err != nil {
return err
}
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func logProgressHeaders(
logPrefix string,
prev uint64,
now uint64,
stats headerdownload.Stats,
logger log.Logger,
) uint64 {
speed := float64(now-prev) / float64(logInterval/time.Second)
var message string
if speed == 0 {
message = "No block headers to write in this log period"
} else {
message = "Wrote block headers"
}
var m runtime.MemStats
dbg.ReadMemStats(&m)
logger.Info(fmt.Sprintf("[%s] %s", logPrefix, message),
"number", now,
"blk/second", speed,
"alloc", libcommon.ByteCount(m.Alloc),
"sys", libcommon.ByteCount(m.Sys),
"invalidHeaders", stats.InvalidHeaders,
"rejectedBadHeaders", stats.RejectedBadHeaders,
)
return now
}
type ChainReaderImpl struct {
config *chain.Config
tx kv.Tx
blockReader services.FullBlockReader
logger log.Logger
}
func NewChainReaderImpl(config *chain.Config, tx kv.Tx, blockReader services.FullBlockReader, logger log.Logger) *ChainReaderImpl {
return &ChainReaderImpl{config, tx, blockReader, logger}
}
func (cr ChainReaderImpl) Config() *chain.Config { return cr.config }
func (cr ChainReaderImpl) CurrentHeader() *types.Header { panic("") }
func (cr ChainReaderImpl) GetHeader(hash libcommon.Hash, number uint64) *types.Header {
if cr.blockReader != nil {
h, _ := cr.blockReader.Header(context.Background(), cr.tx, hash, number)
return h
}
return rawdb.ReadHeader(cr.tx, hash, number)
}
func (cr ChainReaderImpl) GetHeaderByNumber(number uint64) *types.Header {
if cr.blockReader != nil {
h, _ := cr.blockReader.HeaderByNumber(context.Background(), cr.tx, number)
return h
}
return rawdb.ReadHeaderByNumber(cr.tx, number)
}
func (cr ChainReaderImpl) GetHeaderByHash(hash libcommon.Hash) *types.Header {
if cr.blockReader != nil {
number := rawdb.ReadHeaderNumber(cr.tx, hash)
if number == nil {
return nil
}
return cr.GetHeader(hash, *number)
}
h, _ := rawdb.ReadHeaderByHash(cr.tx, hash)
return h
}
func (cr ChainReaderImpl) GetTd(hash libcommon.Hash, number uint64) *big.Int {
td, err := rawdb.ReadTd(cr.tx, hash, number)
if err != nil {
cr.logger.Error("ReadTd failed", "err", err)
return nil
}
return td
}
func (cr ChainReaderImpl) FrozenBlocks() uint64 {
return cr.blockReader.FrozenBlocks()
}
func (cr ChainReaderImpl) GetBlock(hash libcommon.Hash, number uint64) *types.Block {
panic("")
}
func (cr ChainReaderImpl) HasBlock(hash libcommon.Hash, number uint64) bool {
panic("")
}
func (cr ChainReaderImpl) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue {
events, err := cr.blockReader.EventsByBlock(context.Background(), cr.tx, hash, number)
if err != nil {
cr.logger.Error("BorEventsByBlock failed", "err", err)
return nil
}
return events
}
func (cr ChainReaderImpl) BorSpan(spanId uint64) []byte {
span, err := cr.blockReader.Span(context.Background(), cr.tx, spanId)
if err != nil {
cr.logger.Error("[staged sync] BorSpan failed", "err", err)
return nil
}
return span
}