Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect worst peer #6443

Merged
merged 9 commits into from
Jan 31, 2024
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);
pinges marked this conversation as resolved.
Show resolved Hide resolved

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
Loading