Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move latest block fetch out of store callback #1253

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 53 additions & 20 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,17 @@ func (s *Synchronizer) verifierTask(ctx context.Context, block *core.Block, stat
return
}
highestBlockHeader := s.highestBlockHeader.Load()
if highestBlockHeader == nil || highestBlockHeader.Number <= block.Number {
highestBlock, err := s.starknetData.BlockLatest(ctx)
if err != nil {
s.log.Warnw("Failed fetching latest block", "err", err)
} else {
s.highestBlockHeader.Store(highestBlock.Header)
isBehind := highestBlock.Number > block.Number+uint64(maxWorkers())
if s.catchUpMode != isBehind {
resetStreams()
}
s.catchUpMode = isBehind
if highestBlockHeader != nil {
isBehind := highestBlockHeader.Number > block.Number+uint64(maxWorkers())
if s.catchUpMode != isBehind {
resetStreams()
}
omerfirmak marked this conversation as resolved.
Show resolved Hide resolved
s.catchUpMode = isBehind
}

if highestBlockHeader == nil || highestBlockHeader.Number < block.Number {
if s.highestBlockHeader.CompareAndSwap(highestBlockHeader, block.Header) {
s.bestBlockGauge.Set(float64(block.Header.Number))
}
}

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

pendingSem := make(chan struct{}, 1)
go s.pollPending(syncCtx, pendingSem)
latestSem := make(chan struct{}, 1)
go s.pollLatest(syncCtx, latestSem)
omerfirmak marked this conversation as resolved.
Show resolved Hide resolved

for {
select {
Expand All @@ -277,6 +279,7 @@ func (s *Synchronizer) syncBlocks(syncCtx context.Context) {
select {
case <-syncCtx.Done():
pendingSem <- struct{}{}
latestSem <- struct{}{}
return
default:
streamCtx, streamCancel = context.WithCancel(syncCtx)
Expand Down Expand Up @@ -346,18 +349,54 @@ func (s *Synchronizer) pollPending(ctx context.Context, sem chan struct{}) {
select {
case sem <- struct{}{}:
go func() {
defer func() {
<-sem
}()
omerfirmak marked this conversation as resolved.
Show resolved Hide resolved
err := s.fetchAndStorePending(ctx)
if err != nil {
s.log.Debugw("Error while trying to poll pending block", "err", err)
}
<-sem
}()
default:
}
}
}
}

func (s *Synchronizer) pollLatest(ctx context.Context, sem chan struct{}) {
poll := func() {
select {
case sem <- struct{}{}:
go func() {
defer func() {
<-sem
}()
omerfirmak marked this conversation as resolved.
Show resolved Hide resolved
highestBlock, err := s.starknetData.BlockLatest(ctx)
if err != nil {
s.log.Warnw("Failed fetching latest block", "err", err)
} else {
s.highestBlockHeader.Store(highestBlock.Header)
}
s.bestBlockGauge.Set(float64(highestBlock.Header.Number))
}()
default:
}
}

ticker := time.NewTicker(time.Minute)
joshklop marked this conversation as resolved.
Show resolved Hide resolved
poll()

for {
select {
case <-ctx.Done():
ticker.Stop()
joshklop marked this conversation as resolved.
Show resolved Hide resolved
return
case <-ticker.C:
poll()
}
}
}

func (s *Synchronizer) fetchAndStorePending(ctx context.Context) error {
highestBlockHeader := s.highestBlockHeader.Load()
if highestBlockHeader == nil {
Expand Down Expand Up @@ -394,18 +433,12 @@ func (s *Synchronizer) fetchAndStorePending(ctx context.Context) error {

func (s *Synchronizer) updateStats(block *core.Block) {
var (
transactions = block.TransactionCount
currentHeight = block.Number
highestKnownHeight uint64 = 0
transactions = block.TransactionCount
currentHeight = block.Number
)
highestBlockHeader := s.highestBlockHeader.Load()
if highestBlockHeader != nil {
highestKnownHeight = highestBlockHeader.Number
}

s.blockCount.Inc()
s.chainHeightGauge.Set(float64(currentHeight))
s.bestBlockGauge.Set(float64(highestKnownHeight))
s.transactionCount.Add(float64(transactions))
}

Expand Down