From 77a3d03385c5512777b2749eaead3e243f6eab73 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 23 Jun 2017 22:34:38 -0400 Subject: [PATCH] blockchain: explain isCaughtUp logic --- blockchain/pool.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/blockchain/pool.go b/blockchain/pool.go index a95589987..48a258c79 100644 --- a/blockchain/pool.go +++ b/blockchain/pool.go @@ -28,7 +28,7 @@ var peerTimeoutSeconds = time.Duration(15) // not const so we can override with Every so often we ask peers what height they're on so we can keep going. Requests are continuously made for blocks of higher heights until - the limits. If most of the requests have no available peers, and we + we reach the limits. If most of the requests have no available peers, and we are not at peer limits, we can probably switch to consensus reactor */ @@ -129,8 +129,6 @@ func (pool *BlockPool) IsCaughtUp() bool { pool.mtx.Lock() defer pool.mtx.Unlock() - height := pool.height - // Need at least 1 peer to be considered caught up. if len(pool.peers) == 0 { pool.Logger.Debug("Blockpool has no peers") @@ -142,8 +140,11 @@ func (pool *BlockPool) IsCaughtUp() bool { maxPeerHeight = MaxInt(maxPeerHeight, peer.height) } - isCaughtUp := (height > 0 || time.Since(pool.startTime) > 5*time.Second) && (maxPeerHeight == 0 || height >= maxPeerHeight) - pool.Logger.Info(Fmt("IsCaughtUp: %v", isCaughtUp), "height", height, "maxPeerHeight", maxPeerHeight) + // some conditions to determine if we're caught up + receivedBlockOrTimedOut := (pool.height > 0 || time.Since(pool.startTime) > 5*time.Second) + ourChainIsLongestAmongPeers := maxPeerHeight == 0 || pool.height >= maxPeerHeight + isCaughtUp := receivedBlockOrTimedOut && ourChainIsLongestAmongPeers + pool.Logger.Info(Fmt("IsCaughtUp: %v", isCaughtUp), "height", pool.height, "maxPeerHeight", maxPeerHeight) return isCaughtUp }