From 0c6b79afb04c94ffe8dec98d07437c41b69c61d7 Mon Sep 17 00:00:00 2001
From: Dave Collins <davec@conformal.com>
Date: Thu, 3 Oct 2013 11:21:41 -0500
Subject: [PATCH] Don't disconnect on unrequested blocks for regtest.

The "official" regression test tool intentionally sends some unrequested
duplicate blocks to ensure the chain handling code does not fail when
trying to insert them.  This commit adds an exception to the block manager
which typically disconnects peers that send unrequested blocks (they are
misbehaving if they do this) for regression test mode.
---
 blockmanager.go | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/blockmanager.go b/blockmanager.go
index 9bcc73b90..bc11848ef 100644
--- a/blockmanager.go
+++ b/blockmanager.go
@@ -216,12 +216,19 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {
 	// handler can request the parent blocks from the appropriate peer.
 	blockSha, _ := bmsg.block.Sha()
 
-	// If we didnt' ask for this block then the peer is misbehaving.
+	// If we didn't ask for this block then the peer is misbehaving.
 	if _, ok := bmsg.peer.requestedBlocks[*blockSha]; !ok {
-		log.Warnf("[BMGR] Got unreqeusted block from %s, disconnecting",
-			bmsg.peer.addr)
-		bmsg.peer.Disconnect()
-		return
+		// The regression test intentionally sends some blocks twice
+		// to test duplicate block insertion fails.  Don't disconnect
+		// the peer or ignore the block when we're in regression test
+		// mode in this case so the chain code is actually fed the
+		// duplicate blocks.
+		if !cfg.RegressionTest {
+			log.Warnf("[BMGR] Got unrequested block %v from %s -- "+
+				"disconnecting", blockSha, bmsg.peer.addr)
+			bmsg.peer.Disconnect()
+			return
+		}
 	}
 	b.blockPeer[*blockSha] = bmsg.peer