From 1785f9bb1c72a27a27a9d8085ef2db4dc378eb6e Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 5 Aug 2011 21:00:25 +0000 Subject: [PATCH] Clear out the remaining non-security related FindBugs warnings. --- src/com/google/bitcoin/core/Block.java | 14 ++- .../bitcoin/core/NetworkParameters.java | 1 + src/com/google/bitcoin/core/Sha256Hash.java | 3 +- src/com/google/bitcoin/core/Transaction.java | 9 +- .../google/bitcoin/store/DiskBlockStore.java | 112 +++++++++--------- 5 files changed, 78 insertions(+), 61 deletions(-) diff --git a/src/com/google/bitcoin/core/Block.java b/src/com/google/bitcoin/core/Block.java index 5edf361c..eaf5bbe4 100644 --- a/src/com/google/bitcoin/core/Block.java +++ b/src/com/google/bitcoin/core/Block.java @@ -18,6 +18,7 @@ package com.google.bitcoin.core; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.ObjectInputStream; import java.io.OutputStream; import java.math.BigInteger; import java.util.*; @@ -78,6 +79,13 @@ public class Block extends Message { super(params, payloadBytes, 0); } + private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { + ois.defaultReadObject(); + // This code is not actually necessary, as transient fields are initialized to the default value which is in + // this case null. However it clears out a FindBugs warning and makes it explicit what we're doing. + hash = null; + } + void parse() throws ProtocolException { version = readUint32(); prevBlockHash = readHash(); @@ -172,13 +180,13 @@ public class Block extends Message { public Block cloneAsHeader() { Block block = new Block(params); block.nonce = nonce; - block.prevBlockHash = prevBlockHash.clone(); - block.merkleRoot = getMerkleRoot().clone(); + block.prevBlockHash = prevBlockHash.duplicate(); + block.merkleRoot = getMerkleRoot().duplicate(); block.version = version; block.time = time; block.difficultyTarget = difficultyTarget; block.transactions = null; - block.hash = getHash().clone(); + block.hash = getHash().duplicate(); return block; } diff --git a/src/com/google/bitcoin/core/NetworkParameters.java b/src/com/google/bitcoin/core/NetworkParameters.java index d2e76f82..1e6ec66c 100644 --- a/src/com/google/bitcoin/core/NetworkParameters.java +++ b/src/com/google/bitcoin/core/NetworkParameters.java @@ -86,6 +86,7 @@ public class NetworkParameters implements Serializable { t.outputs.add(new TransactionOutput(n, t, scriptPubKeyBytes.toByteArray())); } catch (Exception e) { // Cannot happen. + throw new RuntimeException(e); } genesisBlock.addTransaction(t); return genesisBlock; diff --git a/src/com/google/bitcoin/core/Sha256Hash.java b/src/com/google/bitcoin/core/Sha256Hash.java index 4efee993..1669fa4d 100644 --- a/src/com/google/bitcoin/core/Sha256Hash.java +++ b/src/com/google/bitcoin/core/Sha256Hash.java @@ -74,8 +74,7 @@ public class Sha256Hash implements Serializable { return bytes; } - @Override - public Sha256Hash clone() { + public Sha256Hash duplicate() { return new Sha256Hash(bytes); } } diff --git a/src/com/google/bitcoin/core/Transaction.java b/src/com/google/bitcoin/core/Transaction.java index 2e2cda10..542eb564 100644 --- a/src/com/google/bitcoin/core/Transaction.java +++ b/src/com/google/bitcoin/core/Transaction.java @@ -258,12 +258,15 @@ public class Transaction extends Message implements Serializable { s.append(getHashAsString()); s.append("\n"); if (isCoinBase()) { - String script = "???"; - String script2 = "???"; + String script; + String script2; try { script = inputs.get(0).getScriptSig().toString(); script2 = outputs.get(0).getScriptPubKey().toString(); - } catch (ScriptException e) {} + } catch (ScriptException e) { + script = "???"; + script2 = "???"; + } return " == COINBASE TXN (scriptSig " + script + ") (scriptPubKey " + script2 + ")"; } for (TransactionInput in : inputs) { diff --git a/src/com/google/bitcoin/store/DiskBlockStore.java b/src/com/google/bitcoin/store/DiskBlockStore.java index 9401dd51..5c9b245e 100644 --- a/src/com/google/bitcoin/store/DiskBlockStore.java +++ b/src/com/google/bitcoin/store/DiskBlockStore.java @@ -75,62 +75,68 @@ public class DiskBlockStore implements BlockStore { private void load(File file) throws IOException, BlockStoreException { log.info("Reading block store from {}", file); - InputStream input = new BufferedInputStream(new FileInputStream(file)); - // Read a version byte. - int version = input.read(); - if (version == -1) { - // No such file or the file was empty. - throw new FileNotFoundException(file.getName() + " does not exist or 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]; - input.read(chainHeadHash); - 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]; + InputStream input = null; try { - while (true) { - // Read a block from disk. - if (input.read(headerBytes) < 80) { - // End of file. - break; - } - // 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); + input = new BufferedInputStream(new FileInputStream(file)); + // Read a version byte. + int version = input.read(); + if (version == -1) { + // No such file or the file was empty. + throw new FileNotFoundException(file.getName() + " does not exist or is empty"); } - } 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); + 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 (input.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. + if (input.read(headerBytes) < 80) { + // End of file. + break; + } + // 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 { + if (input != null) input.close(); } - long elapsed = System.currentTimeMillis() - now; - log.info("Block chain read complete in {}ms", elapsed); } public synchronized void put(StoredBlock block) throws BlockStoreException {