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

Peering - compare incoming connection #7617

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Breaking Changes

### Additions and Improvements
- Peering - compare incoming peer with current peers before deciding which to keep [#6968](https://github.com/hyperledger/besu/pull/6968)
- Remove privacy test classes support [#7569](https://github.com/hyperledger/besu/pull/7569)

### Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,42 @@ private boolean alreadyConnectedOrConnecting(final boolean inbound, final Bytes
.anyMatch(c -> !c.isDisconnected() && (!inbound || (inbound && c.inboundInitiated())));
}

public void disconnectWorstUselessPeer() {
streamAvailablePeers()
.filter(p -> !canExceedPeerLimits(p.getId()))
.min(getBestPeerComparator())
.ifPresent(
peer -> {
LOG.atDebug()
.setMessage(
"disconnecting peer {}. Waiting for better peers. Current {} of max {}")
.addArgument(peer::getLoggableId)
.addArgument(this::peerCount)
.addArgument(this::getMaxPeers)
.log();
peer.disconnect(DisconnectMessage.DisconnectReason.USELESS_PEER_BY_CHAIN_COMPARATOR);
});
public void disconnectWorstUselessPeerIfAtCapacityIncludingConnectingPeer(
final EthPeer connectingPeer) {
if (peerCount() >= getMaxPeers()) {
streamAvailablePeers()
.filter(p -> !canExceedPeerLimits(p.getId()))
.min(getBestPeerComparator())
.ifPresent(
worstCurrentPeer -> {
// TODO remove this debug log
LOG.atDebug()
.setMessage("comparing worstCurrentPeer {} with connectingPeer {}")
.addArgument(worstCurrentPeer)
.addArgument(connectingPeer)
.log();
if (getBestPeerComparator().compare(worstCurrentPeer, connectingPeer) < 0) {
LOG.atDebug()
.setMessage(
"disconnecting current peer {}. Waiting for better peers. Current {} of max {}")
.addArgument(worstCurrentPeer::getLoggableId)
.addArgument(this::peerCount)
.addArgument(this::getMaxPeers)
.log();
worstCurrentPeer.disconnect(
DisconnectMessage.DisconnectReason.USELESS_PEER_BY_CHAIN_COMPARATOR);
} else {
LOG.atDebug()
.setMessage(
"disconnecting connecting peer {}. Waiting for better peers. Current {} of max {}")
.addArgument(connectingPeer::getLoggableId)
.addArgument(this::peerCount)
.addArgument(this::getMaxPeers)
.log();
connectingPeer.disconnect(DisconnectMessage.DisconnectReason.TOO_MANY_PEERS);
}
});
}
}

public void setChainHeadTracker(final ChainHeadTracker tracker) {
Expand Down Expand Up @@ -739,14 +760,8 @@ boolean addPeerToEthPeers(final EthPeer peer) {
.log();
}));
} else {
LOG.atTrace()
.setMessage(
"Too many peers. Disconnect peer {} with connection: {}, max connections {}")
.addArgument(peer.getLoggableId())
.addArgument(connection)
.addArgument(peerUpperBound)
.log();
connection.disconnect(DisconnectMessage.DisconnectReason.TOO_MANY_PEERS);
// Compare the incoming peer to the worst current peer and disconnect worst of those
disconnectWorstUselessPeerIfAtCapacityIncludingConnectingPeer(peer);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ protected CompletableFuture<Optional<EthPeer>> selectBestAvailableSyncTarget() {
pivotBlockHeader.getNumber(),
ethPeers.peerCount(),
ethPeers.getMaxPeers());
ethPeers.disconnectWorstUselessPeer();
return completedFuture(Optional.empty());
} else {
return confirmPivotBlockHeader(bestPeer);
Expand Down
Loading