From 694bab4ad0872631b92a7e02e919b7b56214deb8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 2 Mar 2017 18:09:11 -0800 Subject: [PATCH] net: less strict bip152 behavior. --- lib/net/peer.js | 34 +++++++++++++++++++++++++++------- lib/net/pool.js | 11 ++++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/net/peer.js b/lib/net/peer.js index e625632e..527bc01f 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -1480,10 +1480,8 @@ Peer.prototype.blockType = function blockType() { if (this.options.spv) return invTypes.FILTERED_BLOCK; - if (this.options.compact && this.compactMode !== -1) { - if (!this.options.hasWitness() || this.compactWitness) - return invTypes.CMPCT_BLOCK; - } + if (this.options.compact && this.hasCompact()) + return invTypes.CMPCT_BLOCK; if (this.hasWitness()) return invTypes.WITNESS_BLOCK; @@ -1734,8 +1732,11 @@ Peer.prototype.handleVersion = co(function* handleVersion(packet) { } if (this.options.compact) { - if (!this.hasCompact()) - throw new Error('Peer does not support compact blocks.'); + if (!this.hasCompactSupport()) { + this.logger.debug( + 'Peer does not support compact blocks (%s).', + this.hostname()); + } } } @@ -2209,7 +2210,7 @@ Peer.prototype.hasWitness = function hasWitness() { * @returns {Boolean} */ -Peer.prototype.hasCompact = function hasCompact() { +Peer.prototype.hasCompactSupport = function hasCompactSupport() { if (this.version < common.COMPACT_VERSION) return false; @@ -2222,6 +2223,25 @@ Peer.prototype.hasCompact = function hasCompact() { return this.version >= common.COMPACT_WITNESS_VERSION; }; +/** + * Test whether the peer sent us a + * compatible compact block handshake. + * @returns {Boolean} + */ + +Peer.prototype.hasCompact = function hasCompact() { + if (this.compactMode === -1) + return false; + + if (!this.options.hasWitness()) + return true; + + if (!this.compactWitness) + return false; + + return true; +}; + /** * Inspect the peer. * @returns {String} diff --git a/lib/net/pool.js b/lib/net/pool.js index c044f539..60cbcb5b 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1103,7 +1103,7 @@ Pool.prototype.handleOpen = co(function* handleOpen(peer) { // We want compact blocks! if (this.options.compact) { - if (peer.hasCompact()) + if (peer.hasCompactSupport()) peer.sendCompact(this.options.blockMode); } @@ -2435,6 +2435,15 @@ Pool.prototype.handleCmpctBlock = co(function* handleCmpctBlock(peer, packet) { this.logger.info( 'Peer sent unsolicited cmpctblock (%s).', peer.hostname()); + this.destroy(); + return; + } + + if (!peer.hasCompactSupport()) { + this.logger.info( + 'Peer sent unsolicited cmpctblock (%s).', + peer.hostname()); + this.destroy(); return; }