From 9d771fe0b60a6b40c1373fb114abd153e7f976b0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 15 Jun 2016 20:33:17 -0700 Subject: [PATCH] fix leech peers. --- lib/bcoin/peer.js | 28 +++++++++++++++++++--------- lib/bcoin/pool.js | 6 ++---- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index ad54b2d1..7c802180 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -112,6 +112,7 @@ function Peer(pool, options) { this.socket = options.socket; this.host = IP.normalize(this.socket.remoteAddress); this.port = this.socket.remotePort; + this.connected = true; } else if (options.host) { this.host = options.host.host; this.port = options.host.port; @@ -158,14 +159,7 @@ Peer.prototype._init = function init() { var self = this; this.socket.once('connect', function() { - self.ts = utils.now(); - self.connected = true; - self.emit('connect'); - if (self._connectTimeout != null) { - clearTimeout(self._connectTimeout); - self._connectTimeout = null; - } - self._onconnect(); + self._onConnect(); }); this.socket.once('error', function(err) { @@ -190,11 +184,27 @@ Peer.prototype._init = function init() { self.reject(null, 'malformed', 'error parsing message', 10); self._error(err, true); }); + + if (this.connected) { + utils.nextTick(function() { + self._onConnect(); + }); + } }; -Peer.prototype._onconnect = function _onconnect() { +Peer.prototype._onConnect = function _onConnect() { var self = this; + this.ts = utils.now(); + this.connected = true; + + this.emit('connect'); + + if (this._connectTimeout != null) { + clearTimeout(this._connectTimeout); + this._connectTimeout = null; + } + this.request('verack', function callee(err) { if (err) { self._error(err); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 31e27a29..1a0fc5a2 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -839,8 +839,7 @@ Pool.prototype._createPeer = function _createPeer(options) { network: options.network, spv: options.spv, witness: options.witness, - headers: this.options.headers, - ts: options.ts + headers: this.options.headers }); peer.once('close', function() { @@ -1122,8 +1121,7 @@ Pool.prototype._addLeech = function _addLeech(socket) { socket: socket, network: false, spv: false, - witness: false, - ts: utils.now() + witness: false }); bcoin.debug('Added leech peer (%s).', peer.hostname);