-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathsyncer.go
More file actions
1234 lines (1057 loc) · 37.5 KB
/
syncer.go
File metadata and controls
1234 lines (1057 loc) · 37.5 KB
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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package syncing
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"math"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/celestiaorg/go-header"
"github.com/rs/zerolog"
"github.com/evstack/ev-node/block/internal/cache"
"github.com/evstack/ev-node/block/internal/common"
"github.com/evstack/ev-node/block/internal/da"
coreexecutor "github.com/evstack/ev-node/core/execution"
"github.com/evstack/ev-node/pkg/config"
"github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/pkg/raft"
"github.com/evstack/ev-node/pkg/store"
"github.com/evstack/ev-node/types"
)
var _ BlockSyncer = (*Syncer)(nil)
const (
// baseGracePeriodEpochs is the minimum grace window after an epoch ends.
// A tx from epoch N must appear by the end of epoch N+1 under normal conditions.
baseGracePeriodEpochs uint64 = 1
// maxGracePeriodEpochs caps the grace window even under sustained congestion.
maxGracePeriodEpochs uint64 = 4
// fullnessThreshold is the fraction of DefaultMaxBlobSize above which a block
// is considered full. Exceeding it extends the grace period for that epoch.
fullnessThreshold = 0.8
)
// Syncer handles block synchronization from DA and P2P sources.
type Syncer struct {
// Core components
store store.Store
exec coreexecutor.Executor
// Shared components
cache cache.CacheManager
metrics *common.Metrics
// Configuration
config config.Config
genesis genesis.Genesis
options common.BlockOptions
logger zerolog.Logger
// State management
lastState *atomic.Pointer[types.State]
// DA retriever
daClient da.Client
daRetrieverHeight *atomic.Uint64
// P2P stores
headerStore header.Store[*types.P2PSignedHeader]
dataStore header.Store[*types.P2PData]
// Channels for coordination
heightInCh chan common.DAHeightEvent
errorCh chan<- error // Channel to report critical execution client failures
inFlight atomic.Int64
// Handlers
daRetriever DARetriever
fiRetriever da.ForcedInclusionRetriever
p2pHandler p2pHandler
raftRetriever *raftRetriever
daFollower DAFollower
// Forced inclusion tracking
forcedInclusionMu sync.RWMutex
seenBlockTxs map[string]struct{} // SHA-256 hex of every tx seen in a DA-sourced block
seenBlockTxsByHeight map[uint64][]string // DA height → hashes at that height (for pruning)
daBlockBytes map[uint64]uint64 // DA height → total tx bytes (for congestion tracking)
lastCheckedEpochEnd uint64 // highest epochEnd fully verified so far
// Lifecycle
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
hasCriticalError atomic.Bool
// P2P wait coordination
p2pWaitState atomic.Value // stores p2pWaitState
// blockSyncer is the interface used for block sync operations.
// defaults to self, but can be wrapped with tracing.
blockSyncer BlockSyncer
}
// NewSyncer creates a new block syncer
func NewSyncer(
store store.Store,
exec coreexecutor.Executor,
daClient da.Client,
cache cache.Manager,
metrics *common.Metrics,
config config.Config,
genesis genesis.Genesis,
headerStore header.Store[*types.P2PSignedHeader],
dataStore header.Store[*types.P2PData],
logger zerolog.Logger,
options common.BlockOptions,
errorCh chan<- error,
raftNode common.RaftNode,
) *Syncer {
daRetrieverHeight := &atomic.Uint64{}
daRetrieverHeight.Store(genesis.DAStartHeight)
s := &Syncer{
store: store,
exec: exec,
cache: cache,
metrics: metrics,
config: config,
genesis: genesis,
options: options,
lastState: &atomic.Pointer[types.State]{},
daClient: daClient,
daRetrieverHeight: daRetrieverHeight,
headerStore: headerStore,
dataStore: dataStore,
heightInCh: make(chan common.DAHeightEvent, 100),
errorCh: errorCh,
logger: logger.With().Str("component", "syncer").Logger(),
seenBlockTxs: make(map[string]struct{}),
seenBlockTxsByHeight: make(map[uint64][]string),
daBlockBytes: make(map[uint64]uint64),
}
s.blockSyncer = s
if raftNode != nil && !reflect.ValueOf(raftNode).IsNil() {
s.raftRetriever = newRaftRetriever(raftNode, genesis, logger, s,
func(ctx context.Context, state *raft.RaftBlockState) error {
s.logger.Debug().Uint64("header_height", state.LastSubmittedDaHeaderHeight).Uint64("data_height", state.LastSubmittedDaDataHeight).Msg("received raft block state")
cache.SetLastSubmittedHeaderHeight(ctx, state.LastSubmittedDaHeaderHeight)
cache.SetLastSubmittedDataHeight(ctx, state.LastSubmittedDaDataHeight)
return nil
})
}
return s
}
// SetBlockSyncer sets the block syncer interface, allowing injection of
// a tracing wrapper or other decorator.
func (s *Syncer) SetBlockSyncer(bs BlockSyncer) {
s.blockSyncer = bs
}
// Start begins the syncing component
func (s *Syncer) Start(ctx context.Context) (err error) {
ctx, cancel := context.WithCancel(ctx)
s.ctx, s.cancel = ctx, cancel
defer func() { //nolint: contextcheck // use new context as parent can be cancelled already
if err != nil {
_ = s.Stop(context.Background())
}
}()
if err = s.initializeState(); err != nil {
return fmt.Errorf("failed to initialize syncer state: %w", err)
}
// Initialize handlers
s.daRetriever = NewDARetriever(s.daClient, s.cache, s.genesis, s.logger)
if s.config.Instrumentation.IsTracingEnabled() {
s.daRetriever = WithTracingDARetriever(s.daRetriever)
}
s.fiRetriever = da.NewForcedInclusionRetriever(s.daClient, s.logger, s.config.DA.BlockTime.Duration, s.config.Instrumentation.IsTracingEnabled(), s.genesis.DAStartHeight, s.genesis.DAEpochForcedInclusion)
s.fiRetriever.Start(ctx)
s.p2pHandler = NewP2PHandler(s.headerStore, s.dataStore, s.cache, s.genesis, s.logger)
currentHeight, initErr := s.store.Height(ctx)
if initErr != nil {
s.logger.Error().Err(initErr).Msg("failed to set initial processed height for p2p handler")
} else {
s.p2pHandler.SetProcessedHeight(currentHeight)
}
if s.raftRetriever != nil {
if err = s.raftRetriever.Start(ctx); err != nil {
return fmt.Errorf("start raft retriever: %w", err)
}
}
if !s.waitForGenesis() {
return nil
}
// Start main processing loop
s.wg.Go(func() { s.processLoop(ctx) })
// Start the DA follower (subscribe + catchup) and other workers
s.daFollower = NewDAFollower(DAFollowerConfig{
Client: s.daClient,
Retriever: s.daRetriever,
Logger: s.logger,
EventSink: s,
Namespace: s.daClient.GetHeaderNamespace(),
DataNamespace: s.daClient.GetDataNamespace(),
StartDAHeight: s.daRetrieverHeight.Load(),
DABlockTime: s.config.DA.BlockTime.Duration,
})
if err = s.daFollower.Start(ctx); err != nil {
return fmt.Errorf("failed to start DA follower: %w", err)
}
s.startSyncWorkers(ctx)
s.logger.Info().Msg("syncer started")
return nil
}
// Stop shuts down the syncing component
func (s *Syncer) Stop(ctx context.Context) error {
if s.cancel == nil {
return nil
}
s.cancel()
s.cancelP2PWait(0)
if s.fiRetriever != nil {
s.fiRetriever.Stop()
}
if s.daFollower != nil {
s.daFollower.Stop()
}
s.wg.Wait()
// Skip draining if we're shutting down due to a critical error (e.g. execution
// client unavailable).
if !s.hasCriticalError.Load() {
drainCtx, drainCancel := context.WithTimeout(ctx, 5*time.Second)
defer drainCancel()
drained := 0
drainLoop:
for {
select {
case event, ok := <-s.heightInCh:
if !ok {
break drainLoop
}
s.processHeightEvent(drainCtx, &event)
drained++
case <-drainCtx.Done():
s.logger.Warn().Int("remaining", len(s.heightInCh)).Msg("timeout draining height events during shutdown")
break drainLoop
default:
break drainLoop
}
}
if drained > 0 {
s.logger.Info().Int("count", drained).Msg("drained pending height events during shutdown")
}
}
s.logger.Info().Msg("syncer stopped")
close(s.heightInCh)
s.cancel = nil
return nil
}
// getLastState returns the current state
func (s *Syncer) getLastState() types.State {
state := s.lastState.Load()
if state == nil {
return types.State{}
}
return *state
}
// SetLastState updates the current state
func (s *Syncer) SetLastState(state types.State) {
s.lastState.Store(&state)
}
// initializeState loads the current sync state
func (s *Syncer) initializeState() error {
// Load state from store
state, err := s.store.GetState(s.ctx)
if err != nil {
// Initialize new chain state for a fresh full node (no prior state on disk)
// Mirror executor initialization to ensure AppHash matches headers produced by the sequencer.
stateRoot, initErr := s.exec.InitChain(
s.ctx,
s.genesis.StartTime,
s.genesis.InitialHeight,
s.genesis.ChainID,
)
if initErr != nil {
return fmt.Errorf("failed to initialize execution client: %w", initErr)
}
state = types.State{
ChainID: s.genesis.ChainID,
InitialHeight: s.genesis.InitialHeight,
LastBlockHeight: s.genesis.InitialHeight - 1,
LastBlockTime: s.genesis.StartTime,
DAHeight: s.genesis.DAStartHeight,
AppHash: stateRoot,
}
}
if state.DAHeight != 0 && state.DAHeight < s.genesis.DAStartHeight {
return fmt.Errorf("DA height (%d) is lower than DA start height (%d)", state.DAHeight, s.genesis.DAStartHeight)
}
// Persist the initialized state to the store
batch, err := s.store.NewBatch(s.ctx)
if err != nil {
return fmt.Errorf("failed to create batch: %w", err)
}
if err := batch.SetHeight(state.LastBlockHeight); err != nil {
return fmt.Errorf("failed to set store height: %w", err)
}
if err := batch.UpdateState(state); err != nil {
return fmt.Errorf("failed to update state: %w", err)
}
if err := batch.Commit(); err != nil {
return fmt.Errorf("failed to commit batch: %w", err)
}
s.SetLastState(state)
// Initialize lastCheckedEpochEnd based on the restored state's DA height so that
// VerifyForcedInclusionTxs resumes from where we left off instead of re-scanning
// all epochs from genesis on every startup.
if epochSize := s.genesis.DAEpochForcedInclusion; epochSize > 0 && state.DAHeight >= s.genesis.DAStartHeight {
firstEpochEnd := s.genesis.DAStartHeight + epochSize - 1
if state.DAHeight >= firstEpochEnd {
// The last completed epoch end that is fully behind state.DAHeight.
elapsed := state.DAHeight - firstEpochEnd
completedEpochs := elapsed / epochSize
s.lastCheckedEpochEnd = firstEpochEnd + completedEpochs*epochSize
}
}
// Set DA height to the maximum of the genesis start height, the state's DA height, and the cached DA height.
// The cache's DaHeight() is initialized from store metadata, so it's always correct even after cache clear.
// Only use cache.DaHeight() when P2P is actively syncing (headerStore has higher height than current state).
daHeight := s.genesis.DAStartHeight
if state.DAHeight > s.genesis.DAStartHeight {
daHeight = max(daHeight, state.DAHeight-1)
}
if s.headerStore != nil && s.headerStore.Height() > state.LastBlockHeight {
daHeight = max(daHeight, s.cache.DaHeight())
}
// dev mode for da start height
if startHeight := s.config.DA.StartHeight; startHeight > 0 {
s.logger.Info().
Uint64("previous_da_start_height", daHeight).
Uint64("override_da_start_height", s.config.DA.StartHeight).
Msg("DA start height overridden by flag")
daHeight = startHeight
}
s.daRetrieverHeight.Store(daHeight)
s.logger.Info().
Uint64("height", state.LastBlockHeight).
Uint64("da_height", s.daRetrieverHeight.Load()).
Str("chain_id", state.ChainID).
Msg("initialized syncer state")
// Sync execution layer with store on startup
execReplayer := common.NewReplayer(s.store, s.exec, s.genesis, s.logger)
if err := execReplayer.SyncToHeight(s.ctx, state.LastBlockHeight); err != nil {
return fmt.Errorf("failed to sync execution layer on startup: %w", err)
}
return nil
}
// processLoop is the main coordination loop for processing events
func (s *Syncer) processLoop(ctx context.Context) {
s.logger.Info().Msg("starting process loop")
defer s.logger.Info().Msg("process loop stopped")
for {
select {
case <-ctx.Done():
return
case heightEvent, ok := <-s.heightInCh:
if ok {
s.inFlight.Add(1)
s.processHeightEvent(ctx, &heightEvent)
s.inFlight.Add(-1)
}
}
}
}
func (s *Syncer) startSyncWorkers(ctx context.Context) {
// DA follower is already started in Start().
s.wg.Add(2)
go s.pendingWorkerLoop(ctx)
go s.p2pWorkerLoop(ctx)
}
// HasReachedDAHead returns true once the DA follower has caught up to the DA head.
// Once set, it stays true.
func (s *Syncer) HasReachedDAHead() bool {
if s.daFollower != nil {
return s.daFollower.HasReachedHead()
}
return false
}
// PendingCount returns the number of unprocessed height events in the pipeline.
func (s *Syncer) PendingCount() int {
return len(s.heightInCh) + int(s.inFlight.Load()) + s.cache.PendingEventsCount()
}
func (s *Syncer) pendingWorkerLoop(ctx context.Context) {
defer s.wg.Done()
s.logger.Info().Msg("starting pending worker")
defer s.logger.Info().Msg("pending worker stopped")
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.processPendingEvents(ctx)
}
}
}
func (s *Syncer) p2pWorkerLoop(ctx context.Context) {
defer s.wg.Done()
logger := s.logger.With().Str("worker", "p2p").Logger()
logger.Info().Msg("starting P2P worker")
defer logger.Info().Msg("P2P worker stopped")
for {
select {
case <-ctx.Done():
return
default:
}
currentHeight, err := s.store.Height(ctx)
if err != nil {
logger.Error().Err(err).Msg("failed to get current height for P2P worker")
if !s.sleepOrDone(ctx, 50*time.Millisecond) {
return
}
continue
}
targetHeight := currentHeight + 1
waitCtx, cancel := context.WithCancel(ctx)
s.setP2PWaitState(targetHeight, cancel)
err = s.p2pHandler.ProcessHeight(waitCtx, targetHeight, s.heightInCh)
s.cancelP2PWait(targetHeight)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
continue
}
if waitCtx.Err() == nil {
logger.Warn().Err(err).Uint64("height", targetHeight).Msg("P2P handler failed to process height")
}
if !s.sleepOrDone(ctx, 50*time.Millisecond) {
return
}
continue
}
if err := s.waitForStoreHeight(ctx, targetHeight); err != nil {
if errors.Is(err, context.Canceled) {
return
}
logger.Error().Err(err).Uint64("height", targetHeight).Msg("failed waiting for height commit")
}
}
}
func (s *Syncer) waitForGenesis() bool {
if delay := time.Until(s.genesis.StartTime); delay > 0 {
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-s.ctx.Done():
return false
case <-timer.C:
}
}
return true
}
func (s *Syncer) PipeEvent(ctx context.Context, event common.DAHeightEvent) error {
// Avoid sending already seen events to channel (would have been skipped in processHeightEvent anyway)
if s.cache.IsHeaderSeen(event.Header.Hash().String()) {
return nil
}
select {
case s.heightInCh <- event:
return nil
case <-ctx.Done():
s.cache.SetPendingEvent(event.Header.Height(), &event)
return ctx.Err()
default:
s.cache.SetPendingEvent(event.Header.Height(), &event)
}
return nil
}
func (s *Syncer) processHeightEvent(ctx context.Context, event *common.DAHeightEvent) {
height := event.Header.Height()
headerHash := event.Header.Hash().String()
s.logger.Debug().
Uint64("height", height).
Uint64("da_height", event.DaHeight).
Str("hash", headerHash).
Str("source", string(event.Source)).
Msg("processing height event")
currentHeight, err := s.store.Height(ctx)
if err != nil {
s.logger.Error().Err(err).Msg("failed to get current height")
return
}
// Skip if already processed
if height <= currentHeight || s.cache.IsHeaderSeen(headerHash) {
s.logger.Debug().
Uint64("height", height).
Str("source", string(event.Source)).
Msg("height already processed")
return
}
// If this is not the next block in sequence, store as pending event
// This check is crucial as trySyncNextBlock simply attempts to sync the next block
if height != currentHeight+1 {
s.cache.SetPendingEvent(height, event)
s.logger.Debug().Uint64("height", height).Uint64("current_height", currentHeight).Msg("stored as pending event")
return
}
// If this is a P2P event with a DA height hint, trigger targeted DA retrieval
// This allows us to fetch the block directly from the specified DA height instead of sequential scanning
if event.Source == common.SourceP2P {
var daHeightHints []uint64
switch {
case event.DaHeightHints == [2]uint64{0, 0}:
// empty, nothing to do
case event.DaHeightHints[0] == 0:
// check only data
if _, exists := s.cache.GetDataDAIncludedByHeight(height); !exists {
daHeightHints = []uint64{event.DaHeightHints[1]}
}
case event.DaHeightHints[1] == 0:
// check only header
if _, exists := s.cache.GetHeaderDAIncludedByHeight(height); !exists {
daHeightHints = []uint64{event.DaHeightHints[0]}
}
default:
// check both
if _, exists := s.cache.GetHeaderDAIncludedByHeight(height); !exists {
daHeightHints = []uint64{event.DaHeightHints[0]}
}
if _, exists := s.cache.GetDataDAIncludedByHeight(height); !exists {
daHeightHints = append(daHeightHints, event.DaHeightHints[1])
}
if len(daHeightHints) == 2 && daHeightHints[0] == daHeightHints[1] {
daHeightHints = daHeightHints[0:1]
}
}
if len(daHeightHints) > 0 {
currentDAHeight := s.daRetrieverHeight.Load()
// Only fetch the latest DA height if any hint is suspiciously far ahead.
const daHintMaxDrift = uint64(200)
needsValidation := false
for _, h := range daHeightHints {
if h > currentDAHeight+daHintMaxDrift {
needsValidation = true
break
}
}
var latestDAHeight uint64
if needsValidation {
var err error
xCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
latestDAHeight, err = s.daClient.GetLatestDAHeight(xCtx)
cancel()
if err != nil {
s.logger.Warn().Err(err).Msg("failed to fetch latest DA height")
needsValidation = false // ignore error as height is checked in the daRetriever
}
}
for _, daHeightHint := range daHeightHints {
// Skip if we've already fetched past this height
if daHeightHint < currentDAHeight {
continue
}
if needsValidation && daHeightHint > latestDAHeight {
s.logger.Warn().Uint64("da_height_hint", daHeightHint).
Uint64("latest_da_height", latestDAHeight).
Msg("ignoring unreasonable DA height hint from P2P")
continue
}
s.logger.Debug().
Uint64("height", height).
Uint64("da_height_hint", daHeightHint).
Msg("P2P event with DA height hint, queuing priority DA retrieval")
// Queue priority DA retrieval - will be processed in fetchDAUntilCaughtUp
s.daFollower.QueuePriorityHeight(daHeightHint)
}
}
}
// Last data must be got from store if the event comes from DA and the data hash is empty.
// When if the event comes from P2P, the sequencer and then all the full nodes contains the data.
if event.Source == common.SourceDA && bytes.Equal(event.Header.DataHash, common.DataHashForEmptyTxs) && currentHeight > 0 {
_, lastData, err := s.store.GetBlockData(ctx, currentHeight)
if err != nil {
s.logger.Error().Err(err).Msg("failed to get last data")
return
}
event.Data.LastDataHash = lastData.Hash()
}
// Cancel any P2P wait that might still be blocked on this height, as we have a block for it.
s.cancelP2PWait(height)
// Try to sync the next block
if err := s.blockSyncer.TrySyncNextBlock(ctx, event); err != nil {
s.logger.Error().Err(err).
Uint64("event-height", event.Header.Height()).
Uint64("state-height", s.getLastState().LastBlockHeight).
Str("source", string(event.Source)).
Msg("failed to sync next block")
// If the error is not due to a validation error, re-store the event as pending
switch {
case errors.Is(err, errInvalidBlock) || s.hasCriticalError.Load():
// do not reschedule
case errors.Is(err, errMaliciousProposer):
s.sendCriticalError(fmt.Errorf("sequencer malicious. Restart the node with --node.aggregator --node.based_sequencer or keep the chain halted: %w", err))
case errors.Is(err, errInvalidState):
s.sendCriticalError(fmt.Errorf("invalid state detected (block-height %d, state-height %d) "+
"- block references do not match local state. Manual intervention required: %w", event.Header.Height(),
s.getLastState().LastBlockHeight, err))
default:
s.cache.SetPendingEvent(height, event)
}
return
}
}
var (
// errInvalidBlock is returned when a block is failing validation
errInvalidBlock = errors.New("invalid block")
// errInvalidState is returned when the state has diverged from the DA blocks
errInvalidState = errors.New("invalid state")
)
// TrySyncNextBlock attempts to sync the next available block
// the event is always the next block in sequence as processHeightEvent ensures it.
func (s *Syncer) TrySyncNextBlock(ctx context.Context, event *common.DAHeightEvent) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
header := event.Header
data := event.Data
nextHeight := event.Header.Height()
currentState := s.getLastState()
headerHash := header.Hash().String()
s.logger.Info().Uint64("height", nextHeight).Str("source", string(event.Source)).Msg("syncing block")
// Compared to the executor logic where the current block needs to be applied first,
// here only the previous block needs to be applied to proceed to the verification.
// The header validation must be done before applying the block to avoid executing gibberish
if err := s.ValidateBlock(ctx, currentState, data, header); err != nil {
// remove header as da included from cache
s.cache.RemoveHeaderDAIncluded(headerHash)
s.cache.RemoveDataDAIncluded(data.DACommitment().String())
if !errors.Is(err, errInvalidState) && !errors.Is(err, errInvalidBlock) {
return errors.Join(errInvalidBlock, err)
}
return err
}
// Verify forced inclusion transactions if configured.
// The check is actually only performed on DA-sourced blocks.
// P2P nodes aren't actually able to verify forced inclusion txs as DA inclusion happens later
// (so DA hints are not available) and DA hints cannot be trusted. This is a known limitation
// described in the ADR.
if event.Source == common.SourceDA {
if err := s.VerifyForcedInclusionTxs(ctx, event.DaHeight, data); err != nil {
s.logger.Error().Err(err).Uint64("height", nextHeight).Msg("forced inclusion verification failed")
if errors.Is(err, errMaliciousProposer) {
// remove header as da included from cache
s.cache.RemoveHeaderDAIncluded(headerHash)
s.cache.RemoveDataDAIncluded(data.DACommitment().String())
return err
}
}
}
// Apply block
newState, err := s.ApplyBlock(ctx, header.Header, data, currentState)
if err != nil {
return fmt.Errorf("failed to apply block: %w", err)
}
// Update DA height if needed.
// state.DAHeight is used for state persistence and restart recovery.
if event.DaHeight > newState.DAHeight {
newState.DAHeight = event.DaHeight
}
batch, err := s.store.NewBatch(ctx)
if err != nil {
return fmt.Errorf("failed to create batch: %w", err)
}
if err := batch.SaveBlockData(header, data, &header.Signature); err != nil {
return fmt.Errorf("failed to save block: %w", err)
}
if err := batch.SetHeight(nextHeight); err != nil {
return fmt.Errorf("failed to update height: %w", err)
}
if err := batch.UpdateState(newState); err != nil {
return fmt.Errorf("failed to update state: %w", err)
}
if err := batch.Commit(); err != nil {
return fmt.Errorf("failed to commit batch: %w", err)
}
// Update in-memory state after successful commit
s.SetLastState(newState)
s.metrics.Height.Set(float64(newState.LastBlockHeight))
// Mark as seen
s.cache.SetHeaderSeen(headerHash, header.Height())
if !bytes.Equal(header.DataHash, common.DataHashForEmptyTxs) {
s.cache.SetDataSeen(data.DACommitment().String(), newState.LastBlockHeight)
}
if s.p2pHandler != nil {
s.p2pHandler.SetProcessedHeight(newState.LastBlockHeight)
}
return nil
}
// ApplyBlock applies a block to get the new state
func (s *Syncer) ApplyBlock(ctx context.Context, header types.Header, data *types.Data, currentState types.State) (types.State, error) {
// Prepare transactions
rawTxs := make([][]byte, len(data.Txs))
for i, tx := range data.Txs {
rawTxs[i] = []byte(tx)
}
// Execute transactions
ctx = context.WithValue(ctx, types.HeaderContextKey, header)
newAppHash, err := s.executeTxsWithRetry(ctx, rawTxs, header, currentState)
if err != nil {
s.sendCriticalError(fmt.Errorf("failed to execute transactions: %w", err))
return types.State{}, fmt.Errorf("failed to execute transactions: %w", err)
}
// Create new state
newState, err := currentState.NextState(header, newAppHash)
if err != nil {
return types.State{}, fmt.Errorf("failed to create next state: %w", err)
}
return newState, nil
}
// executeTxsWithRetry executes transactions with retry logic.
// NOTE: the function retries the execution client call regardless of the error. Some execution clients errors are irrecoverable, and will eventually halt the node, as expected.
func (s *Syncer) executeTxsWithRetry(ctx context.Context, rawTxs [][]byte, header types.Header, currentState types.State) ([]byte, error) {
for attempt := 1; attempt <= common.MaxRetriesBeforeHalt; attempt++ {
newAppHash, err := s.exec.ExecuteTxs(ctx, rawTxs, header.Height(), header.Time(), currentState.AppHash)
if err != nil {
if attempt == common.MaxRetriesBeforeHalt {
return nil, fmt.Errorf("failed to execute transactions: %w", err)
}
s.logger.Error().Err(err).
Int("attempt", attempt).
Int("max_attempts", common.MaxRetriesBeforeHalt).
Uint64("height", header.Height()).
Msg("failed to execute transactions, retrying")
select {
case <-time.After(common.MaxRetriesTimeout):
continue
case <-ctx.Done():
return nil, fmt.Errorf("context cancelled during retry: %w", ctx.Err())
}
}
return newAppHash, nil
}
return nil, nil
}
// ValidateBlock validates a synced block
// NOTE: if the header was gibberish and somehow passed all validation prior but the data was correct
// or if the data was gibberish and somehow passed all validation prior but the header was correct
// we are still losing both in the pending event. This should never happen.
func (s *Syncer) ValidateBlock(_ context.Context, currState types.State, data *types.Data, header *types.SignedHeader) error {
// Set custom verifier for aggregator node signature
header.SetCustomVerifierForSyncNode(s.options.SyncNodeSignatureBytesProvider)
if err := header.ValidateBasicWithData(data); err != nil { //nolint:contextcheck // validation API does not accept context
return fmt.Errorf("invalid header: %w", err)
}
if err := currState.AssertValidForNextState(header, data); err != nil {
return errors.Join(errInvalidState, err)
}
return nil
}
var errMaliciousProposer = errors.New("malicious proposer detected")
// hashTx returns a hex-encoded SHA256 hash of the transaction.
func hashTx(tx []byte) string {
hash := sha256.Sum256(tx)
return hex.EncodeToString(hash[:])
}
// gracePeriodForEpoch returns the grace window for an epoch based on average
// block fullness. For each fullnessThreshold-sized band above the threshold one
// extra epoch is granted, up to maxGracePeriodEpochs.
func (s *Syncer) gracePeriodForEpoch(epochStart, epochEnd uint64) uint64 {
if epochEnd < epochStart {
return baseGracePeriodEpochs
}
// Empty DA heights contribute 0 bytes and still count toward the average,
// so spare capacity reduces the grace extension.
heightCount := epochEnd - epochStart + 1
s.forcedInclusionMu.RLock()
var totalBytes uint64
for h := epochStart; h <= epochEnd; h++ {
totalBytes += s.daBlockBytes[h]
}
s.forcedInclusionMu.RUnlock()
avgBytes := totalBytes / heightCount
threshold := uint64(math.Round(fullnessThreshold * float64(common.DefaultMaxBlobSize)))
var extra uint64
if avgBytes > threshold {
extra = (avgBytes - threshold) / threshold
}
return min(baseGracePeriodEpochs+extra, maxGracePeriodEpochs)
}
// VerifyForcedInclusionTxs checks that every forced-inclusion tx submitted
// during epochs whose grace window has elapsed appears in seenBlockTxs.
// Txs may be spread across multiple blocks; what matters is that each one
// landed somewhere before its epoch's grace deadline.
func (s *Syncer) VerifyForcedInclusionTxs(ctx context.Context, daHeight uint64, data *types.Data) error {
if s.fiRetriever == nil || s.genesis.DAEpochForcedInclusion == 0 {
return nil
}
epochSize := s.genesis.DAEpochForcedInclusion
daStart := s.genesis.DAStartHeight
// Record txs and byte count for this DA height.
var blockBytes uint64
for _, tx := range data.Txs {
blockBytes += uint64(len(tx))
}
s.forcedInclusionMu.Lock()
hashes := make([]string, 0, len(data.Txs))
for _, tx := range data.Txs {
h := hashTx(tx)
s.seenBlockTxs[h] = struct{}{}
hashes = append(hashes, h)
}
s.seenBlockTxsByHeight[daHeight] = hashes
s.daBlockBytes[daHeight] = blockBytes
s.forcedInclusionMu.Unlock()
if daHeight < daStart || daHeight < s.getLastState().DAHeight {
return nil
}
executionInfo, err := s.exec.GetExecutionInfo(ctx)
if err != nil {
return fmt.Errorf("failed to get execution info: %w", err)
}
var maliciousCount int
// Resume from the last checked epoch rather than re-scanning from genesis.
// If no epoch has been checked yet, start from the first epoch end.
firstEpochEnd := daStart + epochSize - 1
var startEpochEnd uint64
if s.lastCheckedEpochEnd == 0 || s.lastCheckedEpochEnd < firstEpochEnd {
startEpochEnd = firstEpochEnd
} else {
startEpochEnd = s.lastCheckedEpochEnd + epochSize
}
for epochEnd := startEpochEnd; ; epochEnd += epochSize {
epochStart := epochEnd - (epochSize - 1)
gracePeriod := s.gracePeriodForEpoch(epochStart, epochEnd)
graceBoundary := epochEnd + gracePeriod*epochSize
if graceBoundary >= daHeight {
break
}
event, retrieveErr := s.fiRetriever.RetrieveForcedIncludedTxs(ctx, epochEnd)
if retrieveErr != nil {
if errors.Is(retrieveErr, da.ErrForceInclusionNotConfigured) {
return nil
}
return fmt.Errorf("failed to retrieve forced inclusion txs for epoch ending at %d: %w", epochEnd, retrieveErr)
}
if len(event.Txs) == 0 {
if epochEnd > s.lastCheckedEpochEnd {
s.pruneUpTo(epochEnd)
}
continue
}
// Skip intrinsically invalid txs so the sequencer isn't blamed for dropping them.
filterStatuses, filterErr := s.exec.FilterTxs(ctx, event.Txs, common.DefaultMaxBlobSize, executionInfo.MaxGas, true)
if filterErr != nil {
return fmt.Errorf("failed to filter forced inclusion txs: %w", filterErr)
}
for i, tx := range event.Txs {
if filterStatuses[i] != coreexecutor.FilterOK {
continue
}
txHash := hashTx(tx)
s.forcedInclusionMu.RLock()
_, seen := s.seenBlockTxs[txHash]
s.forcedInclusionMu.RUnlock()
if !seen {
maliciousCount++
s.logger.Warn().
Uint64("current_da_height", daHeight).
Uint64("epoch_end", epochEnd).
Uint64("grace_boundary", graceBoundary).
Str("tx_hash", txHash[:16]).
Msg("forced inclusion transaction past grace boundary not included - marking as malicious")
}
}