forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 44
/
stage_bodies.go
462 lines (411 loc) · 14.8 KB
/
stage_bodies.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
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package stagedsync
import (
"context"
"fmt"
"github.com/erigontech/erigon/core"
"runtime"
"time"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/dbg"
"github.com/erigontech/erigon-lib/common/metrics"
"github.com/erigontech/erigon-lib/diagnostics"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/rawdb/blockio"
"github.com/erigontech/erigon/dataflow"
"github.com/erigontech/erigon/eth/stagedsync/stages"
"github.com/erigontech/erigon/turbo/adapter"
"github.com/erigontech/erigon/turbo/services"
"github.com/erigontech/erigon/turbo/stages/bodydownload"
"github.com/erigontech/erigon/turbo/stages/headerdownload"
)
const requestLoopCutOff int = 1
type BodiesCfg struct {
db kv.RwDB
blobStore services.BlobStorage
bd *bodydownload.BodyDownload
bodyReqSend func(context.Context, *bodydownload.BodyRequest) ([64]byte, bool)
penalise func(context.Context, []headerdownload.PenaltyItem)
blockPropagator adapter.BlockPropagator
timeout int
chanConfig chain.Config
blockReader services.FullBlockReader
blockWriter *blockio.BlockWriter
}
func StageBodiesCfg(db kv.RwDB, blobStore services.BlobStorage, bd *bodydownload.BodyDownload,
bodyReqSend func(context.Context, *bodydownload.BodyRequest) ([64]byte, bool), penalise func(context.Context, []headerdownload.PenaltyItem),
blockPropagator adapter.BlockPropagator, timeout int,
chanConfig chain.Config,
blockReader services.FullBlockReader,
blockWriter *blockio.BlockWriter,
) BodiesCfg {
return BodiesCfg{
db: db, bd: bd, blobStore: blobStore, bodyReqSend: bodyReqSend, penalise: penalise, blockPropagator: blockPropagator,
timeout: timeout, chanConfig: chanConfig, blockReader: blockReader,
blockWriter: blockWriter}
}
// BodiesForward progresses Bodies stage in the forward direction
func BodiesForward(s *StageState, u Unwinder, ctx context.Context, tx kv.RwTx, cfg BodiesCfg, test bool, logger log.Logger) error {
var doUpdate bool
startTime := time.Now()
if s.BlockNumber < cfg.blockReader.FrozenBlocks() {
s.BlockNumber = cfg.blockReader.FrozenBlocks()
doUpdate = true
}
var d1, d2, d3, d4, d5, d6 time.Duration
var err error
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
}
timeout := cfg.timeout
// this update is required, because cfg.bd.UpdateFromDb(tx) below reads it and initialises requestedLow accordingly
// if not done, it will cause downloading from block 1
if doUpdate {
if err := s.Update(tx, s.BlockNumber); err != nil {
return err
}
}
// This will update bd.maxProgress
if _, _, _, _, err = cfg.bd.UpdateFromDb(tx); err != nil {
return err
}
defer cfg.bd.ClearBodyCache()
var headerProgress, bodyProgress uint64
if cfg.chanConfig.Bor != nil {
headerProgress, err = stages.GetStageProgress(tx, stages.BorHeimdall)
if err != nil {
return err
}
}
if headerProgress == 0 {
headerProgress, err = stages.GetStageProgress(tx, stages.Headers)
if err != nil {
return err
}
}
bodyProgress = s.BlockNumber
if bodyProgress >= headerProgress {
return nil
}
logPrefix := s.LogPrefix()
if headerProgress <= bodyProgress+16 {
// When processing small number of blocks, we can afford wasting more bandwidth but get blocks quicker
timeout = 1
} else {
// Do not print logs for short periods
diagnostics.Send(diagnostics.BodiesProcessingUpdate{
From: bodyProgress,
To: headerProgress,
})
logger.Info(fmt.Sprintf("[%s] Processing bodies...", logPrefix), "from", bodyProgress, "to", headerProgress)
}
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
// Property of blockchain: same block in different forks will have different hashes.
// Means - can mark all canonical blocks as non-canonical on unwind, and
// do opposite here - without storing any meta-info.
if err := cfg.blockWriter.MakeBodiesCanonical(tx, s.BlockNumber+1); err != nil {
return fmt.Errorf("make block canonical: %w", err)
}
var prevDeliveredCount float64 = 0
var prevWastedCount float64 = 0
timer := time.NewTimer(1 * time.Second) // Check periodically even in the abseence of incoming messages
var req *bodydownload.BodyRequest
var peer [64]byte
var sentToPeer bool
stopped := false
prevProgress := bodyProgress
var noProgressCount uint = 0 // How many time the progress was printed without actual progress
var totalDelivered uint64 = 0
cr := ChainReader{Cfg: cfg.chanConfig, Db: tx, BlockReader: cfg.blockReader, Logger: logger}
loopBody := func() (bool, error) {
// loopCount is used here to ensure we don't get caught in a constant loop of making requests
// having some time out so requesting again and cycling like that forever. We'll cap it
// and break the loop so we can see if there are any records to actually process further down
// then come back here again in the next cycle
for loopCount := 0; loopCount == 0 || (req != nil && sentToPeer && loopCount < requestLoopCutOff); loopCount++ {
start := time.Now()
currentTime := uint64(time.Now().Unix())
req, err = cfg.bd.RequestMoreBodies(tx, cfg.blockReader, currentTime, cfg.blockPropagator)
if err != nil {
return false, fmt.Errorf("request more bodies: %w", err)
}
d1 += time.Since(start)
peer = [64]byte{}
sentToPeer = false
if req != nil {
start = time.Now()
peer, sentToPeer = cfg.bodyReqSend(ctx, req)
d2 += time.Since(start)
}
if req != nil && sentToPeer {
start = time.Now()
cfg.bd.RequestSent(req, currentTime+uint64(timeout), peer)
d3 += time.Since(start)
}
}
start := time.Now()
requestedLow, delivered, err := cfg.bd.GetDeliveries(tx)
if err != nil {
return false, err
}
// this can happen if we have bor heimdall processing
// as the body downloader only has access to headers which
// may be higher than the current bor processing stage
if requestedLow > headerProgress {
requestedLow = headerProgress
}
totalDelivered += delivered
d4 += time.Since(start)
start = time.Now()
toProcess := cfg.bd.NextProcessingCount()
write := true
for i := uint64(0); i < toProcess; i++ {
select {
case <-logEvery.C:
logWritingBodies(logPrefix, bodyProgress, headerProgress, logger)
default:
}
nextBlock := requestedLow + i
rawBody := cfg.bd.GetBodyFromCache(nextBlock, write /* delete */)
if rawBody == nil {
cfg.bd.NotDelivered(nextBlock)
write = false
}
if !write {
continue
}
cfg.bd.NotDelivered(nextBlock)
header, _, err := cfg.bd.GetHeader(nextBlock, cfg.blockReader, tx)
if err != nil {
return false, err
}
// this check is necessary if we have bor heimdall processing as the body downloader only has
// access to headers which may be higher than the current bor processing stage
if headerNumber := header.Number.Uint64(); headerNumber <= headerProgress {
blockHeight := headerNumber
if blockHeight != nextBlock {
return false, fmt.Errorf("[%s] Header block unexpected when matching body, got %v, expected %v", logPrefix, blockHeight, nextBlock)
}
// Txn & uncle roots are verified via bd.requestedMap
err = cfg.bd.Engine.VerifyUncles(cr, header, rawBody.Uncles)
if err != nil {
logger.Error(fmt.Sprintf("[%s] Uncle verification failed", logPrefix), "number", blockHeight, "hash", header.Hash().String(), "err", err)
if err := u.UnwindTo(blockHeight-1, BadBlock(header.Hash(), fmt.Errorf("Uncle verification failed: %w", err)), tx); err != nil {
return false, err
}
return true, nil
}
metrics.UpdateBlockConsumerBodyDownloadDelay(header.Time, header.Number.Uint64(), logger)
if cfg.chanConfig.Parlia != nil && cfg.chanConfig.IsCancun(headerNumber, header.Time) {
if err = core.IsDataAvailable(cr, header, rawBody, cfg.bd.LatestBlock); err != nil {
return false, err
}
}
// Check existence before write - because WriteRawBody isn't idempotent (it allocates new sequence range for transactions on every call)
ok, err := rawdb.WriteRawBodyIfNotExists(tx, header.Hash(), blockHeight, rawBody)
if err != nil {
return false, fmt.Errorf("WriteRawBodyIfNotExists: %w", err)
}
if ok {
if err := rawdb.AppendCanonicalTxNums(tx, blockHeight); err != nil {
return false, err
}
}
if ok && cfg.chanConfig.Parlia != nil && cfg.chanConfig.IsCancun(headerNumber, header.Time) {
_, foundOnDisk, err := cfg.blobStore.ReadBlobSidecars(ctx, header.Number.Uint64(), header.Hash())
if err != nil {
return false, err
}
if !foundOnDisk {
err = cfg.blobStore.WriteBlobSidecars(ctx, header.Hash(), rawBody.Sidecars)
if err != nil {
return false, fmt.Errorf("WriteBlobSidecars: %w", err)
}
logger.Trace("WriteBlobSidecars", "block number", header.Number, "len(sidecars)", len(rawBody.Sidecars))
}
}
if ok {
dataflow.BlockBodyDownloadStates.AddChange(blockHeight, dataflow.BlockBodyCleared)
}
if blockHeight > bodyProgress {
bodyProgress = blockHeight
if err = s.Update(tx, blockHeight); err != nil {
return false, fmt.Errorf("saving Bodies progress: %w", err)
}
}
cfg.bd.AdvanceLow()
}
}
d5 += time.Since(start)
start = time.Now()
if bodyProgress == headerProgress {
return true, nil
}
if test {
stopped = true
return true, nil
}
firstCycle := s.CurrentSyncCycle.IsInitialCycle
if !firstCycle && s.BlockNumber > 0 && noProgressCount >= 5 {
return true, nil
}
timer.Stop()
timer = time.NewTimer(1 * time.Second)
select {
case <-ctx.Done():
stopped = true
case <-logEvery.C:
deliveredCount, wastedCount := cfg.bd.DeliveryCounts()
if prevProgress == bodyProgress {
noProgressCount++
} else {
noProgressCount = 0 // Reset, there was progress
}
logDownloadingBodies(logPrefix, bodyProgress, headerProgress-requestedLow, totalDelivered, prevDeliveredCount, deliveredCount,
prevWastedCount, wastedCount, cfg.bd.BodyCacheSize(), logger)
prevProgress = bodyProgress
prevDeliveredCount = deliveredCount
prevWastedCount = wastedCount
//logger.Info("Timings", "d1", d1, "d2", d2, "d3", d3, "d4", d4, "d5", d5, "d6", d6)
case <-timer.C:
logger.Trace("RequestQueueTime (bodies) ticked")
case <-cfg.bd.DeliveryNotify:
logger.Trace("bodyLoop woken up by the incoming request")
}
d6 += time.Since(start)
return false, nil
}
// kick off the loop and check for any reason to stop and break early
var shouldBreak bool
for !stopped && !shouldBreak {
if shouldBreak, err = loopBody(); err != nil {
return err
}
}
// remove the temporary bucket for bodies stage
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
if stopped {
return libcommon.ErrStopped
}
if bodyProgress > s.BlockNumber+16 {
blocks := bodyProgress - s.BlockNumber
secs := time.Since(startTime).Seconds()
diagnostics.Send(diagnostics.BodiesProcessedUpdate{
HighestBlock: bodyProgress,
Blocks: blocks,
TimeElapsed: secs,
BlkPerSec: float64(blocks) / secs,
})
logger.Info(fmt.Sprintf("[%s] Processed", logPrefix), "highest", bodyProgress,
"blocks", blocks, "in", secs, "blk/sec", uint64(float64(blocks)/secs))
}
return nil
}
func logDownloadingBodies(logPrefix string, committed, remaining uint64, totalDelivered uint64, prevDeliveredCount, deliveredCount,
prevWastedCount, wastedCount float64, bodyCacheSize int, logger log.Logger) {
speed := (deliveredCount - prevDeliveredCount) / float64(logInterval/time.Second)
wastedSpeed := (wastedCount - prevWastedCount) / float64(logInterval/time.Second)
if speed == 0 && wastedSpeed == 0 {
logger.Info(fmt.Sprintf("[%s] No block bodies to write in this log period", logPrefix), "block number", committed)
return
}
var m runtime.MemStats
dbg.ReadMemStats(&m)
diagnostics.Send(diagnostics.BodiesDownloadBlockUpdate{
BlockNumber: committed,
DeliveryPerSec: uint64(speed),
WastedPerSec: uint64(wastedSpeed),
Remaining: remaining,
Delivered: totalDelivered,
BlockPerSec: totalDelivered / uint64(logInterval/time.Second),
Cache: uint64(bodyCacheSize),
Alloc: m.Alloc,
Sys: m.Sys,
})
logger.Info(fmt.Sprintf("[%s] Downloading block bodies", logPrefix),
"block_num", committed,
"delivery/sec", libcommon.ByteCount(uint64(speed)),
"wasted/sec", libcommon.ByteCount(uint64(wastedSpeed)),
"remaining", remaining,
"delivered", totalDelivered,
"blk/sec", totalDelivered/uint64(logInterval/time.Second),
"cache", libcommon.ByteCount(uint64(bodyCacheSize)),
"alloc", libcommon.ByteCount(m.Alloc),
"sys", libcommon.ByteCount(m.Sys),
)
}
func logWritingBodies(logPrefix string, committed, headerProgress uint64, logger log.Logger) {
var m runtime.MemStats
dbg.ReadMemStats(&m)
remaining := headerProgress - committed
diagnostics.Send(diagnostics.BodiesWriteBlockUpdate{
BlockNumber: committed,
Remaining: remaining,
Alloc: m.Alloc,
Sys: m.Sys,
})
logger.Info(fmt.Sprintf("[%s] Writing bodies", logPrefix),
"block_num", committed,
"remaining", remaining,
"alloc", libcommon.ByteCount(m.Alloc),
"sys", libcommon.ByteCount(m.Sys),
)
}
func UnwindBodiesStage(u *UnwindState, tx kv.RwTx, cfg BodiesCfg, ctx context.Context) (err error) {
u.UnwindPoint = max(u.UnwindPoint, cfg.blockReader.FrozenBlocks()) // protect from unwind behind files
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
if err := cfg.blockWriter.MakeBodiesNonCanonical(tx, u.UnwindPoint+1); err != nil {
return err
}
if err = u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func PruneBodiesStage(p *PruneState, tx kv.RwTx, cfg BodiesCfg, ctx context.Context) (err error) {
if cfg.blobStore != nil {
return cfg.blobStore.Prune(p.ForwardProgress)
}
return nil
}