forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_execute.go
917 lines (818 loc) · 27.4 KB
/
stage_execute.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
package stagedsync
import (
"context"
"errors"
"fmt"
"os"
"runtime"
"time"
"github.com/c2h5oh/datasize"
"golang.org/x/sync/errgroup"
"github.com/ledgerwatch/erigon-lib/log/v3"
"github.com/ledgerwatch/erigon/core/rawdb/rawdbhelpers"
"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/common/metrics"
"github.com/ledgerwatch/erigon-lib/config3"
"github.com/ledgerwatch/erigon-lib/diagnostics"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/dbutils"
"github.com/ledgerwatch/erigon-lib/kv/membatch"
"github.com/ledgerwatch/erigon-lib/kv/membatchwithdb"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv/temporal"
libstate "github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/erigon-lib/wrap"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/eth/calltracer"
"github.com/ledgerwatch/erigon/eth/consensuschain"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
tracelogger "github.com/ledgerwatch/erigon/eth/tracers/logger"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/erigon/turbo/shards"
"github.com/ledgerwatch/erigon/turbo/silkworm"
)
const (
logInterval = 30 * time.Second
// stateStreamLimit - don't accumulate state changes if jump is bigger than this amount of blocks
stateStreamLimit uint64 = 1_000
)
type HasChangeSetWriter interface {
ChangeSetWriter() *state.ChangeSetWriter
}
type ChangeSetHook func(blockNum uint64, wr *state.ChangeSetWriter)
type headerDownloader interface {
ReportBadHeaderPoS(badHeader, lastValidAncestor common.Hash)
}
type ExecuteBlockCfg struct {
db kv.RwDB
batchSize datasize.ByteSize
prune prune.Mode
changeSetHook ChangeSetHook
chainConfig *chain.Config
engine consensus.Engine
vmConfig *vm.Config
badBlockHalt bool
stateStream bool
accumulator *shards.Accumulator
blockReader services.FullBlockReader
hd headerDownloader
// last valid number of the stage
dirs datadir.Dirs
historyV3 bool
syncCfg ethconfig.Sync
genesis *types.Genesis
agg *libstate.Aggregator
silkworm *silkworm.Silkworm
blockProduction bool
}
func StageExecuteBlocksCfg(
db kv.RwDB,
pm prune.Mode,
batchSize datasize.ByteSize,
changeSetHook ChangeSetHook,
chainConfig *chain.Config,
engine consensus.Engine,
vmConfig *vm.Config,
accumulator *shards.Accumulator,
stateStream bool,
badBlockHalt bool,
dirs datadir.Dirs,
blockReader services.FullBlockReader,
hd headerDownloader,
genesis *types.Genesis,
syncCfg ethconfig.Sync,
agg *libstate.Aggregator,
silkworm *silkworm.Silkworm,
) ExecuteBlockCfg {
return ExecuteBlockCfg{
db: db,
prune: pm,
batchSize: batchSize,
changeSetHook: changeSetHook,
chainConfig: chainConfig,
engine: engine,
vmConfig: vmConfig,
dirs: dirs,
accumulator: accumulator,
stateStream: stateStream,
badBlockHalt: badBlockHalt,
blockReader: blockReader,
hd: hd,
genesis: genesis,
historyV3: true,
syncCfg: syncCfg,
agg: agg,
silkworm: silkworm,
}
}
func executeBlock(
block *types.Block,
tx kv.RwTx,
batch kv.StatelessRwTx,
cfg ExecuteBlockCfg,
vmConfig vm.Config, // emit copy, because will modify it
writeChangesets bool,
writeReceipts bool,
writeCallTraces bool,
stateStream bool,
logger log.Logger,
) (err error) {
blockNum := block.NumberU64()
stateReader, stateWriter, err := newStateReaderWriter(batch, tx, block, writeChangesets, cfg.accumulator, cfg.blockReader, stateStream)
if err != nil {
return err
}
// where the magic happens
getHeader := func(hash common.Hash, number uint64) *types.Header {
h, _ := cfg.blockReader.Header(context.Background(), tx, hash, number)
return h
}
getTracer := func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) {
return tracelogger.NewStructLogger(&tracelogger.LogConfig{}), nil
}
callTracer := calltracer.NewCallTracer()
vmConfig.Debug = true
vmConfig.Tracer = callTracer
var receipts types.Receipts
var stateSyncReceipt *types.Receipt
var execRs *core.EphemeralExecResult
getHashFn := core.GetHashFn(block.Header(), getHeader)
execRs, err = core.ExecuteBlockEphemerally(cfg.chainConfig, &vmConfig, getHashFn, cfg.engine, block, stateReader, stateWriter, consensuschain.NewReader(cfg.chainConfig, tx, cfg.blockReader, logger), getTracer, logger)
if err != nil {
return fmt.Errorf("%w: %v", consensus.ErrInvalidBlock, err)
}
receipts = execRs.Receipts
stateSyncReceipt = execRs.StateSyncReceipt
// If writeReceipts is false here, append the not to be pruned receipts anyways
if writeReceipts || gatherNoPruneReceipts(&receipts, cfg.chainConfig) {
if err = rawdb.AppendReceipts(tx, blockNum, receipts); err != nil {
return err
}
if stateSyncReceipt != nil && stateSyncReceipt.Status == types.ReceiptStatusSuccessful {
if err := rawdb.WriteBorReceipt(tx, block.NumberU64(), stateSyncReceipt); err != nil {
return err
}
}
}
if cfg.changeSetHook != nil {
if hasChangeSet, ok := stateWriter.(HasChangeSetWriter); ok {
cfg.changeSetHook(blockNum, hasChangeSet.ChangeSetWriter())
}
}
if writeCallTraces {
return callTracer.WriteToDb(tx, block, *cfg.vmConfig)
}
return nil
}
// Filters out and keeps receipts of the contracts that may be needed by CL, namely of the deposit contract.
func gatherNoPruneReceipts(receipts *types.Receipts, chainCfg *chain.Config) bool {
cr := types.Receipts{}
for _, r := range *receipts {
toStore := false
if chainCfg.DepositContract == r.ContractAddress {
toStore = true
} else {
for _, l := range r.Logs {
if chainCfg.DepositContract == l.Address {
toStore = true
break
}
}
}
if toStore {
cr = append(cr, r)
}
}
receipts = &cr
return receipts.Len() > 0
}
func newStateReaderWriter(
batch kv.StatelessRwTx,
tx kv.RwTx,
block *types.Block,
writeChangesets bool,
accumulator *shards.Accumulator,
br services.FullBlockReader,
stateStream bool,
) (state.StateReader, state.WriterWithChangeSets, error) {
var stateReader state.StateReader
var stateWriter state.WriterWithChangeSets
stateReader = state.NewPlainStateReader(batch)
if stateStream {
txs, err := br.RawTransactions(context.Background(), tx, block.NumberU64(), block.NumberU64())
if err != nil {
return nil, nil, err
}
accumulator.StartChange(block.NumberU64(), block.Hash(), txs, false)
} else {
accumulator = nil
}
if writeChangesets {
stateWriter = state.NewPlainStateWriter(batch, tx, block.NumberU64()).SetAccumulator(accumulator)
} else {
stateWriter = state.NewPlainStateWriterNoHistory(batch).SetAccumulator(accumulator)
}
return stateReader, stateWriter, nil
}
// ================ Erigon3 ================
func ExecBlockV3(s *StageState, u Unwinder, txc wrap.TxContainer, toBlock uint64, ctx context.Context, cfg ExecuteBlockCfg, initialCycle bool, logger log.Logger) (err error) {
workersCount := cfg.syncCfg.ExecWorkerCount
if !initialCycle {
workersCount = 1
}
//if initialCycle {
// reconstituteToBlock, found, err := reconstituteBlock(cfg.agg, cfg.db, txc.Tx)
// if err != nil {
// return err
// }
//
// if found && reconstituteToBlock > s.BlockNumber+1 {
// reconWorkers := cfg.syncCfg.ReconWorkerCount
// if err := ReconstituteState(ctx, s, cfg.dirs, reconWorkers, cfg.batchSize, cfg.db, cfg.blockReader, log.New(), cfg.agg, cfg.engine, cfg.chainConfig, cfg.genesis); err != nil {
// return err
// }
// if dbg.StopAfterReconst() {
// os.Exit(1)
// }
// }
//}
prevStageProgress, err := senderStageProgress(txc.Tx, cfg.db)
if err != nil {
return err
}
var to = prevStageProgress
if toBlock > 0 {
to = min(prevStageProgress, toBlock)
}
if to < s.BlockNumber {
return nil
}
parallel := txc.Tx == nil
if err := ExecV3(ctx, s, u, workersCount, cfg, txc, parallel, to, logger, initialCycle); err != nil {
return fmt.Errorf("ExecV3: %w", err)
}
return nil
}
// reconstituteBlock - First block which is not covered by the history snapshot files
func reconstituteBlock(agg *libstate.Aggregator, db kv.RoDB, tx kv.Tx) (n uint64, ok bool, err error) {
sendersProgress, err := senderStageProgress(tx, db)
if err != nil {
return 0, false, err
}
reconToBlock := min(sendersProgress, agg.EndTxNumDomainsFrozen())
if tx == nil {
if err = db.View(context.Background(), func(tx kv.Tx) error {
ok, n, err = rawdbv3.TxNums.FindBlockNum(tx, reconToBlock)
return err
}); err != nil {
return
}
} else {
ok, n, err = rawdbv3.TxNums.FindBlockNum(tx, reconToBlock)
}
return
}
var ErrTooDeepUnwind = fmt.Errorf("too deep unwind")
func unwindExec3(u *UnwindState, s *StageState, txc wrap.TxContainer, ctx context.Context, accumulator *shards.Accumulator, logger log.Logger) (err error) {
fmt.Printf("unwindv3: %d -> %d\n", u.CurrentBlockNumber, u.UnwindPoint)
//txTo, err := rawdbv3.TxNums.Min(tx, u.UnwindPoint+1)
//if err != nil {
// return err
//}
//bn, _, ok, err := domains.SeekCommitment2(tx, 0, txTo)
//if ok && bn != u.UnwindPoint {
// return fmt.Errorf("commitment can unwind only to block: %d, requested: %d. UnwindTo was called with wrong value", bn, u.UnwindPoint)
//}
start := time.Now()
unwindToLimit, err := txc.Tx.(libstate.HasAggTx).AggTx().(*libstate.AggregatorRoTx).CanUnwindDomainsToBlockNum(txc.Tx)
if err != nil {
return err
}
if u.UnwindPoint < unwindToLimit {
return fmt.Errorf("%w: %d < %d", ErrTooDeepUnwind, u.UnwindPoint, unwindToLimit)
}
var domains *libstate.SharedDomains
if txc.Doms == nil {
domains, err = libstate.NewSharedDomains(txc.Tx, logger)
if err != nil {
return err
}
defer domains.Close()
} else {
domains = txc.Doms
}
rs := state.NewStateV3(domains, logger)
// unwind all txs of u.UnwindPoint block. 1 txn in begin/end of block - system txs
txNum, err := rawdbv3.TxNums.Min(txc.Tx, u.UnwindPoint+1)
if err != nil {
return err
}
t := time.Now()
var changeset *[kv.DomainLen][]libstate.DomainEntryDiff
for currentBlock := u.CurrentBlockNumber; currentBlock > u.UnwindPoint; currentBlock-- {
currentHash, err := rawdb.ReadCanonicalHash(txc.Tx, currentBlock)
if err != nil {
return err
}
var ok bool
var currentKeys [kv.DomainLen][]libstate.DomainEntryDiff
currentKeys, ok, err = domains.GetDiffset(txc.Tx, currentHash, currentBlock)
if !ok {
return fmt.Errorf("domains.GetDiffset(%d, %s): not found", currentBlock, currentHash)
}
if err != nil {
return err
}
if changeset == nil {
changeset = ¤tKeys
} else {
for i := range currentKeys {
changeset[i] = libstate.MergeDiffSets(changeset[i], currentKeys[i])
}
}
}
if err := rs.Unwind(ctx, txc.Tx, u.UnwindPoint, txNum, accumulator, changeset); err != nil {
return fmt.Errorf("StateV3.Unwind(%d->%d): %w, took %s", s.BlockNumber, u.UnwindPoint, err, time.Since(t))
}
if err := rawdb.TruncateReceipts(txc.Tx, u.UnwindPoint+1); err != nil {
return fmt.Errorf("truncate receipts: %w", err)
}
if err := rawdb.TruncateBorReceipts(txc.Tx, u.UnwindPoint+1); err != nil {
return fmt.Errorf("truncate bor receipts: %w", err)
}
if err := rawdb.DeleteNewerEpochs(txc.Tx, u.UnwindPoint+1); err != nil {
return fmt.Errorf("delete newer epochs: %w", err)
}
fmt.Printf("unwindv3: %d -> %d done within %s\n", s.BlockNumber, u.UnwindPoint, time.Since(start))
return nil
}
func senderStageProgress(tx kv.Tx, db kv.RoDB) (prevStageProgress uint64, err error) {
if tx != nil {
prevStageProgress, err = stages.GetStageProgress(tx, stages.Senders)
if err != nil {
return prevStageProgress, err
}
} else {
if err = db.View(context.Background(), func(tx kv.Tx) error {
prevStageProgress, err = stages.GetStageProgress(tx, stages.Senders)
if err != nil {
return err
}
return nil
}); err != nil {
return prevStageProgress, err
}
}
return prevStageProgress, nil
}
// ================ Erigon3 End ================
func SpawnExecuteBlocksStage(s *StageState, u Unwinder, txc wrap.TxContainer, toBlock uint64, ctx context.Context, cfg ExecuteBlockCfg, logger log.Logger) (err error) {
if dbg.StagesOnlyBlocks {
return nil
}
if cfg.historyV3 {
if err = ExecBlockV3(s, u, txc, toBlock, ctx, cfg, s.CurrentSyncCycle.IsInitialCycle, logger); err != nil {
return err
}
return nil
}
if config3.EnableHistoryV4InTest {
panic("must use ExecBlockV3")
}
quit := ctx.Done()
useExternalTx := txc.Tx != nil
if !useExternalTx {
txc.Tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer txc.Tx.Rollback()
}
prevStageProgress, errStart := stages.GetStageProgress(txc.Tx, stages.Senders)
if errStart != nil {
return errStart
}
nextStageProgress, err := stages.GetStageProgress(txc.Tx, stages.HashState)
if err != nil {
return err
}
nextStagesExpectData := nextStageProgress > 0 // Incremental move of next stages depend on fully written ChangeSets, Receipts, CallTraceSet
logPrefix := s.LogPrefix()
var to = prevStageProgress
if toBlock > 0 {
to = min(prevStageProgress, toBlock)
}
if cfg.syncCfg.LoopBlockLimit > 0 {
to = s.BlockNumber + uint64(cfg.syncCfg.LoopBlockLimit)
}
if to <= s.BlockNumber {
return nil
}
if to > s.BlockNumber+16 {
logger.Info(fmt.Sprintf("[%s] Blocks execution", logPrefix), "from", s.BlockNumber, "to", to)
}
stateStream := cfg.stateStream && to-s.BlockNumber < stateStreamLimit
// changes are stored through memory buffer
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
stageProgress := s.BlockNumber
logBlock := stageProgress
logTx, lastLogTx := uint64(0), uint64(0)
logTime := time.Now()
startTime := time.Now()
var gas uint64 // used for logs
var currentStateGas uint64 // used for batch commits of state
var stoppedErr error
// Transform batch_size limit into Ggas
gasState := uint64(cfg.batchSize) * uint64(datasize.KB) * 2
//var batch kv.PendingMutations
// state is stored through ethdb batches
batch := membatch.NewHashBatch(txc.Tx, quit, cfg.dirs.Tmp, logger)
// avoids stacking defers within the loop
defer func() {
batch.Close()
}()
var readAhead chan uint64
if s.CurrentSyncCycle.IsInitialCycle && cfg.silkworm == nil { // block read-ahead is not compatible w/ Silkworm one-shot block execution
// snapshots are often stored on cheaper drives. don't expect low-read-latency and manually read-ahead.
// can't use OS-level ReadAhead - because Data >> RAM
// it also warmsup state a bit - by touching senders/coninbase accounts and code
var clean func()
readAhead, clean = blocksReadAhead(ctx, &cfg, 4, cfg.engine, false)
defer clean()
}
//fmt.Printf("exec blocks: %d -> %d\n", stageProgress+1, to)
Loop:
for blockNum := stageProgress + 1; blockNum <= to; blockNum++ {
if stoppedErr = common.Stopped(quit); stoppedErr != nil {
log.Warn("Execution interrupted", "err", stoppedErr)
break
}
if s.CurrentSyncCycle.IsInitialCycle && cfg.silkworm == nil { // block read-ahead is not compatible w/ Silkworm one-shot block execution
select {
case readAhead <- blockNum:
default:
}
}
blockHash, err := cfg.blockReader.CanonicalHash(ctx, txc.Tx, blockNum)
if err != nil {
return err
}
block, _, err := cfg.blockReader.BlockWithSenders(ctx, txc.Tx, blockHash, blockNum)
if err != nil {
return err
}
if block == nil {
logger.Error(fmt.Sprintf("[%s] Empty block", logPrefix), "blocknum", blockNum)
break
}
lastLogTx += uint64(block.Transactions().Len())
// Incremental move of next stages depend on fully written ChangeSets, Receipts, CallTraceSet
writeChangeSets := nextStagesExpectData || blockNum > cfg.prune.History.PruneTo(to)
writeReceipts := nextStagesExpectData || blockNum > cfg.prune.Receipts.PruneTo(to)
writeCallTraces := nextStagesExpectData || blockNum > cfg.prune.CallTraces.PruneTo(to)
metrics.UpdateBlockConsumerPreExecutionDelay(block.Time(), blockNum, logger)
_, isMemoryMutation := txc.Tx.(*membatchwithdb.MemoryMutation)
if cfg.silkworm != nil && !isMemoryMutation {
if useExternalTx {
blockNum, err = silkworm.ExecuteBlocksEphemeral(cfg.silkworm, txc.Tx, cfg.chainConfig.ChainID, blockNum, to, uint64(cfg.batchSize), writeChangeSets, writeReceipts, writeCallTraces)
} else {
// In case of internal tx we close it (no changes, commit not needed): Silkworm will use its own internal tx
txc.Tx.Rollback()
txc.Tx = nil
log.Info("Using Silkworm to commit full range", "fromBlock", s.BlockNumber+1, "toBlock", to)
blockNum, err = silkworm.ExecuteBlocksPerpetual(cfg.silkworm, cfg.db, cfg.chainConfig.ChainID, blockNum, to, uint64(cfg.batchSize), writeChangeSets, writeReceipts, writeCallTraces)
var txErr error
if txc.Tx, txErr = cfg.db.BeginRw(context.Background()); txErr != nil {
return txErr
}
defer txc.Tx.Rollback()
// Recreate memory batch because underlying tx has changed
batch.Close()
batch = membatch.NewHashBatch(txc.Tx, quit, cfg.dirs.Tmp, logger)
}
// In case of any error we need to increment to have the failed block number
if err != nil {
blockNum++
}
} else {
err = executeBlock(block, txc.Tx, batch, cfg, *cfg.vmConfig, writeChangeSets, writeReceipts, writeCallTraces, stateStream, logger)
}
if err != nil {
if errors.Is(err, silkworm.ErrInterrupted) {
logger.Warn(fmt.Sprintf("[%s] Execution interrupted", logPrefix), "block", blockNum, "err", err)
// Remount the termination signal
p, err := os.FindProcess(os.Getpid())
if err != nil {
return err
}
p.Signal(os.Interrupt)
return nil
}
if !errors.Is(err, context.Canceled) {
if cfg.silkworm != nil {
logger.Warn(fmt.Sprintf("[%s] Execution failed", logPrefix), "block", blockNum, "err", err)
} else {
logger.Warn(fmt.Sprintf("[%s] Execution failed", logPrefix), "block", blockNum, "hash", blockHash.String(), "err", err)
}
if cfg.hd != nil && errors.Is(err, consensus.ErrInvalidBlock) {
cfg.hd.ReportBadHeaderPoS(blockHash, block.ParentHash() /* lastValidAncestor */)
}
if cfg.badBlockHalt {
return err
}
}
if errors.Is(err, consensus.ErrInvalidBlock) {
if err := u.UnwindTo(blockNum-1, BadBlock(blockHash, err), txc.Tx); err != nil {
return err
}
} else {
if err := u.UnwindTo(blockNum-1, ExecUnwind, txc.Tx); err != nil {
return err
}
}
break Loop
}
stageProgress = blockNum
metrics.UpdateBlockConsumerPostExecutionDelay(block.Time(), blockNum, logger)
shouldUpdateProgress := batch.BatchSize() >= int(cfg.batchSize)
if shouldUpdateProgress {
commitTime := time.Now()
if err = batch.Flush(ctx, txc.Tx); err != nil {
return err
}
if err = s.Update(txc.Tx, stageProgress); err != nil {
return err
}
if !useExternalTx {
if err = txc.Tx.Commit(); err != nil {
return err
}
txc.Tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
// TODO: This creates stacked up deferrals
defer txc.Tx.Rollback()
}
logger.Info("Committed State", "gas reached", currentStateGas, "gasTarget", gasState, "block", blockNum, "time", time.Since(commitTime), "committedToDb", !useExternalTx)
currentStateGas = 0
batch = membatch.NewHashBatch(txc.Tx, quit, cfg.dirs.Tmp, logger)
}
gas = gas + block.GasUsed()
currentStateGas = currentStateGas + block.GasUsed()
select {
default:
case <-logEvery.C:
logBlock, logTx, logTime = logProgress(logPrefix, logBlock, logTime, blockNum, logTx, lastLogTx, gas, float64(currentStateGas)/float64(gasState), batch, logger, s.BlockNumber, to, startTime)
gas = 0
txc.Tx.CollectMetrics()
stages.SyncMetrics[stages.Execution].SetUint64(blockNum)
}
}
if err = s.Update(txc.Tx, stageProgress); err != nil {
return err
}
if err = batch.Flush(ctx, txc.Tx); err != nil {
return fmt.Errorf("batch commit: %w", err)
}
_, err = rawdb.IncrementStateVersion(txc.Tx)
if err != nil {
return fmt.Errorf("writing plain state version: %w", err)
}
//dumpPlainStateDebug(tx, nil)
if !useExternalTx {
if err = txc.Tx.Commit(); err != nil {
return err
}
}
logger.Info(fmt.Sprintf("[%s] Completed on", logPrefix), "block", stageProgress)
return stoppedErr
}
func blocksReadAhead(ctx context.Context, cfg *ExecuteBlockCfg, workers int, engine consensus.Engine, histV3 bool) (chan uint64, context.CancelFunc) {
const readAheadBlocks = 100
readAhead := make(chan uint64, readAheadBlocks)
g, gCtx := errgroup.WithContext(ctx)
for workerNum := 0; workerNum < workers; workerNum++ {
g.Go(func() (err error) {
var bn uint64
var ok bool
var tx kv.Tx
defer func() {
if tx != nil {
tx.Rollback()
}
}()
for i := 0; ; i++ {
select {
case bn, ok = <-readAhead:
if !ok {
return
}
case <-gCtx.Done():
return gCtx.Err()
}
if i%100 == 0 {
if tx != nil {
tx.Rollback()
}
tx, err = cfg.db.BeginRo(ctx)
if err != nil {
return err
}
}
if err := blocksReadAheadFunc(gCtx, tx, cfg, bn+readAheadBlocks, engine, histV3); err != nil {
return err
}
}
})
}
return readAhead, func() {
close(readAhead)
_ = g.Wait()
}
}
func blocksReadAheadFunc(ctx context.Context, tx kv.Tx, cfg *ExecuteBlockCfg, blockNum uint64, engine consensus.Engine, histV3 bool) error {
block, err := cfg.blockReader.BlockByNumber(ctx, tx, blockNum)
if err != nil {
return err
}
if block == nil {
return nil
}
_, _ = cfg.engine.Author(block.HeaderNoCopy()) // Bor consensus: this calc is heavy and has cache
if histV3 {
return nil
}
senders := block.Body().SendersFromTxs() //TODO: BlockByNumber can return senders
stateReader := state.NewPlainStateReader(tx) //TODO: can do on batch! if make batch thread-safe
for _, sender := range senders {
a, _ := stateReader.ReadAccountData(sender)
if a == nil || a.Incarnation == 0 {
continue
}
if code, _ := stateReader.ReadAccountCode(sender, a.Incarnation, a.CodeHash); len(code) > 0 {
_, _ = code[0], code[len(code)-1]
}
}
for _, txn := range block.Transactions() {
to := txn.GetTo()
if to == nil {
continue
}
a, _ := stateReader.ReadAccountData(*to)
if a == nil || a.Incarnation == 0 {
continue
}
if code, _ := stateReader.ReadAccountCode(*to, a.Incarnation, a.CodeHash); len(code) > 0 {
_, _ = code[0], code[len(code)-1]
}
}
_, _ = stateReader.ReadAccountData(block.Coinbase())
_, _ = block, senders
return nil
}
func logProgress(logPrefix string, prevBlock uint64, prevTime time.Time, currentBlock uint64, prevTx, currentTx uint64, gas uint64,
gasState float64, batch kv.PendingMutations, logger log.Logger, from uint64, to uint64, startTime time.Time) (uint64, uint64, time.Time) {
currentTime := time.Now()
interval := currentTime.Sub(prevTime)
speed := float64(currentBlock-prevBlock) / (float64(interval) / float64(time.Second))
speedTx := float64(currentTx-prevTx) / (float64(interval) / float64(time.Second))
speedMgas := float64(gas) / 1_000_000 / (float64(interval) / float64(time.Second))
var m runtime.MemStats
dbg.ReadMemStats(&m)
var logpairs = []interface{}{
"number", currentBlock,
"blk/s", fmt.Sprintf("%.1f", speed),
"tx/s", fmt.Sprintf("%.1f", speedTx),
"Mgas/s", fmt.Sprintf("%.1f", speedMgas),
"gasState", fmt.Sprintf("%.2f", gasState),
}
batchSize := 0
if batch != nil {
batchSize = batch.BatchSize()
logpairs = append(logpairs, "batch", common.ByteCount(uint64(batchSize)))
}
logpairs = append(logpairs, "alloc", common.ByteCount(m.Alloc), "sys", common.ByteCount(m.Sys))
diagnostics.Send(diagnostics.BlockExecutionStatistics{
From: from,
To: to,
BlockNumber: currentBlock,
BlkPerSec: speed,
TxPerSec: speedTx,
MgasPerSec: speedMgas,
GasState: gasState,
Batch: uint64(batchSize),
Alloc: m.Alloc,
Sys: m.Sys,
TimeElapsed: time.Since(startTime).Round(time.Second).Seconds(),
})
logger.Info(fmt.Sprintf("[%s] Executed blocks", logPrefix), logpairs...)
return currentBlock, currentTx, currentTime
}
func UnwindExecutionStage(u *UnwindState, s *StageState, txc wrap.TxContainer, ctx context.Context, cfg ExecuteBlockCfg, logger log.Logger) (err error) {
//fmt.Printf("unwind: %d -> %d\n", u.CurrentBlockNumber, u.UnwindPoint)
if u.UnwindPoint >= s.BlockNumber {
return nil
}
useExternalTx := txc.Tx != nil
if !useExternalTx {
txc.Tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer txc.Tx.Rollback()
}
logPrefix := u.LogPrefix()
logger.Info(fmt.Sprintf("[%s] Unwind Execution", logPrefix), "from", s.BlockNumber, "to", u.UnwindPoint)
if err = unwindExecutionStage(u, s, txc, ctx, cfg, logger); err != nil {
return err
}
if err = u.Done(txc.Tx); err != nil {
return err
}
//dumpPlainStateDebug(tx, nil)
if !useExternalTx {
if err = txc.Tx.Commit(); err != nil {
return err
}
}
return nil
}
func unwindExecutionStage(u *UnwindState, s *StageState, txc wrap.TxContainer, ctx context.Context, cfg ExecuteBlockCfg, logger log.Logger) error {
var accumulator *shards.Accumulator
if cfg.stateStream && s.BlockNumber-u.UnwindPoint < stateStreamLimit {
accumulator = cfg.accumulator
hash, err := cfg.blockReader.CanonicalHash(ctx, txc.Tx, u.UnwindPoint)
if err != nil {
return fmt.Errorf("read canonical hash of unwind point: %w", err)
}
txs, err := cfg.blockReader.RawTransactions(ctx, txc.Tx, u.UnwindPoint, s.BlockNumber)
if err != nil {
return err
}
accumulator.StartChange(u.UnwindPoint, hash, txs, true)
}
//TODO: why we don't call accumulator.ChangeCode???
return unwindExec3(u, s, txc, ctx, accumulator, logger)
}
func recoverCodeHashPlain(acc *accounts.Account, db kv.Tx, key []byte) {
var address common.Address
copy(address[:], key)
if acc.Incarnation > 0 && acc.IsEmptyCodeHash() {
if codeHash, err2 := db.GetOne(kv.PlainContractCode, dbutils.PlainGenerateStoragePrefix(address[:], acc.Incarnation)); err2 == nil {
copy(acc.CodeHash[:], codeHash)
}
}
}
func PruneExecutionStage(s *PruneState, tx kv.RwTx, cfg ExecuteBlockCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
if s.ForwardProgress > config3.MaxReorgDepthV3 {
// (chunkLen is 8Kb) * (1_000 chunks) = 8mb
// Some blocks on bor-mainnet have 400 chunks of diff = 3mb
var pruneDiffsLimitOnChainTip = 1_000
if s.CurrentSyncCycle.IsInitialCycle {
pruneDiffsLimitOnChainTip *= 10
}
if err := rawdb.PruneTable(tx, kv.ChangeSets3, s.ForwardProgress-config3.MaxReorgDepthV3, ctx, pruneDiffsLimitOnChainTip); err != nil {
return err
}
}
execStepsInDB.Set(rawdbhelpers.IdxStepsCountV3(tx) * 100)
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
pruneTimeout := 3 * time.Second
if s.CurrentSyncCycle.IsInitialCycle {
pruneTimeout = 12 * time.Hour
}
if _, err = tx.(*temporal.Tx).AggTx().(*libstate.AggregatorRoTx).PruneSmallBatches(ctx, pruneTimeout, tx); err != nil { // prune part of retired data, before commit
return err
}
if err = s.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}