From 9edcac8de96df6442d0038abe324b0de36aaa6bb Mon Sep 17 00:00:00 2001 From: Owen Hu Date: Tue, 8 Nov 2022 23:10:24 +0800 Subject: [PATCH 1/3] add timeout for catch up condition check with no peers --- blockchain/v0/pool.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/blockchain/v0/pool.go b/blockchain/v0/pool.go index 46b4b915a..2dcaa55a5 100644 --- a/blockchain/v0/pool.go +++ b/blockchain/v0/pool.go @@ -173,8 +173,11 @@ func (pool *BlockPool) IsCaughtUp() bool { pool.mtx.Lock() defer pool.mtx.Unlock() - // Need at least 1 peer to be considered caught up. - if len(pool.peers) == 0 { + // 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 && time.Since(pool.startTime) < 10*time.Minute { pool.Logger.Debug("Blockpool has no peers") return false } @@ -187,6 +190,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 } From a65d79def08578084c3103f32a5b8ce9fc04cba2 Mon Sep 17 00:00:00 2001 From: Owen Hu Date: Wed, 9 Nov 2022 10:29:58 +0800 Subject: [PATCH 2/3] add pool height check --- blockchain/v0/pool.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/blockchain/v0/pool.go b/blockchain/v0/pool.go index 2dcaa55a5..a74ad1cc4 100644 --- a/blockchain/v0/pool.go +++ b/blockchain/v0/pool.go @@ -177,8 +177,11 @@ func (pool *BlockPool) IsCaughtUp() bool { // 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 && time.Since(pool.startTime) < 10*time.Minute { - pool.Logger.Debug("Blockpool has no peers") + if len(pool.peers) == 0 { + pool.Logger.Debug("Blockpool has no peers", "duration", time.Since(pool.startTime), "height", pool.height, "initHeight", pool.initHeight) + if time.Since(pool.startTime) > 1*time.Minute && pool.height == pool.initHeight { + return true + } return false } From 379ddbab19d193362b21cebee809bf165d893657 Mon Sep 17 00:00:00 2001 From: Owen Hu Date: Wed, 9 Nov 2022 10:30:26 +0800 Subject: [PATCH 3/3] adjust timeout --- blockchain/v0/pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/v0/pool.go b/blockchain/v0/pool.go index a74ad1cc4..9bec87e53 100644 --- a/blockchain/v0/pool.go +++ b/blockchain/v0/pool.go @@ -179,7 +179,7 @@ func (pool *BlockPool) IsCaughtUp() bool { // So we add a 10 minutes timeout to prevent this case. if len(pool.peers) == 0 { pool.Logger.Debug("Blockpool has no peers", "duration", time.Since(pool.startTime), "height", pool.height, "initHeight", pool.initHeight) - if time.Since(pool.startTime) > 1*time.Minute && pool.height == pool.initHeight { + if time.Since(pool.startTime) > 10*time.Minute && pool.height == pool.initHeight { return true } return false