From 7395c800cdefb790de3b9abc3abedae994808a1d Mon Sep 17 00:00:00 2001 From: "stefan.pingel@consensys.net" Date: Mon, 22 Jan 2024 12:36:55 +1000 Subject: [PATCH 1/4] add new comperator Signed-off-by: stefan.pingel@consensys.net --- .../hyperledger/besu/ethereum/eth/manager/EthPeers.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java index e28b0a24ad9..aeadfa05cab 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java @@ -54,10 +54,13 @@ public class EthPeers { private static final Logger LOG = LoggerFactory.getLogger(EthPeers.class); public static final Comparator TOTAL_DIFFICULTY = - Comparator.comparing(((final EthPeer p) -> p.chainState().getEstimatedTotalDifficulty())); + Comparator.comparing((final EthPeer p) -> p.chainState().getEstimatedTotalDifficulty()); public static final Comparator CHAIN_HEIGHT = - Comparator.comparing(((final EthPeer p) -> p.chainState().getEstimatedHeight())); + Comparator.comparing((final EthPeer p) -> p.chainState().getEstimatedHeight()); + + public static final Comparator MOST_USEFUL_PEER = + Comparator.comparing((final EthPeer p) -> p.getReputation().getScore()).thenComparing(CHAIN_HEIGHT); public static final Comparator HEAVIEST_CHAIN = TOTAL_DIFFICULTY.thenComparing(CHAIN_HEIGHT); From 204d3e7b77b8d86a5d90e39741b7d37be697d47b Mon Sep 17 00:00:00 2001 From: "stefan.pingel@consensys.net" Date: Mon, 22 Jan 2024 18:10:11 +1000 Subject: [PATCH 2/4] only disconnect least usefull peer, and only if we have more than maxPeers connected Signed-off-by: stefan.pingel@consensys.net --- .../org/hyperledger/besu/RunnerBuilder.java | 2 +- .../besu/ethereum/eth/manager/EthPeers.java | 3 ++- .../AbstractRetryingSwitchingPeerTask.java | 9 ++++----- .../p2p/network/DefaultP2PNetwork.java | 14 +++++++------- .../besu/ethereum/p2p/rlpx/RlpxAgent.java | 18 +++++++++--------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index bb3172b7dee..8d0a693e27f 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -690,7 +690,7 @@ public Runner build() { .timestampForks(besuController.getGenesisConfigOptions().getForkBlockTimestamps()) .allConnectionsSupplier(ethPeers::getAllConnections) .allActiveConnectionsSupplier(ethPeers::getAllActiveConnections) - .peersLowerBound(ethPeers.getPeerLowerBound()) + .maxPeers(ethPeers.getMaxPeers()) .build(); }; diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java index aeadfa05cab..b0588b6e8f4 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java @@ -60,7 +60,8 @@ public class EthPeers { Comparator.comparing((final EthPeer p) -> p.chainState().getEstimatedHeight()); public static final Comparator MOST_USEFUL_PEER = - Comparator.comparing((final EthPeer p) -> p.getReputation().getScore()).thenComparing(CHAIN_HEIGHT); + Comparator.comparing((final EthPeer p) -> p.getReputation().getScore()) + .thenComparing(CHAIN_HEIGHT); public static final Comparator HEAVIEST_CHAIN = TOTAL_DIFFICULTY.thenComparing(CHAIN_HEIGHT); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java index 06c92c76204..3218532fdd4 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java @@ -137,16 +137,15 @@ private void refreshPeers() { // or the least useful if (peers.peerCount() >= peers.getMaxPeers()) { - failedPeers.stream() - .filter(peer -> !peer.isDisconnected()) - .findAny() - .or(() -> peers.streamAvailablePeers().min(peers.getBestChainComparator())) + failedPeers.stream().filter(peer -> !peer.isDisconnected()).findAny().stream() + .min(EthPeers.MOST_USEFUL_PEER) + .or(() -> peers.streamAvailablePeers().min(EthPeers.MOST_USEFUL_PEER)) .ifPresent( peer -> { LOG.atDebug() .setMessage( "Refresh peers disconnecting peer {}... Waiting for better peers. Current {} of max {}") - .addArgument(peer::getShortNodeId) + .addArgument(peer::toString) .addArgument(peers::peerCount) .addArgument(peers::getMaxPeers) .log(); diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java index ec65934b297..006366d8bdc 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java @@ -189,9 +189,9 @@ public class DefaultP2PNetwork implements P2PNetwork { this.vertx = vertx; // set the requirement here that the number of peers be greater than the lower bound - final int peerLowerBound = rlpxAgent.getPeerLowerBound(); - LOG.debug("setting peerLowerBound {}", peerLowerBound); - peerDiscoveryAgent.addPeerRequirement(() -> rlpxAgent.getConnectionCount() >= peerLowerBound); + final int maxPeers = rlpxAgent.getMaxPeers(); + LOG.debug("setting maxPeers {}", maxPeers); + peerDiscoveryAgent.addPeerRequirement(() -> rlpxAgent.getConnectionCount() >= maxPeers); subscribeDisconnect(reputationManager); } @@ -510,7 +510,7 @@ public static class Builder { private boolean legacyForkIdEnabled = false; private Supplier> allConnectionsSupplier; private Supplier> allActiveConnectionsSupplier; - private int peersLowerBound; + private int maxPeers; public P2PNetwork build() { validate(); @@ -588,7 +588,7 @@ private RlpxAgent createRlpxAgent( .p2pTLSConfiguration(p2pTLSConfiguration) .allConnectionsSupplier(allConnectionsSupplier) .allActiveConnectionsSupplier(allActiveConnectionsSupplier) - .peersLowerBound(peersLowerBound) + .maxPeers(maxPeers) .build(); } @@ -704,8 +704,8 @@ public Builder allActiveConnectionsSupplier( return this; } - public Builder peersLowerBound(final int peersLowerBound) { - this.peersLowerBound = peersLowerBound; + public Builder maxPeers(final int maxPeers) { + this.maxPeers = maxPeers; return this; } } diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java index 98a1f60df37..a024df62740 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java @@ -70,7 +70,7 @@ public class RlpxAgent { private final PeerPrivileges peerPrivileges; private final AtomicBoolean started = new AtomicBoolean(false); private final AtomicBoolean stopped = new AtomicBoolean(false); - private final int lowerBound; + private final int maxPeers; private final Supplier> allConnectionsSupplier; private final Supplier> allActiveConnectionsSupplier; private final Cache> peersConnectingCache = @@ -86,7 +86,7 @@ private RlpxAgent( final ConnectionInitializer connectionInitializer, final PeerRlpxPermissions peerPermissions, final PeerPrivileges peerPrivileges, - final int peersLowerBound, + final int maxPeers, final Supplier> allConnectionsSupplier, final Supplier> allActiveConnectionsSupplier) { this.localNode = localNode; @@ -94,7 +94,7 @@ private RlpxAgent( this.connectionInitializer = connectionInitializer; this.peerPermissions = peerPermissions; this.peerPrivileges = peerPrivileges; - this.lowerBound = peersLowerBound; + this.maxPeers = maxPeers; this.allConnectionsSupplier = allConnectionsSupplier; this.allActiveConnectionsSupplier = allActiveConnectionsSupplier; } @@ -363,8 +363,8 @@ public ConcurrentMap> getMapOfCompletab return peersConnectingCache.asMap(); } - public int getPeerLowerBound() { - return lowerBound; + public int getMaxPeers() { + return maxPeers; } public static class Builder { @@ -379,7 +379,7 @@ public static class Builder { private Optional p2pTLSConfiguration; private Supplier> allConnectionsSupplier; private Supplier> allActiveConnectionsSupplier; - private int peersLowerBound; + private int maxPeers; private Builder() {} @@ -416,7 +416,7 @@ public RlpxAgent build() { connectionInitializer, rlpxPermissions, peerPrivileges, - peersLowerBound, + maxPeers, allConnectionsSupplier, allActiveConnectionsSupplier); } @@ -495,8 +495,8 @@ public Builder allActiveConnectionsSupplier( return this; } - public Builder peersLowerBound(final int peersLowerBound) { - this.peersLowerBound = peersLowerBound; + public Builder maxPeers(final int maxPeers) { + this.maxPeers = maxPeers; return this; } } From 5cad8c8d1f0f179798989f8cdd336ede517c65a3 Mon Sep 17 00:00:00 2001 From: "stefan.pingel@consensys.net" Date: Tue, 30 Jan 2024 17:18:53 +1000 Subject: [PATCH 3/4] remove outdated comment Signed-off-by: stefan.pingel@consensys.net --- .../hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java index 8c139518882..eee530268db 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java @@ -189,7 +189,6 @@ public class DefaultP2PNetwork implements P2PNetwork { this.peerPermissions = peerPermissions; this.vertx = vertx; - // set the requirement here that the number of peers be greater than the lower bound final int maxPeers = rlpxAgent.getMaxPeers(); LOG.debug("setting maxPeers {}", maxPeers); peerDiscoveryAgent.addPeerRequirement(() -> rlpxAgent.getConnectionCount() >= maxPeers); From 549c8e0ddb3f2f107312512cb5a9be759b9997e0 Mon Sep 17 00:00:00 2001 From: Stefan Pingel <16143240+pinges@users.noreply.github.com> Date: Wed, 31 Jan 2024 11:56:26 +1000 Subject: [PATCH 4/4] Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java Co-authored-by: Sally MacFarlane Signed-off-by: Stefan Pingel <16143240+pinges@users.noreply.github.com> --- .../eth/manager/task/AbstractRetryingSwitchingPeerTask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java index 3218532fdd4..b6f7b892902 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractRetryingSwitchingPeerTask.java @@ -144,7 +144,7 @@ private void refreshPeers() { peer -> { LOG.atDebug() .setMessage( - "Refresh peers disconnecting peer {}... Waiting for better peers. Current {} of max {}") + "Refresh peers disconnecting peer {} Waiting for better peers. Current {} of max {}") .addArgument(peer::toString) .addArgument(peers::peerCount) .addArgument(peers::getMaxPeers)