From 599d4a671c129e767a8679310b0eb4d8996ad463 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 14 Jul 2012 18:49:46 +0200 Subject: [PATCH] Add a testnet-in-a-box mode. Which allows connecting to a peer with only the genesis block. --- .../java/com/google/bitcoin/core/NetworkParameters.java | 7 +++++++ core/src/main/java/com/google/bitcoin/core/Peer.java | 2 +- .../java/com/google/bitcoin/core/TCPNetworkConnection.java | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java index 0ffdeef7..e9c22679 100644 --- a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java +++ b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java @@ -114,6 +114,11 @@ public class NetworkParameters implements Serializable { * Returns the number of blocks between subsidy decreases */ private int subsidyDecreaseBlockCount; + + /** + * If we are running in testnet-in-a-box mode, we allow connections to nodes with 0 non-genesis blocks + */ + boolean allowEmptyPeerChains; /** * The version codes that prefix addresses which are acceptable on this network. Although Satoshi intended these to @@ -213,6 +218,7 @@ public class NetworkParameters implements Serializable { n.setSpendableCoinbaseDepth(100); n.setSubsidyDecreaseBlockCount(210000); n.id = ID_TESTNET; + n.allowEmptyPeerChains = false; String genesisHash = n.genesisBlock.getHashAsString(); checkState(genesisHash.equals("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"), genesisHash); @@ -250,6 +256,7 @@ public class NetworkParameters implements Serializable { n.setSpendableCoinbaseDepth(100); n.setSubsidyDecreaseBlockCount(210000); n.id = ID_PRODNET; + n.allowEmptyPeerChains = false; String genesisHash = n.genesisBlock.getHashAsString(); checkState(genesisHash.equals("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"), genesisHash); diff --git a/core/src/main/java/com/google/bitcoin/core/Peer.java b/core/src/main/java/com/google/bitcoin/core/Peer.java index 4eb29ab3..95427e01 100644 --- a/core/src/main/java/com/google/bitcoin/core/Peer.java +++ b/core/src/main/java/com/google/bitcoin/core/Peer.java @@ -746,7 +746,7 @@ public class Peer { // chainHeight should not be zero/negative because we shouldn't have given the user a Peer that is to another // client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or // there is a bug in the peer management code. - Preconditions.checkState(chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight); + Preconditions.checkState(params.allowEmptyPeerChains || chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight); return chainHeight - blockChain.getChainHead().getHeight(); } diff --git a/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java b/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java index 0d03c4d7..95195922 100644 --- a/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java +++ b/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java @@ -161,7 +161,8 @@ public class TCPNetworkConnection implements NetworkConnection { // mode nodes because we can't download the data from them we need to find/verify transactions. Some bogus // implementations claim to have a block chain in their services field but then report a height of zero, filter // them out here. - if (!versionMessage.hasBlockChain() || versionMessage.bestHeight <= 0) { + if (!versionMessage.hasBlockChain() || + (!params.allowEmptyPeerChains && versionMessage.bestHeight <= 0)) { // Shut down the channel throw new ProtocolException("Peer does not have a copy of the block chain."); }