From f0f9f798c5881072ac571d6460f656c6cea26b8e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 20 Jan 2017 17:23:14 -0800 Subject: [PATCH] pool: fix inv. do not hold lock for bip151 msgs. --- lib/net/peer.js | 52 +++++++++++++++++++++++++++++++++++++------------ lib/net/pool.js | 4 ++-- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/net/peer.js b/lib/net/peer.js index dd289cc0..9c0f56f6 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -669,7 +669,7 @@ Peer.prototype.announceBlock = function announceBlock(blocks) { return; } - this.sendInv(inv); + this.queueInv(inv); }; /** @@ -722,7 +722,7 @@ Peer.prototype.announceTX = function announceTX(txs) { inv.push(tx.toInv()); } - this.sendInv(inv); + this.queueInv(inv); }; /** @@ -730,7 +730,7 @@ Peer.prototype.announceTX = function announceTX(txs) { * @param {InvItem[]} items */ -Peer.prototype.sendInv = function sendInv(items) { +Peer.prototype.queueInv = function queueInv(items) { var hasBlock = false; var i, item; @@ -792,11 +792,30 @@ Peer.prototype.flushInv = function flushInv() { * @param {InvItem[]} items */ -Peer.prototype.forceInv = function forceInv(items) { +Peer.prototype.sendInv = function sendInv(items) { + var i, item, chunk; + + if (!this.handshake) + return; + + if (this.destroyed) + return; + if (!Array.isArray(items)) items = [items]; - this.send(new packets.InvPacket(items)); + for (i = 0; i < items.length; i++) { + item = items[i]; + this.invFilter.add(item.hash, 'hex'); + } + + if (items.length === 0) + return; + + for (i = 0; i < items.length; i += 1000) { + chunk = items.slice(i, i + 1000); + this.send(new packets.InvPacket(chunk)); + } }; /** @@ -1378,13 +1397,22 @@ Peer.prototype.getTX = function getTX(hashes) { */ Peer.prototype.readPacket = co(function* readPacket(packet) { - var unlock = yield this.locker.lock(); - try { - this.socket.pause(); - return yield this.handlePacket(packet); - } finally { - this.socket.resume(); - unlock(); + var unlock; + + switch (packet.type) { + case packetTypes.ENCINIT: + case packetTypes.ENCACK: + return yield this.handlePacket(packet); + default: + unlock = yield this.locker.lock(); + try { + this.socket.pause(); + return yield this.handlePacket(packet); + } finally { + this.socket.resume(); + unlock(); + } + break; } }); diff --git a/lib/net/pool.js b/lib/net/pool.js index d3b2d272..4e46a795 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1356,7 +1356,7 @@ Pool.prototype.handleGetData = co(function* handleGetData(peer, packet) { } if (item.hash === peer.hashContinue) { - peer.forceInv([new InvItem(invTypes.BLOCK, this.chain.tip.hash)]); + peer.sendInv([new InvItem(invTypes.BLOCK, this.chain.tip.hash)]); peer.hashContinue = null; } } @@ -1943,7 +1943,7 @@ Pool.prototype.handleMempool = co(function* handleMempool(peer, packet) { this.logger.debug('Sending mempool snapshot (%s).', peer.hostname); - peer.sendInv(items); + peer.queueInv(items); this.emit('mempool', packet, peer); });