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
Cherry pick bitcoinj@29f450b
  • Loading branch information
pvyhnal-generalbytes authored and oscarguindzberg committed Nov 23, 2020
1 parent b9d95ce commit f9bfede
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 @@ -155,6 +155,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 @@ -1560,6 +1562,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 f9bfede

Please sign in to comment.