From 91ab651c8cdc8137d3f99ffc65c98f664858b07e Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Sun, 23 Sep 2018 20:10:16 -0500 Subject: [PATCH] PeerGroup: Add check to not duplicate peers 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 https://github.com/bisq-network/bisq/issues/1703 Cherry-pick https://github.com/bisq-network/bitcoinj/commit/05e675ed925a7fc91af5810c0d272e8869781b0d --- .../java/org/bitcoinj/core/PeerGroup.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/PeerGroup.java b/core/src/main/java/org/bitcoinj/core/PeerGroup.java index 09bca468723..aea67607227 100644 --- a/core/src/main/java/org/bitcoinj/core/PeerGroup.java +++ b/core/src/main/java/org/bitcoinj/core/PeerGroup.java @@ -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; } @@ -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()) @@ -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();