Replace Block.fakeClock with Utils.mockClock

This commit is contained in:
Matt Corallo 2013-05-22 18:45:04 +02:00 committed by Mike Hearn
parent 5369ca925a
commit fb912322ff
3 changed files with 14 additions and 9 deletions

View file

@ -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. */ /** 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; 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. // Fields defined as part of the protocol format.
private long version; private long version;
private Sha256Hash prevBlockHash; private Sha256Hash prevBlockHash;
@ -627,7 +624,7 @@ public class Block extends Message {
private void checkTimestamp() throws VerificationException { private void checkTimestamp() throws VerificationException {
maybeParseHeader(); maybeParseHeader();
// Allow injection of a fake clock to allow unit testing. // 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) if (time > currentTime + ALLOWED_TIME_DRIFT)
throw new VerificationException("Block too far in future"); throw new VerificationException("Block too far in future");
} }

View file

@ -432,6 +432,14 @@ public class Utils {
return mockTime; 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. * Returns the current time, or a mocked out equivalent.
*/ */

View file

@ -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 // Add a bunch of blocks in a loop until we reach a difficulty transition point. The unit test params have an
// artificially shortened period. // artificially shortened period.
Block prev = unitTestParams.getGenesisBlock(); Block prev = unitTestParams.getGenesisBlock();
Block.fakeClock = System.currentTimeMillis() / 1000; Utils.setMockClock(System.currentTimeMillis()/1000);
for (int i = 0; i < unitTestParams.getInterval() - 1; i++) { 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)); assertTrue(chain.add(newBlock));
prev = newBlock; prev = newBlock;
// The fake chain should seem to be "fast" for the purposes of difficulty calculations. // 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. // Now add another block that has no difficulty adjustment, it should be rejected.
try { try {
chain.add(prev.createNextBlock(coinbaseTo, Block.fakeClock)); chain.add(prev.createNextBlock(coinbaseTo, Utils.now().getTime()/1000));
fail(); fail();
} catch (VerificationException e) { } catch (VerificationException e) {
} }
// Create a new block with the right difficulty target given our blistering speed relative to the huge amount // 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). // 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.setDifficultyTarget(0x201fFFFFL);
b.solve(); b.solve();
assertTrue(chain.add(b)); assertTrue(chain.add(b));