From fb912322ff5708725af5ad1aa347e1125ebb7402 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 22 May 2013 18:45:04 +0200 Subject: [PATCH] Replace Block.fakeClock with Utils.mockClock --- core/src/main/java/com/google/bitcoin/core/Block.java | 5 +---- core/src/main/java/com/google/bitcoin/core/Utils.java | 8 ++++++++ .../java/com/google/bitcoin/core/BlockChainTest.java | 10 +++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Block.java b/core/src/main/java/com/google/bitcoin/core/Block.java index e2dd91a5d..896db5bac 100644 --- a/core/src/main/java/com/google/bitcoin/core/Block.java +++ b/core/src/main/java/com/google/bitcoin/core/Block.java @@ -70,9 +70,6 @@ public class Block extends Message { /** A value for difficultyTarget (nBits) that allows half of all possible hash solutions. Used in unit testing. */ public static final long EASIEST_DIFFICULTY_TARGET = 0x207fFFFFL; - // For unit testing. If not zero, use this instead of the current time. - static long fakeClock = 0; - // Fields defined as part of the protocol format. private long version; private Sha256Hash prevBlockHash; @@ -627,7 +624,7 @@ public class Block extends Message { private void checkTimestamp() throws VerificationException { maybeParseHeader(); // Allow injection of a fake clock to allow unit testing. - long currentTime = fakeClock != 0 ? fakeClock : System.currentTimeMillis() / 1000; + long currentTime = Utils.now().getTime()/1000; if (time > currentTime + ALLOWED_TIME_DRIFT) throw new VerificationException("Block too far in future"); } diff --git a/core/src/main/java/com/google/bitcoin/core/Utils.java b/core/src/main/java/com/google/bitcoin/core/Utils.java index c692b332a..155b8d639 100644 --- a/core/src/main/java/com/google/bitcoin/core/Utils.java +++ b/core/src/main/java/com/google/bitcoin/core/Utils.java @@ -432,6 +432,14 @@ public class Utils { return mockTime; } + /** + * Sets the mock clock to the given time (in seconds) + * @param mockClock + */ + public static void setMockClock(long mockClock) { + mockTime = new Date(mockClock * 1000); + } + /** * Returns the current time, or a mocked out equivalent. */ diff --git a/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java b/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java index 6fa3e6e9d..9bf9cc29d 100644 --- a/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java +++ b/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java @@ -170,23 +170,23 @@ public class BlockChainTest { // Add a bunch of blocks in a loop until we reach a difficulty transition point. The unit test params have an // artificially shortened period. Block prev = unitTestParams.getGenesisBlock(); - Block.fakeClock = System.currentTimeMillis() / 1000; + Utils.setMockClock(System.currentTimeMillis()/1000); for (int i = 0; i < unitTestParams.getInterval() - 1; i++) { - Block newBlock = prev.createNextBlock(coinbaseTo, Block.fakeClock); + Block newBlock = prev.createNextBlock(coinbaseTo, Utils.now().getTime()/1000); assertTrue(chain.add(newBlock)); prev = newBlock; // The fake chain should seem to be "fast" for the purposes of difficulty calculations. - Block.fakeClock += 2; + Utils.rollMockClock(2); } // Now add another block that has no difficulty adjustment, it should be rejected. try { - chain.add(prev.createNextBlock(coinbaseTo, Block.fakeClock)); + chain.add(prev.createNextBlock(coinbaseTo, Utils.now().getTime()/1000)); fail(); } catch (VerificationException e) { } // Create a new block with the right difficulty target given our blistering speed relative to the huge amount // of time it's supposed to take (set in the unit test network parameters). - Block b = prev.createNextBlock(coinbaseTo, Block.fakeClock); + Block b = prev.createNextBlock(coinbaseTo, Utils.now().getTime()/1000); b.setDifficultyTarget(0x201fFFFFL); b.solve(); assertTrue(chain.add(b));