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.
This commit is contained in:
Dave Collins 2021-02-03 13:03:54 -06:00
parent 32a14f8d7f
commit 14adb6c24d
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2

View File

@ -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))