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 34461fe
  • Loading branch information
ManfredKarrer authored and oscarguindzberg committed Apr 10, 2019
1 parent be26e43 commit 40ba1b1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ public void go() {
if (retryTime > now) {
long delay = retryTime - now;
log.info("Waiting {} ms 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 @@ -512,6 +513,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 @@ -873,7 +885,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 40ba1b1

Please sign in to comment.