-
Notifications
You must be signed in to change notification settings - Fork 1
/
stage_bodies.go
351 lines (320 loc) · 11 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
package stagedsync
import (
"context"
"fmt"
"runtime"
"time"
"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/kv"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/rawdb/blockio"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/dataflow"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/turbo/adapter"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/erigon/turbo/stages/bodydownload"
"github.com/ledgerwatch/erigon/turbo/stages/headerdownload"
)
const requestLoopCutOff int = 1
type BodiesCfg struct {
db kv.RwDB
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
historyV3 bool
}
func StageBodiesCfg(db kv.RwDB, 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,
historyV3 bool,
blockWriter *blockio.BlockWriter) BodiesCfg {
return BodiesCfg{db: db, bd: bd, bodyReqSend: bodyReqSend, penalise: penalise, blockPropagator: blockPropagator, timeout: timeout, chanConfig: chanConfig, blockReader: blockReader, historyV3: historyV3, 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, // Set to true in tests, allows the stage to fail rather than wait indefinitely
firstCycle bool,
logger log.Logger,
) error {
var doUpdate bool
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
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
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}
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
}
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
}
blockHeight := header.Number.Uint64()
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)
u.UnwindTo(blockHeight-1, BadBlock(header.Hash(), fmt.Errorf("Uncle verification failed: %w", err)))
return true, nil
}
// 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 cfg.historyV3 && ok {
if err := rawdb.AppendCanonicalTxNums(tx, blockHeight); err != nil {
return false, err
}
}
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
}
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 {
logger.Info(fmt.Sprintf("[%s] Processed", logPrefix), "highest", bodyProgress)
}
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)
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,
"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
logger.Info(fmt.Sprintf("[%s] Writing block 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) {
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
}