Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[R4R] add timeout for catch up condition check with no peers (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-reorg authored Jan 7, 2023
1 parent 89216e0 commit 119afd7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions blockchain/v0/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,15 @@ func (pool *BlockPool) IsCaughtUp() bool {
pool.mtx.Lock()
defer pool.mtx.Unlock()

// Need at least 1 peer to be considered caught up.
// Normally it needs at least 1 peer to be considered caught up.
// There is a change that a node with the majority of voting power has no peers, it will never have changes
// to catch up and switch to consensus reactor, resulting in stopping the chain.
// So we add a 10 minutes timeout to prevent this case.
if len(pool.peers) == 0 {
pool.Logger.Debug("Blockpool has no peers")
pool.Logger.Debug("Blockpool has no peers", "duration", time.Since(pool.startTime), "height", pool.height, "initHeight", pool.initHeight)
if time.Since(pool.startTime) > 10*time.Minute && pool.height == pool.initHeight {
return true
}
return false
}

Expand All @@ -187,6 +193,7 @@ func (pool *BlockPool) IsCaughtUp() bool {
receivedBlockOrTimedOut := pool.height > 0 || time.Since(pool.startTime) > 5*time.Second
ourChainIsLongestAmongPeers := pool.maxPeerHeight == 0 || pool.height >= (pool.maxPeerHeight-1)
isCaughtUp := receivedBlockOrTimedOut && ourChainIsLongestAmongPeers
pool.Logger.Debug("IsCaughtUp", "isCaughtUp", isCaughtUp, "receivedBlockOrTimedOut", receivedBlockOrTimedOut, "ourChainIsLongestAmongPeers", ourChainIsLongestAmongPeers, "pool.maxPeerHeight", pool.maxPeerHeight, "pool.height", pool.height, "pool.startTime", pool.startTime)
return isCaughtUp
}

Expand Down

0 comments on commit 119afd7

Please sign in to comment.