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 Dec 20, 2018
1 parent 0424c08 commit 91ab651
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 @@ -619,7 +619,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 @@ -633,6 +635,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 @@ -1033,7 +1046,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 91ab651

Please sign in to comment.