99 "sync/atomic"
1010 "time"
1111
12- goheader "github.com/celestiaorg/go-header"
1312 "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,6 +25,7 @@ import (
2525type daRetriever interface {
2626 RetrieveFromDA (ctx context.Context , daHeight uint64 ) ([]common.DAHeightEvent , error )
2727}
28+
2829type p2pHandler interface {
2930 ProcessHeaderRange (ctx context.Context , fromHeight , toHeight uint64 ) []common.DAHeightEvent
3031 ProcessDataRange (ctx context.Context , fromHeight , toHeight uint64 ) []common.DAHeightEvent
@@ -53,9 +54,9 @@ type Syncer struct {
5354 // DA state
5455 daHeight uint64
5556
56- // P2P stores
57- headerStore goheader. Store [* types.SignedHeader ]
58- dataStore goheader. Store [* types.Data ]
57+ // P2P handling
58+ headerBroadcaster common. Broadcaster [* types.SignedHeader ]
59+ dataBroadcaster common. Broadcaster [* types.Data ]
5960
6061 // Channels for coordination
6162 heightInCh chan common.DAHeightEvent
@@ -83,27 +84,27 @@ func NewSyncer(
8384 metrics * common.Metrics ,
8485 config config.Config ,
8586 genesis genesis.Genesis ,
86- headerStore goheader. Store [* types.SignedHeader ],
87- dataStore goheader. Store [* types.Data ],
87+ headerBroadcaster common. Broadcaster [* types.SignedHeader ],
88+ dataBroadcaster common. Broadcaster [* types.Data ],
8889 logger zerolog.Logger ,
8990 options common.BlockOptions ,
9091 errorCh chan <- error ,
9192) * Syncer {
9293 return & Syncer {
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 (),
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 (),
107108 }
108109}
109110
@@ -118,7 +119,7 @@ func (s *Syncer) Start(ctx context.Context) error {
118119
119120 // Initialize handlers
120121 s .daRetriever = NewDARetriever (s .da , s .cache , s .config , s .genesis , s .options , s .logger )
121- s .p2pHandler = NewP2PHandler (s .headerStore , s .dataStore , s .genesis , s .options , s .logger )
122+ s .p2pHandler = NewP2PHandler (s .headerBroadcaster . Store () , s .dataBroadcaster . Store () , s .genesis , s .options , s .logger )
122123
123124 // Start main processing loop
124125 s .wg .Add (1 )
@@ -327,7 +328,7 @@ func (s *Syncer) tryFetchFromP2P(lastHeaderHeight, lastDataHeight *uint64, block
327328 select {
328329 case <- blockTicker :
329330 // Process headers
330- newHeaderHeight := s .headerStore .Height ()
331+ newHeaderHeight := s .headerBroadcaster . Store () .Height ()
331332 if newHeaderHeight > * lastHeaderHeight {
332333 events := s .p2pHandler .ProcessHeaderRange (s .ctx , * lastHeaderHeight + 1 , newHeaderHeight )
333334 for _ , event := range events {
@@ -344,7 +345,7 @@ func (s *Syncer) tryFetchFromP2P(lastHeaderHeight, lastDataHeight *uint64, block
344345 }
345346
346347 // Process data
347- newDataHeight := s .dataStore .Height ()
348+ newDataHeight := s .headerBroadcaster . Store () .Height ()
348349 if newDataHeight == newHeaderHeight {
349350 * lastDataHeight = newDataHeight
350351 } else if newDataHeight > * lastDataHeight {
@@ -407,6 +408,15 @@ func (s *Syncer) processHeightEvent(event *common.DAHeightEvent) {
407408 }
408409 return
409410 }
411+
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
419+ }
410420}
411421
412422// errInvalidBlock is returned when a block is failing validation
0 commit comments