Skip to content

Commit

Permalink
PeerGroup: Add check to not duplicate peers
Browse files Browse the repository at this point in the history
The inactives collection contained duplicated peerAddresses after
connection loss and reconnect.
We add a check to see if the to-get-added peerAddress is not already
in the collection and only add it if it is absent.
This problem was not discovered when using the public network as the
chance that same peerAddress get reported is pretty low. But with our
provided nodes we got frequently duplicates.

Fixes bisq-network/bisq#1703

Cherry-pick 05e675e
  • Loading branch information
ManfredKarrer authored and oscarguindzberg committed Feb 12, 2019
1 parent 0988148 commit 34461fe
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,9 @@ public void go() {
if (retryTime > now) {
long delay = retryTime - now;
log.info("Waiting {} msec before next connect attempt {}", delay, addrToTry == null ? "" : "to " + addrToTry);
inactives.add(addrToTry);

if (!isAlreadyAdded(addrToTry))
inactives.add(addrToTry);
executor.schedule(this, delay, TimeUnit.MILLISECONDS);
return;
}
Expand All @@ -596,6 +598,17 @@ public void go() {
}
};

private boolean isAlreadyAdded(PeerAddress peerAddress) {
boolean isAlreadyAdded = false;
for (PeerAddress a : inactives) {
if (a.getHostname() != null && a.getHostname().equals(peerAddress.getHostname())) {
isAlreadyAdded = true;
break;
}
}
return isAlreadyAdded;
}

private void triggerConnections() {
// Run on a background thread due to the need to potentially retry and back off in the background.
if (!executor.isShutdown())
Expand Down Expand Up @@ -996,7 +1009,10 @@ private boolean addInactive(PeerAddress peerAddress) {
return false;
}
backoffMap.put(peerAddress, new ExponentialBackoff(peerBackoffParams));
inactives.offer(peerAddress);

if (!isAlreadyAdded(peerAddress))
inactives.offer(peerAddress);

return true;
} finally {
lock.unlock();
Expand Down

0 comments on commit 34461fe

Please sign in to comment.