diff --git a/core/src/main/java/com/google/bitcoin/core/GetBlocksMessage.java b/core/src/main/java/com/google/bitcoin/core/GetBlocksMessage.java index 7865f1cf..ef26e464 100644 --- a/core/src/main/java/com/google/bitcoin/core/GetBlocksMessage.java +++ b/core/src/main/java/com/google/bitcoin/core/GetBlocksMessage.java @@ -100,7 +100,7 @@ public class GetBlocksMessage extends Message { @Override public int hashCode() { int hashCode = (int) version ^ "getblocks".hashCode(); - for (int i = 0; i < locator.size(); i++) hashCode ^= locator.get(i).hashCode(); + for (Sha256Hash aLocator : locator) hashCode ^= aLocator.hashCode(); hashCode ^= stopHash.hashCode(); return hashCode; } diff --git a/core/src/main/java/com/google/bitcoin/core/GetHeadersMessage.java b/core/src/main/java/com/google/bitcoin/core/GetHeadersMessage.java index b52786cd..23614eda 100644 --- a/core/src/main/java/com/google/bitcoin/core/GetHeadersMessage.java +++ b/core/src/main/java/com/google/bitcoin/core/GetHeadersMessage.java @@ -56,7 +56,7 @@ public class GetHeadersMessage extends GetBlocksMessage { @Override public int hashCode() { int hashCode = (int) version ^ "getheaders".hashCode(); - for (int i = 0; i < locator.size(); i++) hashCode ^= locator.get(i).hashCode(); + for (Sha256Hash aLocator : locator) hashCode ^= aLocator.hashCode(); hashCode ^= stopHash.hashCode(); return hashCode; } diff --git a/core/src/main/java/com/google/bitcoin/core/HeadersMessage.java b/core/src/main/java/com/google/bitcoin/core/HeadersMessage.java index 0bcef7b1..cfde4507 100644 --- a/core/src/main/java/com/google/bitcoin/core/HeadersMessage.java +++ b/core/src/main/java/com/google/bitcoin/core/HeadersMessage.java @@ -70,7 +70,7 @@ public class HeadersMessage extends Message { // Read 80 bytes of the header and one more byte for the transaction list, which is always a 00 because the // transaction list is empty. byte[] blockHeader = readBytes(81); - if (blockHeader[80] != 00) + if (blockHeader[80] != 0) throw new ProtocolException("Block header does not end with a null byte"); Block newBlockHeader = new Block(this.params, blockHeader, true, true, 81); blockHeaders.add(newBlockHeader); diff --git a/core/src/main/java/com/google/bitcoin/store/DiskBlockStore.java b/core/src/main/java/com/google/bitcoin/store/DiskBlockStore.java index a0eed564..4a1be9d8 100644 --- a/core/src/main/java/com/google/bitcoin/store/DiskBlockStore.java +++ b/core/src/main/java/com/google/bitcoin/store/DiskBlockStore.java @@ -109,69 +109,66 @@ public class DiskBlockStore implements BlockStore { private void load(File theFile) throws IOException, BlockStoreException { log.info("Reading block store from {}", theFile); - try { - // Read a version byte. - int version = file.read(); - if (version == -1) { - // No such file or the file was empty. - throw new FileNotFoundException(theFile.getName() + " is empty"); - } - if (version != 1) { - throw new BlockStoreException("Bad version number: " + version); - } - // Chain head pointer is the first thing in the file. - byte[] chainHeadHash = new byte[32]; - if (file.read(chainHeadHash) < chainHeadHash.length) - throw new BlockStoreException("Truncated block store: cannot read chain head hash"); - this.chainHead = new Sha256Hash(chainHeadHash); - log.info("Read chain head from disk: {}", this.chainHead); - long now = System.currentTimeMillis(); - // Rest of file is raw block headers. - byte[] headerBytes = new byte[Block.HEADER_SIZE]; - try { - while (true) { - // Read a block from disk. - int read = file.read(headerBytes); - if (read == -1) { - // End of file. - break; - } - if (read < headerBytes.length) { - throw new BlockStoreException("Truncated block store: partial block read"); - } - // Parse it. - Block b = new Block(params, headerBytes); - // Look up the previous block it connects to. - StoredBlock prev = get(b.getPrevBlockHash()); - StoredBlock s; - if (prev == null) { - // First block in the stored chain has to be treated specially. - if (b.equals(params.genesisBlock)) { - s = new StoredBlock(params.genesisBlock.cloneAsHeader(), params.genesisBlock.getWork(), 0); - } else { - throw new BlockStoreException("Could not connect " + b.getHash().toString() + " to " - + b.getPrevBlockHash().toString()); - } - } else { - // Don't try to verify the genesis block to avoid upsetting the unit tests. - b.verifyHeader(); - // Calculate its height and total chain work. - s = prev.build(b); - } - // Save in memory. - blockMap.put(b.getHash(), s); - } - } catch (ProtocolException e) { - // Corrupted file. - throw new BlockStoreException(e); - } catch (VerificationException e) { - // Should not be able to happen unless the file contains bad blocks. - throw new BlockStoreException(e); - } - long elapsed = System.currentTimeMillis() - now; - log.info("Block chain read complete in {}ms", elapsed); - } finally { + // Read a version byte. + int version = file.read(); + if (version == -1) { + // No such file or the file was empty. + throw new FileNotFoundException(theFile.getName() + " is empty"); } + if (version != 1) { + throw new BlockStoreException("Bad version number: " + version); + } + // Chain head pointer is the first thing in the file. + byte[] chainHeadHash = new byte[32]; + if (file.read(chainHeadHash) < chainHeadHash.length) + throw new BlockStoreException("Truncated block store: cannot read chain head hash"); + this.chainHead = new Sha256Hash(chainHeadHash); + log.info("Read chain head from disk: {}", this.chainHead); + long now = System.currentTimeMillis(); + // Rest of file is raw block headers. + byte[] headerBytes = new byte[Block.HEADER_SIZE]; + try { + while (true) { + // Read a block from disk. + int read = file.read(headerBytes); + if (read == -1) { + // End of file. + break; + } + if (read < headerBytes.length) { + throw new BlockStoreException("Truncated block store: partial block read"); + } + // Parse it. + Block b = new Block(params, headerBytes); + // Look up the previous block it connects to. + StoredBlock prev = get(b.getPrevBlockHash()); + StoredBlock s; + if (prev == null) { + // First block in the stored chain has to be treated specially. + if (b.equals(params.genesisBlock)) { + s = new StoredBlock(params.genesisBlock.cloneAsHeader(), params.genesisBlock.getWork(), 0); + } else { + throw new BlockStoreException("Could not connect " + b.getHash().toString() + " to " + + b.getPrevBlockHash().toString()); + } + } else { + // Don't try to verify the genesis block to avoid upsetting the unit tests. + b.verifyHeader(); + // Calculate its height and total chain work. + s = prev.build(b); + } + // Save in memory. + blockMap.put(b.getHash(), s); + } + } catch (ProtocolException e) { + // Corrupted file. + throw new BlockStoreException(e); + } catch (VerificationException e) { + // Should not be able to happen unless the file contains bad blocks. + throw new BlockStoreException(e); + } + long elapsed = System.currentTimeMillis() - now; + log.info("Block chain read complete in {}ms", elapsed); } private void ensureOpen() throws BlockStoreException { diff --git a/core/src/main/java/com/google/bitcoin/store/WalletExtensionSerializer.java b/core/src/main/java/com/google/bitcoin/store/WalletExtensionSerializer.java index 92063078..b68041aa 100644 --- a/core/src/main/java/com/google/bitcoin/store/WalletExtensionSerializer.java +++ b/core/src/main/java/com/google/bitcoin/store/WalletExtensionSerializer.java @@ -43,6 +43,6 @@ public class WalletExtensionSerializer { * Get collection of extensions to add, should be overridden by any class adding wallet extensions. */ public Collection getExtensionsToWrite(Wallet wallet) { - return Collections.emptyList(); + return Collections.emptyList(); } } diff --git a/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java b/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java index b751e9b5..9ad3eadf 100644 --- a/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java +++ b/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java @@ -185,11 +185,11 @@ public class BitcoinURI { } // Attempt to decode the rest of the tokens into a parameter map. - for (int i = 0; i < nameValuePairTokens.length; i++) { - String[] tokens = nameValuePairTokens[i].split("="); + for (String nameValuePairToken : nameValuePairTokens) { + String[] tokens = nameValuePairToken.split("="); if (tokens.length != 2 || "".equals(tokens[0])) { throw new BitcoinURIParseException("Malformed Bitcoin URI - cannot parse name value pair '" + - nameValuePairTokens[i] + "'"); + nameValuePairToken + "'"); } String nameToken = tokens[0].toLowerCase();