diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index bed6b847..ac440527 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -231,75 +231,8 @@ Peer.prototype._onConnect = function _onConnect() { this._connectTimeout = null; } - this.request('verack', function callee(err) { - if (err) { - self._error(err); - self.destroy(); - return; - } - - // Wait for _their_ version. - if (!self.version) { - self.logger.debug( - 'Peer sent a verack without a version (%s).', - self.hostname); - self.request('version', callee); - return; - } - - self.ack = true; - self.ts = utils.now(); - - // Setup the ping interval. - self.ping.timer = setInterval(function() { - self.sendPing(); - }, self.ping.interval); - - // Ask for headers-only. - if (self.options.headers) { - if (self.version.version > 70012) - self.write(self.framer.sendHeaders()); - } - - // Let them know we support segwit (old - // segwit3 nodes require this instead - // of service bits). - if (self.options.witness) { - if (self.version.version >= 70012) - self.write(self.framer.haveWitness()); - } - - // We want compact blocks! - if (self.options.compact && self.version.hasCompact()) - self.sendCompact(); - - // Find some more peers. - self.write(self.framer.getAddr()); - - // Relay our spv filter if we have one. - self.updateWatch(); - - // Announce our currently broadcasted items. - self.announce(self.pool.inv.items); - - // Set a fee rate filter. - if (self.pool.feeRate !== -1) - self.setFeeRate(self.pool.feeRate); - - // Start syncing the chain. - self.sync(); - - // Ask for the mempool if we're synced. - if (self.network.requestMempool) { - if (self.loader && self.pool.synced) - self.sendMempool(); - } - - self.logger.debug('Received verack (%s).', self.hostname); - - // Finally we can let the pool know - // that this peer is ready to go. - self.emit('ack'); + this.request('verack', function(err) { + self._onAck(err); }); // Say hello. @@ -313,6 +246,84 @@ Peer.prototype._onConnect = function _onConnect() { } }; +/** + * Handle `ack` event (called on verack). + * @private + */ + +Peer.prototype._onAck = function _onAck(err) { + if (err) { + this._error(err); + this.destroy(); + return; + } + + // Wait for _their_ version. + if (!this.version) { + this.logger.debug( + 'Peer sent a verack without a version (%s).', + this.hostname); + this.request('version', this._onAck.bind(this)); + return; + } + + this.ack = true; + this.ts = utils.now(); + + // Setup the ping interval. + this.ping.timer = setInterval(function() { + self.sendPing(); + }, this.ping.interval); + + // Ask for headers-only. + if (this.options.headers) { + if (this.version.version >= 70012) + this.write(this.framer.sendHeaders()); + } + + // Let them know we support segwit (old + // segwit3 nodes require this instead + // of service bits). + if (this.options.witness) { + if (this.version.version >= 70012) + this.write(this.framer.haveWitness()); + } + + // We want compact blocks! + if (this.options.compact) { + if (this.version.version >= 70014) + this.sendCompact(); + } + + // Find some more peers. + this.write(this.framer.getAddr()); + + // Relay our spv filter if we have one. + this.updateWatch(); + + // Announce our currently broadcasted items. + this.announce(this.pool.inv.items); + + // Set a fee rate filter. + if (this.pool.feeRate !== -1) + this.setFeeRate(this.pool.feeRate); + + // Start syncing the chain. + this.sync(); + + // Ask for the mempool if we're synced. + if (this.network.requestMempool) { + if (this.loader && this.pool.synced) + this.sendMempool(); + } + + this.logger.debug('Received verack (%s).', this.hostname); + + // Finally we can let the pool know + // that this peer is ready to go. + this.emit('ack'); +}; + /** * Create the socket and begin connecting. This method * will use `options.createSocket` if provided. @@ -1393,7 +1404,7 @@ Peer.prototype._getItem = function _getItem(item, callback) { return callback(null, entry.msg); } - if (this.options.selfish) + if (this.pool.options.selfish) return callback(); if (item.isTX()) { @@ -1896,7 +1907,7 @@ Peer.prototype._handleGetBlockTxn = function _handleGetBlockTxn(payload) { if (this.chain.db.options.prune) return done(); - if (this.options.selfish) + if (this.pool.options.selfish) return done(); this.chain.db.getBlock(payload.hash, function(err, block) { @@ -2014,6 +2025,16 @@ Peer.prototype.sendGetBlocks = function getBlocks(locator, stop) { */ Peer.prototype.sendMempool = function sendMempool() { + if (!this.version) + return; + + if (!this.version.hasBloom()) { + this.logger.debug( + 'Cannot request mempool for non-bloom peer (%s).', + this.hostname); + return; + } + this.logger.debug( 'Requesting inv packet from peer with mempool (%s).', this.hostname);