|
|
|
@ -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; |
|
|
|
@ -30,21 +32,34 @@ import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
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 = 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; |
|
|
|
|
static final long USELESS_RESPONSE_WINDOW_IN_MILLIS = |
|
|
|
|
TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES); |
|
|
|
|
|
|
|
|
|
private final ConcurrentMap<Integer, AtomicInteger> timeoutCountByRequestType = |
|
|
|
|
new ConcurrentHashMap<>(); |
|
|
|
|
private final Queue<Long> uselessResponseTimes = new ConcurrentLinkedQueue<>(); |
|
|
|
|
|
|
|
|
|
private static final int DEFAULT_SCORE = 100; |
|
|
|
|
private static final int SMALL_ADJUSTMENT = 1; |
|
|
|
|
private static final int LARGE_ADJUSTMENT = 10; |
|
|
|
|
|
|
|
|
|
private long score = DEFAULT_SCORE; |
|
|
|
|
private int score; |
|
|
|
|
|
|
|
|
|
private final int maxScore; |
|
|
|
|
|
|
|
|
|
public PeerReputation() { |
|
|
|
|
this(DEFAULT_INITIAL_SCORE, DEFAULT_MAX_SCORE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 = initialScore; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Optional<DisconnectReason> recordRequestTimeout(final int requestCode) { |
|
|
|
|
final int newTimeoutCount = getOrCreateTimeoutCount(requestCode).incrementAndGet(); |
|
|
|
@ -86,7 +101,9 @@ public class PeerReputation implements Comparable<PeerReputation> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void recordUsefulResponse() { |
|
|
|
|
score += SMALL_ADJUSTMENT; |
|
|
|
|
if (score < maxScore) { |
|
|
|
|
score = Math.min(maxScore, score + SMALL_ADJUSTMENT); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean shouldRemove(final Long timestamp, final long currentTimestamp) { |
|
|
|
@ -100,6 +117,10 @@ public class PeerReputation implements Comparable<PeerReputation> { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public int compareTo(final @Nonnull PeerReputation otherReputation) { |
|
|
|
|
return Long.compare(this.score, otherReputation.score); |
|
|
|
|
return Integer.compare(this.score, otherReputation.score); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int getScore() { |
|
|
|
|
return score; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|