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

Useful response improves reputation #4130

Merged
merged 3 commits into from
Jul 20, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ public void recordUselessResponse(final String requestType) {
reputation.recordUselessResponse(System.currentTimeMillis()).ifPresent(this::disconnect);
}

public void recordUsefulResponse() {
reputation.recordUsefulResposne();
}

public void disconnect(final DisconnectReason reason) {
connection.disconnect(reason);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.ethereum.eth.manager.EthPeer.DisconnectCallback;
import org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator;
import org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.permissioning.NodeMessagePermissioningProvider;
Expand Down Expand Up @@ -115,6 +116,12 @@ public void registerDisconnect(final PeerConnection connection) {
disconnectCallbacks.forEach(callback -> callback.onDisconnect(peer));
peer.handleDisconnect();
abortPendingRequestsAssignedToDisconnectedPeers();
final PeerInfo peerInfo = peer.getConnection().getPeerInfo();
LOG.debug(
"Disconnected EthPeer {}, client ID: {}, {}",
peerInfo.getNodeId(),
peerInfo.getClientId(),
peer.getReputation());
}
reattemptPendingPeerRequests();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class PeerReputation implements Comparable<PeerReputation> {
private static final int SMALL_ADJUSTMENT = 1;
private static final int LARGE_ADJUSTMENT = 10;

private int score = DEFAULT_SCORE;
private long score = DEFAULT_SCORE;

public Optional<DisconnectReason> recordRequestTimeout(final int requestCode) {
final int newTimeoutCount = getOrCreateTimeoutCount(requestCode).incrementAndGet();
Expand Down Expand Up @@ -85,6 +85,10 @@ public Optional<DisconnectReason> recordUselessResponse(final long timestamp) {
}
}

public void recordUsefulResposne() {
score += SMALL_ADJUSTMENT;
}

private boolean shouldRemove(final Long timestamp, final long currentTimestamp) {
return timestamp != null && timestamp + USELESS_RESPONSE_WINDOW_IN_MILLIS < currentTimestamp;
}
Expand All @@ -96,6 +100,6 @@ public String toString() {

@Override
public int compareTo(final @Nonnull PeerReputation otherReputation) {
return Integer.compare(this.score, otherReputation.score);
return Long.compare(this.score, otherReputation.score);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ private void handleMessage(
}
try {
final Optional<R> result = processResponse(streamClosed, message, peer);
result.ifPresent(promise::complete);
result.ifPresent(
r -> {
promise.complete(r);
peer.recordUsefulResponse();
});
} catch (final RLPException e) {
// Peer sent us malformed data - disconnect
LOG.debug("Disconnecting with BREACH_OF_PROTOCOL due to malformed message: {}", peer, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ public void compareTo_withDifferentNodeId() {
assertThat(peer2.compareTo(peer1)).isEqualTo(-1);
}

@Test
public void recordUsefullResponse() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/Usefull/Useful

final EthPeer peer = createPeer();
final EthPeer peer2 = createPeer();
peer.recordUsefulResponse();
assertThat(peer.getReputation().compareTo(peer2.getReputation())).isGreaterThan(0);
}

private void messageStream(
final ResponseStreamSupplier getStream,
final MessageData targetMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,10 @@ public void shouldDiscardEmptyResponseRecordsAfterTimeWindowElapses() {
1001 + PeerReputation.USELESS_RESPONSE_WINDOW_IN_MILLIS + 1))
.isEmpty();
}

@Test
public void shouldIncreaseScore() {
reputation.recordUsefulResposne();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

assertThat(reputation.compareTo(new PeerReputation())).isGreaterThan(0);
}
}