Skip to content

Commit

Permalink
PeerGroup: Add addPeersFromAddressMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarguindzberg committed Feb 12, 2019
1 parent f76d341 commit 659f3c1
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public class PeerGroup implements TransactionBroadcaster {
// How many connections we want to have open at the current time. If we lose connections, we'll try opening more
// until we reach this count.
@GuardedBy("lock") private int maxConnections;
// if true, we will listen to "addr" network messages and add nodes discovered this way.
// if false, only nodes found by discovery process are used/added.
@GuardedBy("lock")
private boolean addPeersFromAddressMessage = true;
// Minimum protocol version we will allow ourselves to connect to: require Bloom filtering.
private volatile int vMinRequiredProtocolVersion;

Expand Down Expand Up @@ -250,7 +254,7 @@ public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock f
}
}

private class PeerStartupListener implements PeerConnectedEventListener, PeerDisconnectedEventListener {
private class PeerStartupListener implements PeerConnectedEventListener, PeerDisconnectedEventListener, PreMessageReceivedEventListener {
@Override
public void onPeerConnected(Peer peer, int peerCount) {
handleNewPeer(peer);
Expand All @@ -261,6 +265,18 @@ public void onPeerDisconnected(Peer peer, int peerCount) {
// The channel will be automatically removed from channels.
handlePeerDeath(peer, null);
}

@Override
public Message onPreMessageReceived(Peer peer, Message m) {
if (m instanceof AddressMessage && addPeersFromAddressMessage) {
for( PeerAddress peerAddress : ((AddressMessage)m).getAddresses() ) {
addInactive(peerAddress);
}
}

// Just pass the message right through for further processing.
return m;
}
}

private final PeerStartupListener startupListener = new PeerStartupListener();
Expand Down Expand Up @@ -487,6 +503,20 @@ public void setMaxConnections(int maxConnections) {
channels.closeConnections(-adjustment);
}

/**
* Switch for enabling network peer discovery.
* if true, we will listen to "addr" network messages and add nodes discovered this way.
* if false, only nodes found by discovery process are used/added.
*/
public void setAddPeersFromAddressMessage(boolean addPeersFromAddressMessage) {
lock.lock();
try {
this.addPeersFromAddressMessage = addPeersFromAddressMessage;
} finally {
lock.unlock();
}
}

/**
* Configure download of pending transaction dependencies. A change of values only takes effect for newly connected
* peers.
Expand Down Expand Up @@ -1130,6 +1160,10 @@ private boolean maybeCheckForLocalhostPeer() {
socket = new Socket();
socket.connect(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), params.getPort()), vConnectTimeoutMillis);
localhostCheckState = LocalhostCheckState.FOUND;

// If we are connected to localhost we don't want to get other peers added from AddressMessage calls.
setAddPeersFromAddressMessage(false);

return true;
} catch (IOException e) {
log.info("Localhost peer not detected.");
Expand Down

0 comments on commit 659f3c1

Please sign in to comment.