From b008cd03889e0cd88e00935efc43d07876162e25 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 6 Apr 2012 18:41:40 +0200 Subject: [PATCH] Don't validate difficulty transitions on the testnet as there was a recent rule change, and nobody cares about security on testnet anyway. --- .../java/com/google/bitcoin/core/BlockChain.java | 3 ++- .../com/google/bitcoin/core/NetworkParameters.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/bitcoin/core/BlockChain.java b/core/src/main/java/com/google/bitcoin/core/BlockChain.java index 14af14f5..36a77899 100644 --- a/core/src/main/java/com/google/bitcoin/core/BlockChain.java +++ b/core/src/main/java/com/google/bitcoin/core/BlockChain.java @@ -207,7 +207,8 @@ public class BlockChain { // Create a new StoredBlock from this block. It will throw away the transaction data so when block goes // out of scope we will reclaim the used memory. StoredBlock newStoredBlock = storedPrev.build(block); - checkDifficultyTransitions(storedPrev, newStoredBlock); + if (params.checkBlockDifficulty) + checkDifficultyTransitions(storedPrev, newStoredBlock); blockStore.put(newStoredBlock); connectBlock(newStoredBlock, storedPrev, block.transactions); } 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 c0119672..00c3a01c 100644 --- a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java +++ b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java @@ -94,6 +94,11 @@ public class NetworkParameters implements Serializable { * signatures using it. */ public byte[] alertSigningKey; + /** + * Whether to check block difficulty on this network. The rules for testnet have changed and become + * quite complicated, so don't bother verifying them. It's only a useful security check on the main network. + */ + public boolean checkBlockDifficulty; /** * See getId(). This may be null for old deserialized wallets. In that case we derive it heuristically @@ -153,6 +158,7 @@ public class NetworkParameters implements Serializable { n.genesisBlock.setDifficultyTarget(0x1d07fff8L); n.genesisBlock.setNonce(384568319); n.id = ID_TESTNET; + n.checkBlockDifficulty = false; String genesisHash = n.genesisBlock.getHashAsString(); checkState(genesisHash.equals("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"), genesisHash); @@ -182,6 +188,7 @@ public class NetworkParameters implements Serializable { n.genesisBlock.setTime(1231006505L); n.genesisBlock.setNonce(2083236893); n.id = ID_PRODNET; + n.checkBlockDifficulty = true; String genesisHash = n.genesisBlock.getHashAsString(); checkState(genesisHash.equals("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"), genesisHash); @@ -197,6 +204,7 @@ public class NetworkParameters implements Serializable { n.genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); n.interval = 10; n.targetTimespan = 200000000; // 6 years. Just a very big number. + n.checkBlockDifficulty = true; n.id = "com.google.bitcoin.unittest"; return n; } @@ -216,6 +224,12 @@ public class NetworkParameters implements Serializable { return id; } + public boolean equals(Object other) { + if (!(other instanceof NetworkParameters)) return false; + NetworkParameters o = (NetworkParameters) other; + return o.getId().equals(getId()); + } + /** Returns the network parameters for the given string ID or NULL if not recognized. */ public static NetworkParameters fromID(String id) { if (id.equals(ID_PRODNET)) {