3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Clear all possible NPE findbugs warnings, and fix some getter/setter synchronization mismatches.

This commit is contained in:
Mike Hearn 2013-02-21 14:29:26 +01:00
parent 002539f2b8
commit 85c9950d9e
4 changed files with 23 additions and 17 deletions

View File

@ -131,7 +131,9 @@ 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) {
} else {
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.
@ -139,7 +141,8 @@ public class MemoryPool {
return 0;
} else {
Preconditions.checkState(entry.addresses == null);
return entry.tx.get().getConfidence().numBroadcastPeers();
return tx.getConfidence().numBroadcastPeers();
}
}
}

View File

@ -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;

View File

@ -122,10 +122,13 @@ public class FullPrunedBlockChainTest {
chain.add(rollingBlock);
WeakReference<StoredUndoableBlock> undoBlock = new WeakReference<StoredUndoableBlock>(store.getUndoBlock(rollingBlock.getHash()));
assertTrue(undoBlock.get() != null);
assertTrue(undoBlock.get().getTransactions() == null);
WeakReference<TransactionOutputChanges> changes = new WeakReference<TransactionOutputChanges>(undoBlock.get().getTxOutChanges());
StoredUndoableBlock storedUndoableBlock = undoBlock.get();
assertTrue(storedUndoableBlock != null);
assertTrue(storedUndoableBlock.getTransactions() == null);
WeakReference<TransactionOutputChanges> changes = new WeakReference<TransactionOutputChanges>(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++) {