99 "sync/atomic"
1010 "time"
1111
12+ goheader "github.com/celestiaorg/go-header"
1213 "github.com/rs/zerolog"
13- "golang.org/x/sync/errgroup"
1414
1515 "github.com/evstack/ev-node/block/internal/cache"
1616 "github.com/evstack/ev-node/block/internal/common"
@@ -25,7 +25,6 @@ import (
2525type daRetriever interface {
2626 RetrieveFromDA (ctx context.Context , daHeight uint64 ) ([]common.DAHeightEvent , error )
2727}
28-
2928type p2pHandler interface {
3029 ProcessHeaderRange (ctx context.Context , fromHeight , toHeight uint64 ) []common.DAHeightEvent
3130 ProcessDataRange (ctx context.Context , fromHeight , toHeight uint64 ) []common.DAHeightEvent
@@ -54,9 +53,9 @@ type Syncer struct {
5453 // DA state
5554 daHeight uint64
5655
57- // P2P handling
58- headerBroadcaster common. Broadcaster [* types.SignedHeader ]
59- dataBroadcaster common. Broadcaster [* types.Data ]
56+ // P2P stores
57+ headerStore goheader. Store [* types.SignedHeader ]
58+ dataStore goheader. Store [* types.Data ]
6059
6160 // Channels for coordination
6261 heightInCh chan common.DAHeightEvent
@@ -84,27 +83,27 @@ func NewSyncer(
8483 metrics * common.Metrics ,
8584 config config.Config ,
8685 genesis genesis.Genesis ,
87- headerBroadcaster common. Broadcaster [* types.SignedHeader ],
88- dataBroadcaster common. Broadcaster [* types.Data ],
86+ headerStore goheader. Store [* types.SignedHeader ],
87+ dataStore goheader. Store [* types.Data ],
8988 logger zerolog.Logger ,
9089 options common.BlockOptions ,
9190 errorCh chan <- error ,
9291) * Syncer {
9392 return & Syncer {
94- store : store ,
95- exec : exec ,
96- da : da ,
97- cache : cache ,
98- metrics : metrics ,
99- config : config ,
100- genesis : genesis ,
101- options : options ,
102- headerBroadcaster : headerBroadcaster ,
103- dataBroadcaster : dataBroadcaster ,
104- lastStateMtx : & sync.RWMutex {},
105- heightInCh : make (chan common.DAHeightEvent , 10_000 ),
106- errorCh : errorCh ,
107- logger : logger .With ().Str ("component" , "syncer" ).Logger (),
93+ store : store ,
94+ exec : exec ,
95+ da : da ,
96+ cache : cache ,
97+ metrics : metrics ,
98+ config : config ,
99+ genesis : genesis ,
100+ options : options ,
101+ headerStore : headerStore ,
102+ dataStore : dataStore ,
103+ lastStateMtx : & sync.RWMutex {},
104+ heightInCh : make (chan common.DAHeightEvent , 10_000 ),
105+ errorCh : errorCh ,
106+ logger : logger .With ().Str ("component" , "syncer" ).Logger (),
108107 }
109108}
110109
@@ -119,7 +118,7 @@ func (s *Syncer) Start(ctx context.Context) error {
119118
120119 // Initialize handlers
121120 s .daRetriever = NewDARetriever (s .da , s .cache , s .config , s .genesis , s .options , s .logger )
122- s .p2pHandler = NewP2PHandler (s .headerBroadcaster . Store () , s .dataBroadcaster . Store () , s .genesis , s .options , s .logger )
121+ s .p2pHandler = NewP2PHandler (s .headerStore , s .dataStore , s .genesis , s .options , s .logger )
123122
124123 // Start main processing loop
125124 s .wg .Add (1 )
@@ -328,7 +327,7 @@ func (s *Syncer) tryFetchFromP2P(lastHeaderHeight, lastDataHeight *uint64, block
328327 select {
329328 case <- blockTicker :
330329 // Process headers
331- newHeaderHeight := s .headerBroadcaster . Store () .Height ()
330+ newHeaderHeight := s .headerStore .Height ()
332331 if newHeaderHeight > * lastHeaderHeight {
333332 events := s .p2pHandler .ProcessHeaderRange (s .ctx , * lastHeaderHeight + 1 , newHeaderHeight )
334333 for _ , event := range events {
@@ -345,7 +344,7 @@ func (s *Syncer) tryFetchFromP2P(lastHeaderHeight, lastDataHeight *uint64, block
345344 }
346345
347346 // Process data
348- newDataHeight := s .dataBroadcaster . Store () .Height ()
347+ newDataHeight := s .dataStore .Height ()
349348 if newDataHeight == newHeaderHeight {
350349 * lastDataHeight = newDataHeight
351350 } else if newDataHeight > * lastDataHeight {
@@ -409,13 +408,15 @@ func (s *Syncer) processHeightEvent(event *common.DAHeightEvent) {
409408 return
410409 }
411410
412- // broadcast header and data to P2P network
413- g , ctx := errgroup .WithContext (s .ctx )
414- g .Go (func () error { return s .headerBroadcaster .WriteToStoreAndBroadcast (ctx , event .Header ) })
415- g .Go (func () error { return s .dataBroadcaster .WriteToStoreAndBroadcast (ctx , event .Data ) })
416- if err := g .Wait (); err != nil {
417- s .logger .Error ().Err (err ).Msg ("failed to broadcast header and/data" )
418- // don't fail block production on broadcast error
411+ // Append new event to p2p stores
412+ if err := s .headerStore .Append (s .ctx , event .Header ); err != nil {
413+ s .logger .Error ().Err (err ).Msg ("failed to append event header to p2p store" )
414+ return
415+ }
416+
417+ if err := s .dataStore .Append (s .ctx , event .Data ); err != nil {
418+ s .logger .Error ().Err (err ).Msg ("failed to append event data to p2p store" )
419+ return
419420 }
420421}
421422
0 commit comments