3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 23:32:16 +00:00

PeerGroup: clear some static analysis warnings. Remove dead code.

This commit is contained in:
Mike Hearn 2013-12-23 21:45:01 +00:00
parent 2b577840b5
commit b0ce535aa9
2 changed files with 18 additions and 46 deletions

View File

@ -17,11 +17,11 @@
package com.google.bitcoin.core; package com.google.bitcoin.core;
import com.google.bitcoin.net.ClientConnectionManager;
import com.google.bitcoin.net.NioClientManager;
import com.google.bitcoin.net.discovery.PeerDiscovery; import com.google.bitcoin.net.discovery.PeerDiscovery;
import com.google.bitcoin.net.discovery.PeerDiscoveryException; import com.google.bitcoin.net.discovery.PeerDiscoveryException;
import com.google.bitcoin.script.Script; import com.google.bitcoin.script.Script;
import com.google.bitcoin.net.ClientConnectionManager;
import com.google.bitcoin.net.NioClientManager;
import com.google.bitcoin.utils.ListenerRegistration; import com.google.bitcoin.utils.ListenerRegistration;
import com.google.bitcoin.utils.Threading; import com.google.bitcoin.utils.Threading;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@ -36,8 +36,10 @@ import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -121,7 +123,11 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
@Override @Override
public void onBlocksDownloaded(Peer peer, Block block, int blocksLeft) { public void onBlocksDownloaded(Peer peer, Block block, int blocksLeft) {
handleBlocksDownloaded(); double rate = checkNotNull(chain).getFalsePositiveRate();
if (rate > bloomFilterFPRate * MAX_FP_RATE_INCREASE) {
log.info("Force update Bloom filter due to high false positive rate");
recalculateFastCatchupAndFilter(true);
}
} }
}; };
@ -136,14 +142,6 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
@Override public void onCoinsSent(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { onChanged(); } @Override public void onCoinsSent(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { onChanged(); }
}; };
private void handleBlocksDownloaded() {
double rate = chain.getFalsePositiveRate();
if (rate > bloomFilterFPRate * MAX_FP_RATE_INCREASE) {
log.info("Force update Bloom filter due to high false positive rate");
recalculateFastCatchupAndFilter(true);
}
}
private class PeerStartupListener extends AbstractPeerEventListener { private class PeerStartupListener extends AbstractPeerEventListener {
@Override @Override
public void onPeerConnected(Peer peer, int peerCount) { public void onPeerConnected(Peer peer, int peerCount) {
@ -337,11 +335,8 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
* a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it, * a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it,
* and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for * and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for
* {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain. * {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain.
*
* @param name
* @param version
*/ */
public void setUserAgent(String name, String version, String comments) { public void setUserAgent(String name, String version, @Nullable String comments) {
//TODO Check that height is needed here (it wasnt, but it should be, no?) //TODO Check that height is needed here (it wasnt, but it should be, no?)
int height = chain == null ? 0 : chain.getBestChainHeight(); int height = chain == null ? 0 : chain.getBestChainHeight();
VersionMessage ver = new VersionMessage(params, height, false); VersionMessage ver = new VersionMessage(params, height, false);
@ -370,9 +365,6 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
* a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it, * a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it,
* and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for * and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for
* {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain. * {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain.
*
* @param name
* @param version
*/ */
public void setUserAgent(String name, String version) { public void setUserAgent(String name, String version) {
setUserAgent(name, version, null); setUserAgent(name, version, null);
@ -654,7 +646,8 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
peer.setBloomFilter(filter); peer.setBloomFilter(filter);
// Reset the false positive estimate so that we don't send a flood of filter updates // Reset the false positive estimate so that we don't send a flood of filter updates
// if the estimate temporarily overshoots our threshold. // if the estimate temporarily overshoots our threshold.
chain.resetFalsePositiveEstimate(); if (chain != null)
chain.resetFalsePositiveEstimate();
} }
} }
// Now adjust the earliest key time backwards by a week to handle the case of clock drift. This can occur // Now adjust the earliest key time backwards by a week to handle the case of clock drift. This can occur
@ -894,7 +887,7 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
pingRunnable[0].run(); pingRunnable[0].run();
} }
private void setDownloadPeer(Peer peer) { private void setDownloadPeer(@Nullable Peer peer) {
lock.lock(); lock.lock();
try { try {
if (downloadPeer == peer) { if (downloadPeer == peer) {
@ -1068,7 +1061,6 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
* propagation across the network can be observed. If no value has been set using * propagation across the network can be observed. If no value has been set using
* {@link PeerGroup#setMinBroadcastConnections(int)} a default of half of whatever * {@link PeerGroup#setMinBroadcastConnections(int)} a default of half of whatever
* {@link com.google.bitcoin.core.PeerGroup#getMaxConnections()} returns is used. * {@link com.google.bitcoin.core.PeerGroup#getMaxConnections()} returns is used.
* @return
*/ */
public int getMinBroadcastConnections() { public int getMinBroadcastConnections() {
lock.lock(); lock.lock();
@ -1263,6 +1255,7 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
* Given a list of Peers, return a Peer to be used as the download peer. If you don't want PeerGroup to manage * Given a list of Peers, return a Peer to be used as the download peer. If you don't want PeerGroup to manage
* download peer statuses for you, just override this and always return null. * download peer statuses for you, just override this and always return null.
*/ */
@Nullable
protected Peer selectDownloadPeer(List<Peer> peers) { protected Peer selectDownloadPeer(List<Peer> peers) {
// Characteristics to select for in order of importance: // Characteristics to select for in order of importance:
// - Chain height is reasonable (majority of nodes) // - Chain height is reasonable (majority of nodes)
@ -1311,28 +1304,6 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
return candidates2.get(0).peer; return candidates2.get(0).peer;
} }
private static class PeerGroupThreadFactory implements ThreadFactory {
static final AtomicInteger poolNumber = new AtomicInteger(1);
final ThreadGroup group;
final AtomicInteger threadNumber = new AtomicInteger(1);
final String namePrefix;
PeerGroupThreadFactory() {
group = Thread.currentThread().getThreadGroup();
namePrefix = "PeerGroup-" + poolNumber.getAndIncrement() + "-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
// Lower the priority of the peer threads. This is to avoid competing with UI threads created by the API
// user when doing lots of work, like downloading the block chain. We select a priority level one lower
// than the parent thread, or the minimum.
t.setPriority(Math.max(Thread.MIN_PRIORITY, Thread.currentThread().getPriority() - 1));
t.setDaemon(true);
return t;
}
}
/** /**
* Returns the currently selected download peer. Bear in mind that it may have changed as soon as this method * Returns the currently selected download peer. Bear in mind that it may have changed as soon as this method
* returns. Can return null if no peer was selected. * returns. Can return null if no peer was selected.

View File

@ -16,6 +16,7 @@
package com.google.bitcoin.core; package com.google.bitcoin.core;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetAddress; import java.net.InetAddress;
@ -278,7 +279,7 @@ public class VersionMessage extends Message {
* @param comments Optional (can be null) platform or other node specific information. * @param comments Optional (can be null) platform or other node specific information.
* @throws IllegalArgumentException if name, version or comments contains invalid characters. * @throws IllegalArgumentException if name, version or comments contains invalid characters.
*/ */
public void appendToSubVer(String name, String version, String comments) { public void appendToSubVer(String name, String version, @Nullable String comments) {
checkSubVerComponent(name); checkSubVerComponent(name);
checkSubVerComponent(version); checkSubVerComponent(version);
if (comments != null) { if (comments != null) {