Skip to content

Commit

Permalink
Disconnect worst peer (#6443)
Browse files Browse the repository at this point in the history
* When refreshing, only disconnect a peer if we have max peers
* If we are disconnecting a peer, disconnect the least useful peer 

Signed-off-by: [email protected] <[email protected]>
Signed-off-by: Stefan Pingel <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
  • Loading branch information
pinges and macfarla authored Jan 31, 2024
1 parent 79245bb commit 497faef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ public Runner build() {
.timestampForks(besuController.getGenesisConfigOptions().getForkBlockTimestamps())
.allConnectionsSupplier(ethPeers::getAllConnections)
.allActiveConnectionsSupplier(ethPeers::getAllActiveConnections)
.peersLowerBound(ethPeers.getPeerLowerBound())
.maxPeers(ethPeers.getMaxPeers())
.build();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@
public class EthPeers {
private static final Logger LOG = LoggerFactory.getLogger(EthPeers.class);
public static final Comparator<EthPeer> TOTAL_DIFFICULTY =
Comparator.comparing(((final EthPeer p) -> p.chainState().getEstimatedTotalDifficulty()));
Comparator.comparing((final EthPeer p) -> p.chainState().getEstimatedTotalDifficulty());

public static final Comparator<EthPeer> CHAIN_HEIGHT =
Comparator.comparing(((final EthPeer p) -> p.chainState().getEstimatedHeight()));
Comparator.comparing((final EthPeer p) -> p.chainState().getEstimatedHeight());

public static final Comparator<EthPeer> MOST_USEFUL_PEER =
Comparator.comparing((final EthPeer p) -> p.getReputation().getScore())
.thenComparing(CHAIN_HEIGHT);

public static final Comparator<EthPeer> HEAVIEST_CHAIN =
TOTAL_DIFFICULTY.thenComparing(CHAIN_HEIGHT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,14 @@ 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 {}")
"Refresh peers disconnecting peer {} Waiting for better peers. Current {} of max {}")
.addArgument(peer::getLoggableId)
.addArgument(peers::peerCount)
.addArgument(peers::getMaxPeers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,9 @@ 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 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);
}

Expand Down Expand Up @@ -512,7 +511,7 @@ public static class Builder {
private boolean legacyForkIdEnabled = false;
private Supplier<Stream<PeerConnection>> allConnectionsSupplier;
private Supplier<Stream<PeerConnection>> allActiveConnectionsSupplier;
private int peersLowerBound;
private int maxPeers;
private PeerTable peerTable;

public P2PNetwork build() {
Expand Down Expand Up @@ -593,7 +592,7 @@ private RlpxAgent createRlpxAgent(
.p2pTLSConfiguration(p2pTLSConfiguration)
.allConnectionsSupplier(allConnectionsSupplier)
.allActiveConnectionsSupplier(allActiveConnectionsSupplier)
.peersLowerBound(peersLowerBound)
.maxPeers(maxPeers)
.peerTable(peerTable)
.build();
}
Expand Down Expand Up @@ -710,8 +709,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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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<Stream<PeerConnection>> allConnectionsSupplier;
private final Supplier<Stream<PeerConnection>> allActiveConnectionsSupplier;
private final Cache<Bytes, CompletableFuture<PeerConnection>> peersConnectingCache =
Expand All @@ -87,15 +87,15 @@ private RlpxAgent(
final ConnectionInitializer connectionInitializer,
final PeerRlpxPermissions peerPermissions,
final PeerPrivileges peerPrivileges,
final int peersLowerBound,
final int maxPeers,
final Supplier<Stream<PeerConnection>> allConnectionsSupplier,
final Supplier<Stream<PeerConnection>> allActiveConnectionsSupplier) {
this.localNode = localNode;
this.connectionEvents = connectionEvents;
this.connectionInitializer = connectionInitializer;
this.peerPermissions = peerPermissions;
this.peerPrivileges = peerPrivileges;
this.lowerBound = peersLowerBound;
this.maxPeers = maxPeers;
this.allConnectionsSupplier = allConnectionsSupplier;
this.allActiveConnectionsSupplier = allActiveConnectionsSupplier;
}
Expand Down Expand Up @@ -358,8 +358,8 @@ public ConcurrentMap<Bytes, CompletableFuture<PeerConnection>> getMapOfCompletab
return peersConnectingCache.asMap();
}

public int getPeerLowerBound() {
return lowerBound;
public int getMaxPeers() {
return maxPeers;
}

public static class Builder {
Expand All @@ -374,7 +374,7 @@ public static class Builder {
private Optional<TLSConfiguration> p2pTLSConfiguration;
private Supplier<Stream<PeerConnection>> allConnectionsSupplier;
private Supplier<Stream<PeerConnection>> allActiveConnectionsSupplier;
private int peersLowerBound;
private int maxPeers;
private PeerTable peerTable;

private Builder() {}
Expand Down Expand Up @@ -413,7 +413,7 @@ public RlpxAgent build() {
connectionInitializer,
rlpxPermissions,
peerPrivileges,
peersLowerBound,
maxPeers,
allConnectionsSupplier,
allActiveConnectionsSupplier);
}
Expand Down Expand Up @@ -492,8 +492,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;
}

Expand Down

0 comments on commit 497faef

Please sign in to comment.