From 2191a9979f93924d3791bcc58ae134cb030ce4d9 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 5 Aug 2011 14:10:57 +0000 Subject: [PATCH] Rename Block.getTime() to Block.getTimeSeconds() and note the metric used in the javadoc. Don't rethrow BlockStoreException as RuntimeException in BlockChain constructor. Updates issue 66. --- src/com/google/bitcoin/core/Block.java | 7 +++++-- src/com/google/bitcoin/core/BlockChain.java | 19 ++++++++----------- .../google/bitcoin/core/DownloadListener.java | 2 +- .../google/bitcoin/core/BlockChainTest.java | 4 +--- .../google/bitcoin/core/ChainSplitTests.java | 2 +- .../google/bitcoin/core/PeerGroupTest.java | 2 +- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/com/google/bitcoin/core/Block.java b/src/com/google/bitcoin/core/Block.java index cdb2802c..5edf361c 100644 --- a/src/com/google/bitcoin/core/Block.java +++ b/src/com/google/bitcoin/core/Block.java @@ -436,8 +436,11 @@ public class Block extends Message { this.hash = null; } - /** Returns the time in seconds at which the block was solved and broadcast, according to the clock of the solving node. */ - public long getTime() { + /** + * Returns the time at which the block was solved and broadcast, according to the clock of the solving node. + * This is measured in seconds since the UNIX epoch (midnight Jan 1st 1970). + */ + public long getTimeSeconds() { return time; } diff --git a/src/com/google/bitcoin/core/BlockChain.java b/src/com/google/bitcoin/core/BlockChain.java index 5b1ee866..ab6460d1 100644 --- a/src/com/google/bitcoin/core/BlockChain.java +++ b/src/com/google/bitcoin/core/BlockChain.java @@ -79,7 +79,7 @@ public class BlockChain { * For the store you can use a {@link com.google.bitcoin.store.MemoryBlockStore} if you don't care about saving the downloaded data, or a * {@link com.google.bitcoin.store.BoundedOverheadBlockStore} if you'd like to ensure fast startup the next time you run the program. */ - public BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore) { + public BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore) throws BlockStoreException { this(params, new ArrayList(), blockStore); if (wallet != null) addWallet(wallet); @@ -89,21 +89,18 @@ public class BlockChain { * Constructs a BlockChain that has no wallet at all. This is helpful when you don't actually care about sending * and receiving coins but rather, just want to explore the network data structures. */ - public BlockChain(NetworkParameters params, BlockStore blockStore) { + public BlockChain(NetworkParameters params, BlockStore blockStore) throws BlockStoreException { this(params, new ArrayList(), blockStore); } /** * Constructs a BlockChain connected to the given list of wallets and a store. */ - public BlockChain(NetworkParameters params, List wallets, BlockStore blockStore){ - try { - this.blockStore = blockStore; - chainHead = blockStore.getChainHead(); - log.info("chain head is:\n{}", chainHead.getHeader()); - } catch (BlockStoreException e) { - throw new RuntimeException(e); - } + public BlockChain(NetworkParameters params, List wallets, + BlockStore blockStore) throws BlockStoreException { + this.blockStore = blockStore; + chainHead = blockStore.getChainHead(); + log.info("chain head is:\n{}", chainHead.getHeader()); this.params = params; this.wallets = new ArrayList(wallets); } @@ -397,7 +394,7 @@ public class BlockChain { log.info("Difficulty transition traversal took {}msec", System.currentTimeMillis() - now); Block blockIntervalAgo = cursor.getHeader(); - int timespan = (int) (prev.getTime() - blockIntervalAgo.getTime()); + int timespan = (int) (prev.getTimeSeconds() - blockIntervalAgo.getTimeSeconds()); // Limit the adjustment step. if (timespan < params.targetTimespan / 4) timespan = params.targetTimespan / 4; diff --git a/src/com/google/bitcoin/core/DownloadListener.java b/src/com/google/bitcoin/core/DownloadListener.java index 04191e8e..3865b897 100644 --- a/src/com/google/bitcoin/core/DownloadListener.java +++ b/src/com/google/bitcoin/core/DownloadListener.java @@ -59,7 +59,7 @@ public class DownloadListener extends AbstractPeerEventListener { double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft)); if ((int)pct != lastPercent) { - progress(pct, new Date(block.getTime() * 1000)); + progress(pct, new Date(block.getTimeSeconds() * 1000)); lastPercent = (int)pct; } } diff --git a/tests/com/google/bitcoin/core/BlockChainTest.java b/tests/com/google/bitcoin/core/BlockChainTest.java index 5b9fdbd8..eea56d53 100644 --- a/tests/com/google/bitcoin/core/BlockChainTest.java +++ b/tests/com/google/bitcoin/core/BlockChainTest.java @@ -44,10 +44,8 @@ public class BlockChainTest { } @Before - public void setUp() { - + public void setUp() throws Exception { testNetChain = new BlockChain(testNet, new Wallet(testNet), new MemoryBlockStore(testNet)); - unitTestParams = NetworkParameters.unitTests(); wallet = new Wallet(unitTestParams); wallet.addKey(new ECKey()); diff --git a/tests/com/google/bitcoin/core/ChainSplitTests.java b/tests/com/google/bitcoin/core/ChainSplitTests.java index ce2134cc..eba11ebe 100644 --- a/tests/com/google/bitcoin/core/ChainSplitTests.java +++ b/tests/com/google/bitcoin/core/ChainSplitTests.java @@ -34,7 +34,7 @@ public class ChainSplitTests { private Address someOtherGuy; @Before - public void setUp() { + public void setUp() throws Exception { unitTestParams = NetworkParameters.unitTests(); wallet = new Wallet(unitTestParams); wallet.addKey(new ECKey()); diff --git a/tests/com/google/bitcoin/core/PeerGroupTest.java b/tests/com/google/bitcoin/core/PeerGroupTest.java index 2b251222..6ad59284 100644 --- a/tests/com/google/bitcoin/core/PeerGroupTest.java +++ b/tests/com/google/bitcoin/core/PeerGroupTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License");