From 83a9bbd4dd832d3699a51aad5931b902d7ad7f2b Mon Sep 17 00:00:00 2001
From: Dave Collins <davec@conformal.com>
Date: Wed, 4 Sep 2013 20:24:58 -0500
Subject: [PATCH] Update getheaders unknown block locator handling.

When no blocks in the block locator are found, start with the block after
the genesis block.  This means the client will start over with the genesis
block if unknown block locators are provided.  This mirrors the behavior
in the reference implementation.
---
 peer.go | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/peer.go b/peer.go
index 1a184237..ca56c298 100644
--- a/peer.go
+++ b/peer.go
@@ -495,7 +495,10 @@ func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) {
 	}
 
 	// Find the most recent known block based on the block locator.
-	startIdx := int64(-1)
+	// It's 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.
+	startIdx := int64(1)
 	for _, hash := range msg.BlockLocatorHashes {
 		block, err := p.server.db.FetchBlockBySha(hash)
 		if err == nil {
@@ -505,15 +508,6 @@ func (p *peer) handleGetHeadersMsg(msg *btcwire.MsgGetHeaders) {
 		}
 	}
 
-	// When the block locator refers to an unknown block, just return an
-	// empty headers message.  This behavior mirrors the reference
-	// implementation.
-	if startIdx == -1 {
-		headersMsg := btcwire.NewMsgHeaders()
-		p.QueueMessage(headersMsg)
-		return
-	}
-
 	// Don't attempt to fetch more than we can put into a single message.
 	if endIdx-startIdx > btcwire.MaxBlockHeadersPerMsg {
 		endIdx = startIdx + btcwire.MaxBlockHeadersPerMsg