forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_call_traces.go
512 lines (466 loc) · 13.8 KB
/
stage_call_traces.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
package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"runtime"
"time"
"github.com/RoaringBitmap/roaring/roaring64"
"github.com/c2h5oh/datasize"
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/common/length"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/params"
)
type CallTracesCfg struct {
db kv.RwDB
prune prune.Mode
ToBlock uint64 // not setting this params means no limit
tmpdir string
}
func StageCallTracesCfg(
db kv.RwDB,
prune prune.Mode,
toBlock uint64,
tmpdir string,
) CallTracesCfg {
return CallTracesCfg{
db: db,
prune: prune,
ToBlock: toBlock,
tmpdir: tmpdir,
}
}
func SpawnCallTraces(s *StageState, tx kv.RwTx, cfg CallTracesCfg, ctx context.Context, 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()
}
quit := ctx.Done()
endBlock, err := s.ExecutionAt(tx)
if cfg.ToBlock > 0 && cfg.ToBlock < endBlock {
endBlock = cfg.ToBlock
}
logPrefix := s.LogPrefix()
if err != nil {
return fmt.Errorf("getting last executed block: %w", err)
}
if endBlock == s.BlockNumber {
return nil
}
if err := promoteCallTraces(logPrefix, tx, s.BlockNumber+1, endBlock, bitmapsBufLimit, bitmapsFlushEvery, quit, cfg.tmpdir, logger); err != nil {
return err
}
if err := s.Update(tx, endBlock); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func promoteCallTraces(logPrefix string, tx kv.RwTx, startBlock, endBlock uint64, bufLimit datasize.ByteSize, flushEvery time.Duration, quit <-chan struct{}, tmpdir string, logger log.Logger) error {
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
froms := map[string]*roaring64.Bitmap{}
tos := map[string]*roaring64.Bitmap{}
collectorFrom := etl.NewCollector(logPrefix, tmpdir, etl.NewSortableBuffer(etl.BufferOptimalSize), logger)
defer collectorFrom.Close()
collectorTo := etl.NewCollector(logPrefix, tmpdir, etl.NewSortableBuffer(etl.BufferOptimalSize), logger)
defer collectorTo.Close()
checkFlushEvery := time.NewTicker(flushEvery)
defer checkFlushEvery.Stop()
traceCursor, err := tx.RwCursorDupSort(kv.CallTraceSet)
if err != nil {
return fmt.Errorf("failed to create cursor: %w", err)
}
defer traceCursor.Close()
var k, v []byte
prev := startBlock
for k, v, err = traceCursor.Seek(hexutility.EncodeTs(startBlock)); k != nil; k, v, err = traceCursor.Next() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k)
if blockNum > endBlock {
break
}
if len(v) != length.Addr+1 {
return fmt.Errorf(" wrong size of value in CallTraceSet: %x (size %d)", v, len(v))
}
mapKey := string(v[:length.Addr])
if v[length.Addr]&1 > 0 {
m, ok := froms[mapKey]
if !ok {
m = roaring64.New()
froms[mapKey] = m
}
m.Add(blockNum)
}
if v[length.Addr]&2 > 0 {
m, ok := tos[mapKey]
if !ok {
m = roaring64.New()
tos[mapKey] = m
}
m.Add(blockNum)
}
select {
default:
case <-logEvery.C:
var m runtime.MemStats
dbg.ReadMemStats(&m)
speed := float64(blockNum-prev) / float64(logInterval/time.Second)
prev = blockNum
logger.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockNum,
"blk/second", speed,
"alloc", libcommon.ByteCount(m.Alloc),
"sys", libcommon.ByteCount(m.Sys))
case <-checkFlushEvery.C:
if needFlush64(froms, bufLimit) {
if err := flushBitmaps64(collectorFrom, froms); err != nil {
return err
}
froms = map[string]*roaring64.Bitmap{}
}
if needFlush64(tos, bufLimit) {
if err := flushBitmaps64(collectorTo, tos); err != nil {
return err
}
tos = map[string]*roaring64.Bitmap{}
}
}
}
if err = flushBitmaps64(collectorFrom, froms); err != nil {
return err
}
if err = flushBitmaps64(collectorTo, tos); err != nil {
return err
}
// Clean up before loading call traces to reclaim space
var prunedMin uint64 = math.MaxUint64
var prunedMax uint64 = 0
for k, _, err = traceCursor.First(); k != nil; k, _, err = traceCursor.NextNoDup() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k)
if blockNum+params.FullImmutabilityThreshold >= endBlock {
break
}
select {
default:
case <-logEvery.C:
var m runtime.MemStats
dbg.ReadMemStats(&m)
logger.Info(fmt.Sprintf("[%s] Pruning call trace table", logPrefix), "number", blockNum,
"alloc", libcommon.ByteCount(m.Alloc), "sys", libcommon.ByteCount(m.Sys))
}
if err = tx.Delete(kv.CallTraceSet, k); err != nil {
return fmt.Errorf("remove trace call set for block %d: %w", blockNum, err)
}
if blockNum < prunedMin {
prunedMin = blockNum
}
if blockNum > prunedMax {
prunedMax = blockNum
}
}
if prunedMax != 0 && prunedMax > prunedMin+16 {
logger.Info(fmt.Sprintf("[%s] Pruned call trace intermediate table", logPrefix), "from", prunedMin, "to", prunedMax)
}
if err := finaliseCallTraces(collectorFrom, collectorTo, logPrefix, tx, quit); err != nil {
return err
}
return nil
}
func finaliseCallTraces(collectorFrom, collectorTo *etl.Collector, logPrefix string, tx kv.RwTx, quit <-chan struct{}) error {
var buf = bytes.NewBuffer(nil)
lastChunkKey := make([]byte, 128)
reader := bytes.NewReader(nil)
reader2 := bytes.NewReader(nil)
var loaderFunc = func(k []byte, v []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
reader.Reset(v)
currentBitmap := roaring64.New()
if _, err := currentBitmap.ReadFrom(reader); err != nil {
return err
}
lastChunkKey = lastChunkKey[:len(k)+8]
copy(lastChunkKey, k)
binary.BigEndian.PutUint64(lastChunkKey[len(k):], ^uint64(0))
lastChunkBytes, err := table.Get(lastChunkKey)
if err != nil {
return fmt.Errorf("find last chunk failed: %w", err)
}
if len(lastChunkBytes) > 0 {
lastChunk := roaring64.New()
reader2.Reset(lastChunkBytes)
_, err = lastChunk.ReadFrom(reader2)
if err != nil {
return fmt.Errorf("couldn't read last log index chunk: %w, len(lastChunkBytes)=%d", err, len(lastChunkBytes))
}
currentBitmap.Or(lastChunk) // merge last existing chunk from db - next loop will overwrite it
}
if err := bitmapdb.WalkChunkWithKeys64(k, currentBitmap, bitmapdb.ChunkLimit, func(chunkKey []byte, chunk *roaring64.Bitmap) error {
buf.Reset()
if _, err := chunk.WriteTo(buf); err != nil {
return err
}
return next(k, chunkKey, buf.Bytes())
}); err != nil {
return err
}
return nil
}
if err := collectorFrom.Load(tx, kv.CallFromIndex, loaderFunc, etl.TransformArgs{Quit: quit}); err != nil {
return err
}
if err := collectorTo.Load(tx, kv.CallToIndex, loaderFunc, etl.TransformArgs{Quit: quit}); err != nil {
return err
}
return nil
}
func UnwindCallTraces(u *UnwindState, s *StageState, tx kv.RwTx, cfg CallTracesCfg, ctx context.Context, logger log.Logger) (err error) {
if s.BlockNumber <= u.UnwindPoint {
return nil
}
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
logPrefix := u.LogPrefix()
if s.BlockNumber-u.UnwindPoint > 16 {
logger.Info(fmt.Sprintf("[%s] Unwind", logPrefix), "from", s.BlockNumber, "to", u.UnwindPoint)
}
if err := DoUnwindCallTraces(logPrefix, tx, s.BlockNumber, u.UnwindPoint, ctx, cfg.tmpdir, logger); 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 DoUnwindCallTraces(logPrefix string, db kv.RwTx, from, to uint64, ctx context.Context, tmpdir string, logger log.Logger) error {
froms := etl.NewCollector(logPrefix, tmpdir, etl.NewOldestEntryBuffer(etl.BufferOptimalSize), logger)
defer froms.Close()
tos := etl.NewCollector(logPrefix, tmpdir, etl.NewOldestEntryBuffer(etl.BufferOptimalSize), logger)
defer tos.Close()
logEvery := time.NewTicker(30 * time.Second)
defer logEvery.Stop()
traceCursor, err := db.RwCursorDupSort(kv.CallTraceSet)
if err != nil {
return fmt.Errorf("create cursor for call traces: %w", err)
}
defer traceCursor.Close()
var k, v []byte
prev := to + 1
for k, v, err = traceCursor.Seek(hexutility.EncodeTs(to + 1)); k != nil; k, v, err = traceCursor.Next() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k)
if blockNum >= from {
break
}
if len(v) != length.Addr+1 {
return fmt.Errorf("wrong size of value in CallTraceSet: %x (size %d)", v, len(v))
}
mapKey := v[:length.Addr]
if v[length.Addr]&1 > 0 {
if err = froms.Collect(mapKey, nil); err != nil {
return err
}
}
if v[length.Addr]&2 > 0 {
if err = tos.Collect(mapKey, nil); err != nil {
return err
}
}
select {
case <-logEvery.C:
var m runtime.MemStats
dbg.ReadMemStats(&m)
speed := float64(blockNum-prev) / float64(logInterval/time.Second)
prev = blockNum
logger.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockNum,
"blk/second", speed,
"alloc", libcommon.ByteCount(m.Alloc),
"sys", libcommon.ByteCount(m.Sys))
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
}
if err = froms.Load(db, "", func(k, v []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
return bitmapdb.TruncateRange64(db, kv.CallFromIndex, k, to+1)
}, etl.TransformArgs{}); err != nil {
return fmt.Errorf("TruncateRange: bucket=%s, %w", kv.CallFromIndex, err)
}
if err = tos.Load(db, "", func(k, v []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
return bitmapdb.TruncateRange64(db, kv.CallToIndex, k, to+1)
}, etl.TransformArgs{}); err != nil {
return fmt.Errorf("TruncateRange: bucket=%s, %w", kv.CallFromIndex, err)
}
return nil
}
func PruneCallTraces(s *PruneState, tx kv.RwTx, cfg CallTracesCfg, ctx context.Context, logger log.Logger) (err error) {
logPrefix := s.LogPrefix()
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
if cfg.prune.CallTraces.Enabled() {
if err = pruneCallTraces(tx, logPrefix, cfg.prune.CallTraces.PruneTo(s.ForwardProgress), ctx, cfg.tmpdir, logger); err != nil {
return err
}
}
if err := s.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func pruneCallTraces(tx kv.RwTx, logPrefix string, pruneTo uint64, ctx context.Context, tmpdir string, logger log.Logger) error {
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
froms := etl.NewCollector(logPrefix, tmpdir, etl.NewOldestEntryBuffer(etl.BufferOptimalSize), logger)
defer froms.Close()
tos := etl.NewCollector(logPrefix, tmpdir, etl.NewOldestEntryBuffer(etl.BufferOptimalSize), logger)
defer tos.Close()
{
traceCursor, err := tx.CursorDupSort(kv.CallTraceSet)
if err != nil {
return fmt.Errorf("create cursor for call traces: %w", err)
}
defer traceCursor.Close()
var k, v []byte
for k, v, err = traceCursor.First(); k != nil; k, v, err = traceCursor.Next() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k)
if blockNum >= pruneTo {
break
}
if len(v) != length.Addr+1 {
return fmt.Errorf("wrong size of value in CallTraceSet: %x (size %d)", v, len(v))
}
mapKey := v[:length.Addr]
if v[length.Addr]&1 > 0 {
if err := froms.Collect(mapKey, nil); err != nil {
return err
}
}
if v[length.Addr]&2 > 0 {
if err := tos.Collect(mapKey, nil); err != nil {
return err
}
}
select {
case <-logEvery.C:
var m runtime.MemStats
dbg.ReadMemStats(&m)
logger.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockNum, "alloc", libcommon.ByteCount(m.Alloc), "sys", libcommon.ByteCount(m.Sys))
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
}
}
{
c, err := tx.RwCursor(kv.CallFromIndex)
if err != nil {
return err
}
defer c.Close()
if err := froms.Load(tx, "", func(from, _ []byte, _ etl.CurrentTableReader, _ etl.LoadNextFunc) error {
for k, _, err := c.Seek(from); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k[length.Addr:])
if !bytes.HasPrefix(k, from) || blockNum >= pruneTo {
break
}
if err = c.DeleteCurrent(); err != nil {
return fmt.Errorf("failed delete, block=%d: %w", blockNum, err)
}
}
select {
case <-logEvery.C:
logger.Info(fmt.Sprintf("[%s]", logPrefix), "table", kv.CallFromIndex, "key", hex.EncodeToString(from))
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
return nil
}, etl.TransformArgs{}); err != nil {
return err
}
}
{
c, err := tx.RwCursor(kv.CallToIndex)
if err != nil {
return err
}
defer c.Close()
if err := tos.Load(tx, "", func(to, _ []byte, _ etl.CurrentTableReader, _ etl.LoadNextFunc) error {
for k, _, err := c.Seek(to); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k[length.Addr:])
if !bytes.HasPrefix(k, to) || blockNum >= pruneTo {
break
}
if err = c.DeleteCurrent(); err != nil {
return fmt.Errorf("failed delete, block=%d: %w", blockNum, err)
}
}
select {
case <-logEvery.C:
logger.Info(fmt.Sprintf("[%s]", logPrefix), "table", kv.CallToIndex, "key", hex.EncodeToString(to))
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
return nil
}, etl.TransformArgs{}); err != nil {
return err
}
}
return nil
}