Skip to content

Commit

Permalink
Peer: Disconnect remote peers which repeatedly don't respond to pings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvyhnal-generalbytes authored and Andreas Schildbach committed Mar 11, 2019
1 parent ce9801f commit 29f450b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/org/bitcoinj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public GetDataRequest(Sha256Hash hash, SettableFuture future) {
private final ReentrantLock lastPingTimesLock = new ReentrantLock();
@GuardedBy("lastPingTimesLock") private long[] lastPingTimes = null;
private final CopyOnWriteArrayList<PendingPing> pendingPings;
// Disconnect from a peer that is not responding to Pings
private static final int PENDING_PINGS_LIMIT = 50;
private static final int PING_MOVING_AVERAGE_WINDOW = 20;

private volatile VersionMessage vPeerVersionMessage;
Expand Down Expand Up @@ -1552,6 +1554,10 @@ protected ListenableFuture<Long> ping(long nonce) throws ProtocolException {
final VersionMessage ver = vPeerVersionMessage;
if (!ver.isPingPongSupported())
throw new ProtocolException("Peer version is too low for measurable pings: " + ver);
if (pendingPings.size() > PENDING_PINGS_LIMIT) {
log.info("{}: Too many pending pings, disconnecting", this);
close();
}
PendingPing pendingPing = new PendingPing(nonce);
pendingPings.add(pendingPing);
sendMessage(new Ping(pendingPing.nonce));
Expand Down

0 comments on commit 29f450b

Please sign in to comment.