Skip to content

Commit

Permalink
Retry to sync if there are no peers
Browse files Browse the repository at this point in the history
  • Loading branch information
IronGauntlets committed Jan 31, 2024
1 parent 864bd20 commit 88058f7
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (s *syncService) randomNodeHeight(ctx context.Context) (int, error) {
return int(header.Number), nil
}

const retyrDuration = 5 * time.Second

func (s *syncService) start(ctx context.Context) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand All @@ -84,8 +86,13 @@ func (s *syncService) start(ctx context.Context) {
var err error
randHeight, err = s.randomNodeHeight(ctx)
if err != nil {
s.logError("Failed to get random node height", err)
cancelIteration()
if errors.Is(err, errNoPeers) {
s.log.Infow("No peers available", "retrying in", retyrDuration.String())
time.Sleep(retyrDuration)
} else {
s.logError("Failed to get random node height", err)
}
continue
}

Expand All @@ -98,10 +105,10 @@ func (s *syncService) start(ctx context.Context) {

blockBehind := randHeight - (nextHeight - 1)
if blockBehind <= 0 {
s.log.Infow("Random node height is the same or less as local height, retrying in 100ms", "Random node height", randHeight,
"Current height", nextHeight-1)
time.Sleep(100 * time.Millisecond)
s.log.Infow("Random node height is the same or less as local height", " retrying in", retyrDuration.String(),
"Random node height", randHeight, "Current height", nextHeight-1)
cancelIteration()
time.Sleep(retyrDuration)
continue
}

Expand Down

0 comments on commit 88058f7

Please sign in to comment.