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

Introduce a cap to reputation score increase #4230

Merged
merged 6 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.eth.manager;

import static com.google.common.base.Preconditions.checkArgument;

import org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason;

import java.util.Map;
Expand All @@ -32,7 +34,8 @@
public class PeerReputation implements Comparable<PeerReputation> {
static final long USELESS_RESPONSE_WINDOW_IN_MILLIS =
TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);
static final int DEFAULT_MAX_SCORE = 100;
static final int DEFAULT_MAX_SCORE = 150;
static final int DEFAULT_INITIAL_SCORE = 100;
private static final Logger LOG = LoggerFactory.getLogger(PeerReputation.class);
private static final int TIMEOUT_THRESHOLD = 3;
private static final int USELESS_RESPONSE_THRESHOLD = 5;
Expand All @@ -49,12 +52,13 @@ public class PeerReputation implements Comparable<PeerReputation> {
private final int maxScore;

public PeerReputation() {
this(DEFAULT_MAX_SCORE);
this(DEFAULT_INITIAL_SCORE, DEFAULT_MAX_SCORE);
}

public PeerReputation(final int maxScore) {
public PeerReputation(final int initialScore, final int maxScore) {
checkArgument(initialScore <= maxScore, "Inital score must be less than or equal to max score");
this.maxScore = maxScore;
this.score = maxScore;
this.score = initialScore;
}

public Optional<DisconnectReason> recordRequestTimeout(final int requestCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@

public class PeerReputationTest {

private static final int INITIAL_SCORE = 25;
private static final int MAX_SCORE = 50;
private final PeerReputation reputation = new PeerReputation(MAX_SCORE);
private final PeerReputation reputation = new PeerReputation(INITIAL_SCORE, MAX_SCORE);

@Test(expected = java.lang.IllegalArgumentException.class)
public void shouldThrowOnInvalidInitialScore() {
new PeerReputation(2, 1);
fab-10 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void shouldOnlyDisconnectWhenTimeoutLimitReached() {
Expand Down Expand Up @@ -84,15 +90,15 @@ public void shouldDiscardEmptyResponseRecordsAfterTimeWindowElapses() {

@Test
public void shouldIncreaseScore() {
reputation.recordUselessResponse(1001);
final int scoreDecreased = reputation.getScore();
reputation.recordUsefulResponse();
assertThat(reputation.getScore()).isGreaterThan(scoreDecreased);
assertThat(reputation.getScore()).isGreaterThan(INITIAL_SCORE);
}

@Test
public void shouldNotIncreaseScoreOverMax() {
reputation.recordUsefulResponse();
for (int i = 0; i <= MAX_SCORE + 1; i++) {
reputation.recordUsefulResponse();
}
assertThat(reputation.getScore()).isEqualTo(MAX_SCORE);
}
}