Skip to content

Commit

Permalink
Useful response improves reputation (hyperledger#4130)
Browse files Browse the repository at this point in the history
* useful response improves reputation

Signed-off-by: Stefan <stefan.pingel@consensys.net>
  • Loading branch information
pinges authored Jul 20, 2022
1 parent 0ca45f3 commit b7877be
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
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() {
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();
assertThat(reputation.compareTo(new PeerReputation())).isGreaterThan(0);
}
}

0 comments on commit b7877be

Please sign in to comment.