Clear out some FindBugs warnings.

This commit is contained in:
Mike Hearn 2012-06-08 17:53:50 +02:00
parent 893013b415
commit 643088145e
8 changed files with 45 additions and 48 deletions

View File

@ -16,6 +16,7 @@
package com.google.bitcoin.core;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.Arrays;
@ -54,18 +55,12 @@ public class Base58 {
return "";
}
input = copyOfRange(input, 0, input.length);
//
// Count leading zeroes
//
// Count leading zeroes.
int zeroCount = 0;
while (zeroCount < input.length && input[zeroCount] == 0) {
++zeroCount;
}
//
// The actual encoding
//
// The actual encoding.
byte[] temp = new byte[input.length * 2];
int j = temp.length;
@ -75,27 +70,25 @@ public class Base58 {
if (input[startAt] == 0) {
++startAt;
}
temp[--j] = (byte) ALPHABET[mod];
}
//
// Strip extra '1' if there are some after decoding
//
// Strip extra '1' if there are some after decoding.
while (j < temp.length && temp[j] == ALPHABET[0]) {
++j;
}
//
// Add as many leading '1' as there were leading zeros.
//
while (--zeroCount >= 0) {
temp[--j] = (byte) ALPHABET[0];
}
byte[] output = copyOfRange(temp, j, temp.length);
return new String(output);
}
try {
return new String(output, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
public static byte[] decode(String input) throws AddressFormatException {
if (input.length() == 0) {

View File

@ -49,14 +49,14 @@ public class MemoryPool {
// Before we see the full transaction, we need to track how many peers advertised it, so we can estimate its
// confidence pre-chain inclusion assuming an un-tampered with network connection. After we see the full transaction
// we need to switch from tracking that data in the Entry to tracking it in the TransactionConfidence object itself.
private class WeakTransactionReference extends WeakReference<Transaction> {
private static class WeakTransactionReference extends WeakReference<Transaction> {
public Sha256Hash hash;
public WeakTransactionReference(Transaction tx, ReferenceQueue<Transaction> queue) {
super(tx, queue);
hash = tx.getHash();
}
};
private class Entry {
private static class Entry {
// Invariants: one of the two fields must be null, to indicate which is used.
Set<PeerAddress> addresses;
// We keep a weak reference to the transaction. This means that if no other bit of code finds the transaction
@ -160,14 +160,15 @@ public class MemoryPool {
Preconditions.checkState(entry.addresses == null);
// We only want one canonical object instance for a transaction no matter how many times it is
// deserialized.
if (entry.tx.get() == null) {
Transaction transaction = entry.tx.get();
if (transaction == null) {
// We previously downloaded this transaction, but the garbage collector threw it away because
// no other part of the system cared enough to keep it around (it's not relevant to us).
// Given the lack of interest last time we probably don't need to track it this time either.
log.info("{}: Provided with a transaction that we previously threw away: {}", byPeer, tx.getHash());
} else {
// We saw it before and kept it around. Hand back the canonical copy.
tx = entry.tx.get();
tx = transaction;
log.info("{}: Provided with a transaction downloaded before: [{}] {}",
new Object[] { byPeer, tx.getConfidence().numBroadcastPeers(), tx.getHash() });
}
@ -209,8 +210,8 @@ public class MemoryPool {
// This TX or its hash have been previously announced.
if (entry.tx != null) {
Preconditions.checkState(entry.addresses == null);
if (entry.tx.get() != null) {
Transaction tx = entry.tx.get();
Transaction tx = entry.tx.get();
if (tx != null) {
tx.getConfidence().markBroadcastBy(byPeer);
log.info("{}: Announced transaction we have seen before [{}] {}",
new Object[] { byPeer, tx.getConfidence().numBroadcastPeers(), tx.getHashAsString() });

View File

@ -403,6 +403,7 @@ public class Peer {
switch (item.type) {
case Transaction: transactions.add(item); break;
case Block: blocks.add(item); break;
default: throw new IllegalStateException("Not implemented: " + item.type);
}
}
@ -411,9 +412,11 @@ public class Peer {
Iterator<InventoryItem> it = transactions.iterator();
while (it.hasNext()) {
InventoryItem item = it.next();
if (memoryPool == null && downloadData) {
// If there's no memory pool only download transactions if we're configured to.
getdata.addItem(item);
if (memoryPool == null) {
if (downloadData) {
// If there's no memory pool only download transactions if we're configured to.
getdata.addItem(item);
}
} else {
// Only download the transaction if we are the first peer that saw it be advertised. Other peers will also
// see it be advertised in inv packets asynchronously, they co-ordinate via the memory pool. We could

View File

@ -527,9 +527,9 @@ public class Transaction extends ChildMessage implements Serializable {
public String toString() {
// Basic info about the tx.
StringBuffer s = new StringBuffer();
s.append(String.format(" %s: %s\n", getHashAsString(), getConfidence()));
s.append(String.format(" %s: %s%n", getHashAsString(), getConfidence()));
if (inputs.size() == 0) {
s.append(" INCOMPLETE: No inputs!\n");
s.append(String.format(" INCOMPLETE: No inputs!%n"));
return s.toString();
}
if (isCoinBase()) {
@ -561,7 +561,7 @@ public class Transaction extends ChildMessage implements Serializable {
} catch (Exception e) {
s.append("[exception: ").append(e.getMessage()).append("]");
}
s.append("\n");
s.append(String.format("%n"));
}
for (TransactionOutput out : outputs) {
s.append(" ");
@ -588,7 +588,7 @@ public class Transaction extends ChildMessage implements Serializable {
} catch (Exception e) {
s.append("[exception: ").append(e.getMessage()).append("]");
}
s.append("\n");
s.append(String.format("%n"));
}
return s.toString();
}

View File

@ -373,11 +373,14 @@ public class TransactionConfidence implements Serializable {
/** Returns a copy of this object. Event listeners are not duplicated. */
public synchronized TransactionConfidence duplicate() {
TransactionConfidence c = new TransactionConfidence(transaction);
c.broadcastBy.addAll(broadcastBy);
c.confidenceType = confidenceType;
c.overridingTransaction = overridingTransaction;
c.appearedAtChainHeight = appearedAtChainHeight;
return c;
// There is no point in this sync block, it's just to help FindBugs.
synchronized (c) {
c.broadcastBy.addAll(broadcastBy);
c.confidenceType = confidenceType;
c.overridingTransaction = overridingTransaction;
c.appearedAtChainHeight = appearedAtChainHeight;
return c;
}
}
private void runListeners() {

View File

@ -1327,13 +1327,13 @@ public class Wallet implements Serializable {
@Override
public synchronized String toString() {
StringBuilder builder = new StringBuilder();
builder.append(String.format("Wallet containing %s BTC in:\n", bitcoinValueToFriendlyString(getBalance())));
builder.append(String.format(" %d unspent transactions\n", unspent.size()));
builder.append(String.format(" %d spent transactions\n", spent.size()));
builder.append(String.format(" %d pending transactions\n", pending.size()));
builder.append(String.format(" %d inactive transactions\n", inactive.size()));
builder.append(String.format(" %d dead transactions\n", dead.size()));
builder.append(String.format("Last seen best block: %s\n", getLastBlockSeenHash()));
builder.append(String.format("Wallet containing %s BTC in:%n", bitcoinValueToFriendlyString(getBalance())));
builder.append(String.format(" %d unspent transactions%n", unspent.size()));
builder.append(String.format(" %d spent transactions%n", spent.size()));
builder.append(String.format(" %d pending transactions%n", pending.size()));
builder.append(String.format(" %d inactive transactions%n", inactive.size()));
builder.append(String.format(" %d dead transactions%n", dead.size()));
builder.append(String.format("Last seen best block: %s%n", getLastBlockSeenHash()));
// Do the keys.
builder.append("\nKeys:\n");
for (ECKey key : keychain) {
@ -1514,7 +1514,7 @@ public class Wallet implements Serializable {
// The old blocks have contributed to the depth and work done for all the transactions in the
// wallet that are in blocks up to and including the chain split block.
// The total depth and work done is calculated here and then subtracted from the appropriate transactions.
int depthToSubtract = oldBlocks == null ? 0 : oldBlocks.size();
int depthToSubtract = oldBlocks.size();
BigInteger workDoneToSubtract = BigInteger.ZERO;
for (StoredBlock b : oldBlocks) {

View File

@ -94,8 +94,8 @@ public class IrcDiscovery implements PeerDiscovery {
InetAddress ip = InetAddress.getByName("irc.lfnet.org");
log.info("Connecting to IRC with " + ip);
connection = new Socket(server, port);
writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
// Generate a random nick for the connection. This is chosen to be clearly identifiable as coming from
// BitCoinJ but not match the standard nick format, so full peers don't try and connect to us.

View File

@ -402,9 +402,6 @@ public class WalletTest {
assertEquals(TransactionConfidence.ConfidenceType.OVERRIDDEN_BY_DOUBLE_SPEND,
send1.getConfidence().getConfidenceType());
// Receive 10 BTC.
nanos = Utils.toNanoCoins(10, 0);
TestUtils.DoubleSpends doubleSpends = TestUtils.createFakeDoubleSpendTxns(params, myAddress);
// t1 spends to our wallet. t2 double spends somewhere else.
wallet.receivePending(doubleSpends.t1);