Skip to content

Commit 0bfac9d

Browse files
authored
Move latest block fetch out of store callback (#1253)
store callback is on the hot path of sync process, a network request is not suitable to be here since it blocks the entire sync process for arbitrarily long
1 parent 80eea85 commit 0bfac9d

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

sync/sync.go

+53-20
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,17 @@ func (s *Synchronizer) verifierTask(ctx context.Context, block *core.Block, stat
222222
return
223223
}
224224
highestBlockHeader := s.highestBlockHeader.Load()
225-
if highestBlockHeader == nil || highestBlockHeader.Number <= block.Number {
226-
highestBlock, err := s.starknetData.BlockLatest(ctx)
227-
if err != nil {
228-
s.log.Warnw("Failed fetching latest block", "err", err)
229-
} else {
230-
s.highestBlockHeader.Store(highestBlock.Header)
231-
isBehind := highestBlock.Number > block.Number+uint64(maxWorkers())
232-
if s.catchUpMode != isBehind {
233-
resetStreams()
234-
}
235-
s.catchUpMode = isBehind
225+
if highestBlockHeader != nil {
226+
isBehind := highestBlockHeader.Number > block.Number+uint64(maxWorkers())
227+
if s.catchUpMode != isBehind {
228+
resetStreams()
229+
}
230+
s.catchUpMode = isBehind
231+
}
232+
233+
if highestBlockHeader == nil || highestBlockHeader.Number < block.Number {
234+
if s.highestBlockHeader.CompareAndSwap(highestBlockHeader, block.Header) {
235+
s.bestBlockGauge.Set(float64(block.Header.Number))
236236
}
237237
}
238238

@@ -266,6 +266,8 @@ func (s *Synchronizer) syncBlocks(syncCtx context.Context) {
266266

267267
pendingSem := make(chan struct{}, 1)
268268
go s.pollPending(syncCtx, pendingSem)
269+
latestSem := make(chan struct{}, 1)
270+
go s.pollLatest(syncCtx, latestSem)
269271

270272
for {
271273
select {
@@ -277,6 +279,7 @@ func (s *Synchronizer) syncBlocks(syncCtx context.Context) {
277279
select {
278280
case <-syncCtx.Done():
279281
pendingSem <- struct{}{}
282+
latestSem <- struct{}{}
280283
return
281284
default:
282285
streamCtx, streamCancel = context.WithCancel(syncCtx)
@@ -346,18 +349,54 @@ func (s *Synchronizer) pollPending(ctx context.Context, sem chan struct{}) {
346349
select {
347350
case sem <- struct{}{}:
348351
go func() {
352+
defer func() {
353+
<-sem
354+
}()
349355
err := s.fetchAndStorePending(ctx)
350356
if err != nil {
351357
s.log.Debugw("Error while trying to poll pending block", "err", err)
352358
}
353-
<-sem
354359
}()
355360
default:
356361
}
357362
}
358363
}
359364
}
360365

366+
func (s *Synchronizer) pollLatest(ctx context.Context, sem chan struct{}) {
367+
poll := func() {
368+
select {
369+
case sem <- struct{}{}:
370+
go func() {
371+
defer func() {
372+
<-sem
373+
}()
374+
highestBlock, err := s.starknetData.BlockLatest(ctx)
375+
if err != nil {
376+
s.log.Warnw("Failed fetching latest block", "err", err)
377+
} else {
378+
s.highestBlockHeader.Store(highestBlock.Header)
379+
}
380+
s.bestBlockGauge.Set(float64(highestBlock.Header.Number))
381+
}()
382+
default:
383+
}
384+
}
385+
386+
ticker := time.NewTicker(time.Minute)
387+
poll()
388+
389+
for {
390+
select {
391+
case <-ctx.Done():
392+
ticker.Stop()
393+
return
394+
case <-ticker.C:
395+
poll()
396+
}
397+
}
398+
}
399+
361400
func (s *Synchronizer) fetchAndStorePending(ctx context.Context) error {
362401
highestBlockHeader := s.highestBlockHeader.Load()
363402
if highestBlockHeader == nil {
@@ -394,18 +433,12 @@ func (s *Synchronizer) fetchAndStorePending(ctx context.Context) error {
394433

395434
func (s *Synchronizer) updateStats(block *core.Block) {
396435
var (
397-
transactions = block.TransactionCount
398-
currentHeight = block.Number
399-
highestKnownHeight uint64 = 0
436+
transactions = block.TransactionCount
437+
currentHeight = block.Number
400438
)
401-
highestBlockHeader := s.highestBlockHeader.Load()
402-
if highestBlockHeader != nil {
403-
highestKnownHeight = highestBlockHeader.Number
404-
}
405439

406440
s.blockCount.Inc()
407441
s.chainHeightGauge.Set(float64(currentHeight))
408-
s.bestBlockGauge.Set(float64(highestKnownHeight))
409442
s.transactionCount.Add(float64(transactions))
410443
}
411444

0 commit comments

Comments
 (0)