From a4201e6bd5ea9380978b6c8498d30ef0f3bc4184 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 28 Feb 2017 10:08:17 -0800 Subject: [PATCH] peer: better stall detection. --- lib/net/peer.js | 12 ++++++++++++ lib/net/pool.js | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/net/peer.js b/lib/net/peer.js index 876a595f..75fda90e 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -1322,6 +1322,18 @@ Peer.prototype.maybeTimeout = function maybeTimeout() { this.destroy(); return; } + + keys = this.requestMap.keys(); + + for (i = 0; i < keys.length; i++) { + key = keys[i]; + entry = this.requestMap.get(key); + if (now > entry + Peer.BLOCK_TIMEOUT) { + this.error('Peer is stalling (block).'); + this.destroy(); + return; + } + } } }; diff --git a/lib/net/pool.js b/lib/net/pool.js index 84fbf5f9..942b47eb 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -2455,7 +2455,7 @@ Pool.prototype.handleCmpctBlock = co(function* handleCmpctBlock(peer, packet) { peer.destroy(); return; } - peer.requestMap.insert(hash); + peer.requestMap.set(hash, util.ms()); assert(!this.requestMap.has(hash)); this.requestMap.insert(hash); } @@ -3012,6 +3012,7 @@ Pool.prototype.getBlocks = co(function* getBlocks(peer, tip, stop) { */ Pool.prototype.getBlock = function getBlock(peer, hashes) { + var now = util.ms(); var items = []; var i, hash; @@ -3031,7 +3032,7 @@ Pool.prototype.getBlock = function getBlock(peer, hashes) { continue; this.requestMap.insert(hash); - peer.requestMap.insert(hash); + peer.requestMap.set(hash, now); items.push(hash); } @@ -3055,6 +3056,7 @@ Pool.prototype.getBlock = function getBlock(peer, hashes) { */ Pool.prototype.getTX = function getTX(peer, hashes) { + var now = util.ms(); var items = []; var i, hash; @@ -3074,7 +3076,7 @@ Pool.prototype.getTX = function getTX(peer, hashes) { continue; this.requestMap.insert(hash); - peer.requestMap.insert(hash); + peer.requestMap.set(hash, now); items.push(hash); }