From 14adb6c24d6d416ca709e6526b6087eae7e84e91 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 3 Feb 2021 13:03:54 -0600 Subject: [PATCH] server: Respond to getheaders when same chain tip. The handler for getheaders currently ignores the request when the chain is not yet believed to be current. This is generally desirable and correct behavior, since it might otherwise lead peers to incorrect conclusions about the state of the peer. However, on private networks, such as simnet, it is not at all uncommon for every node in the network to no longer be current if a block hasn't been mined in a long time or when all nodes are stopped and restarted. In order to better handle these types of edge conditions, this modifies the server to respond to getheaders when the local chain tip is exactly the same as the requested locator even when it is not marked current yet. This results in more robust handling for private networks while still providing the normal desirable behavior prior to being current. --- server.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/server.go b/server.go index 60486758..c5f6ff75 100644 --- a/server.go +++ b/server.go @@ -1215,8 +1215,13 @@ func (sp *serverPeer) OnGetBlocks(p *peer.Peer, msg *wire.MsgGetBlocks) { // OnGetHeaders is invoked when a peer receives a getheaders wire message. func (sp *serverPeer) OnGetHeaders(p *peer.Peer, msg *wire.MsgGetHeaders) { - // Ignore getheaders requests if not in sync. - if !sp.server.syncManager.IsCurrent() { + // Ignore getheaders requests if not in sync unless the local best chain + // is exactly at the same tip as the requesting peer. + locatorHashes := msg.BlockLocatorHashes + chain := sp.server.chain + if !sp.server.syncManager.IsCurrent() && (len(locatorHashes) == 0 || + *locatorHashes[0] != chain.BestSnapshot().PrevHash) { + return } @@ -1228,8 +1233,7 @@ func (sp *serverPeer) OnGetHeaders(p *peer.Peer, msg *wire.MsgGetHeaders) { // Use the block after the genesis block if no other blocks in the // provided locator are known. This does mean the client will start // over with the genesis block if unknown block locators are provided. - chain := sp.server.chain - headers := chain.LocateHeaders(msg.BlockLocatorHashes, &msg.HashStop) + headers := chain.LocateHeaders(locatorHashes, &msg.HashStop) // Send found headers to the requesting peer. blockHeaders := make([]*wire.BlockHeader, len(headers))