From 85c9950d9e1f357dacf81685809e8211530f8cdc Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 21 Feb 2013 14:29:26 +0100 Subject: [PATCH] Clear all possible NPE findbugs warnings, and fix some getter/setter synchronization mismatches. --- .../com/google/bitcoin/core/MemoryPool.java | 19 +++++++++++-------- .../com/google/bitcoin/core/Transaction.java | 2 +- .../store/MemoryFullPrunedBlockStore.java | 10 +++++----- .../core/FullPrunedBlockChainTest.java | 9 ++++++--- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/MemoryPool.java b/core/src/main/java/com/google/bitcoin/core/MemoryPool.java index 93f99975..6767e297 100644 --- a/core/src/main/java/com/google/bitcoin/core/MemoryPool.java +++ b/core/src/main/java/com/google/bitcoin/core/MemoryPool.java @@ -131,15 +131,18 @@ public class MemoryPool { // We've seen at least one peer announce with an inv. Preconditions.checkNotNull(entry.addresses); return entry.addresses.size(); - } else if (entry.tx.get() == null) { - // We previously downloaded this transaction, but nothing cared about it so the garbage collector threw - // it away. We also deleted the set that tracked which peers had seen it. Treat this case as a zero and - // just delete it from the map. - memoryPool.remove(txHash); - return 0; } else { - Preconditions.checkState(entry.addresses == null); - return entry.tx.get().getConfidence().numBroadcastPeers(); + final Transaction tx = entry.tx.get(); + if (tx == null) { + // We previously downloaded this transaction, but nothing cared about it so the garbage collector threw + // it away. We also deleted the set that tracked which peers had seen it. Treat this case as a zero and + // just delete it from the map. + memoryPool.remove(txHash); + return 0; + } else { + Preconditions.checkState(entry.addresses == null); + return tx.getConfidence().numBroadcastPeers(); + } } } diff --git a/core/src/main/java/com/google/bitcoin/core/Transaction.java b/core/src/main/java/com/google/bitcoin/core/Transaction.java index 7b0ec766..f0c6135a 100644 --- a/core/src/main/java/com/google/bitcoin/core/Transaction.java +++ b/core/src/main/java/com/google/bitcoin/core/Transaction.java @@ -953,7 +953,7 @@ public class Transaction extends ChildMessage implements Serializable { maybeParse(); return outputs.get(index); } - + public synchronized TransactionConfidence getConfidence() { if (confidence == null) { confidence = new TransactionConfidence(this); diff --git a/core/src/main/java/com/google/bitcoin/store/MemoryFullPrunedBlockStore.java b/core/src/main/java/com/google/bitcoin/store/MemoryFullPrunedBlockStore.java index d613a3e8..0d14f647 100644 --- a/core/src/main/java/com/google/bitcoin/store/MemoryFullPrunedBlockStore.java +++ b/core/src/main/java/com/google/bitcoin/store/MemoryFullPrunedBlockStore.java @@ -366,7 +366,7 @@ public class MemoryFullPrunedBlockStore implements FullPrunedBlockStore { return storedBlock == null ? null : storedBlock.block; } - public StoredBlock getOnceUndoableStoredBlock(Sha256Hash hash) throws BlockStoreException { + public synchronized StoredBlock getOnceUndoableStoredBlock(Sha256Hash hash) throws BlockStoreException { Preconditions.checkNotNull(blockMap, "MemoryFullPrunedBlockStore is closed"); StoredBlockAndWasUndoableFlag storedBlock = blockMap.get(hash); return (storedBlock != null && storedBlock.wasUndoable) ? storedBlock.block : null; @@ -377,7 +377,7 @@ public class MemoryFullPrunedBlockStore implements FullPrunedBlockStore { return fullBlockMap.get(hash); } - public StoredBlock getChainHead() throws BlockStoreException { + public synchronized StoredBlock getChainHead() throws BlockStoreException { Preconditions.checkNotNull(blockMap, "MemoryFullPrunedBlockStore is closed"); return chainHead; } @@ -387,12 +387,12 @@ public class MemoryFullPrunedBlockStore implements FullPrunedBlockStore { this.chainHead = chainHead; } - public StoredBlock getVerifiedChainHead() throws BlockStoreException { + public synchronized StoredBlock getVerifiedChainHead() throws BlockStoreException { Preconditions.checkNotNull(blockMap, "MemoryFullPrunedBlockStore is closed"); return verifiedChainHead; } - public void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException { + public synchronized void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException { Preconditions.checkNotNull(blockMap, "MemoryFullPrunedBlockStore is closed"); this.verifiedChainHead = chainHead; if (this.chainHead.getHeight() < chainHead.getHeight()) @@ -442,7 +442,7 @@ public class MemoryFullPrunedBlockStore implements FullPrunedBlockStore { transactionOutputMap.abortDatabaseBatchWrite(); } - public boolean hasUnspentOutputs(Sha256Hash hash, int numOutputs) throws BlockStoreException { + public synchronized boolean hasUnspentOutputs(Sha256Hash hash, int numOutputs) throws BlockStoreException { for (int i = 0; i < numOutputs; i++) if (getTransactionOutput(hash, i) != null) return true; diff --git a/core/src/test/java/com/google/bitcoin/core/FullPrunedBlockChainTest.java b/core/src/test/java/com/google/bitcoin/core/FullPrunedBlockChainTest.java index 2e0ea7ed..1ddcec9b 100644 --- a/core/src/test/java/com/google/bitcoin/core/FullPrunedBlockChainTest.java +++ b/core/src/test/java/com/google/bitcoin/core/FullPrunedBlockChainTest.java @@ -122,10 +122,13 @@ public class FullPrunedBlockChainTest { chain.add(rollingBlock); WeakReference undoBlock = new WeakReference(store.getUndoBlock(rollingBlock.getHash())); - assertTrue(undoBlock.get() != null); - assertTrue(undoBlock.get().getTransactions() == null); - WeakReference changes = new WeakReference(undoBlock.get().getTxOutChanges()); + + StoredUndoableBlock storedUndoableBlock = undoBlock.get(); + assertTrue(storedUndoableBlock != null); + assertTrue(storedUndoableBlock.getTransactions() == null); + WeakReference changes = new WeakReference(storedUndoableBlock.getTxOutChanges()); assertTrue(changes.get() != null); + storedUndoableBlock = null; // Blank the reference so it can be GCd. // Create a chain longer than UNDOABLE_BLOCKS_STORED for (int i = 0; i < UNDOABLE_BLOCKS_STORED; i++) {