From 0e155405cc5f92b955e3794734f059e491fb886c 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 ef3e46f3b74..f282c52a0c9 100644 --- a/core/src/main/java/org/bitcoinj/core/PeerGroup.java +++ b/core/src/main/java/org/bitcoinj/core/PeerGroup.java @@ -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; } @@ -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()) @@ -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();