fix leech peers.

This commit is contained in:
Christopher Jeffrey 2016-06-15 20:33:17 -07:00
parent d38de71468
commit 9d771fe0b6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 21 additions and 13 deletions

View File

@ -112,6 +112,7 @@ function Peer(pool, options) {
this.socket = options.socket; this.socket = options.socket;
this.host = IP.normalize(this.socket.remoteAddress); this.host = IP.normalize(this.socket.remoteAddress);
this.port = this.socket.remotePort; this.port = this.socket.remotePort;
this.connected = true;
} else if (options.host) { } else if (options.host) {
this.host = options.host.host; this.host = options.host.host;
this.port = options.host.port; this.port = options.host.port;
@ -158,14 +159,7 @@ Peer.prototype._init = function init() {
var self = this; var self = this;
this.socket.once('connect', function() { this.socket.once('connect', function() {
self.ts = utils.now(); self._onConnect();
self.connected = true;
self.emit('connect');
if (self._connectTimeout != null) {
clearTimeout(self._connectTimeout);
self._connectTimeout = null;
}
self._onconnect();
}); });
this.socket.once('error', function(err) { this.socket.once('error', function(err) {
@ -190,11 +184,27 @@ Peer.prototype._init = function init() {
self.reject(null, 'malformed', 'error parsing message', 10); self.reject(null, 'malformed', 'error parsing message', 10);
self._error(err, true); 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; 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) { this.request('verack', function callee(err) {
if (err) { if (err) {
self._error(err); self._error(err);

View File

@ -839,8 +839,7 @@ Pool.prototype._createPeer = function _createPeer(options) {
network: options.network, network: options.network,
spv: options.spv, spv: options.spv,
witness: options.witness, witness: options.witness,
headers: this.options.headers, headers: this.options.headers
ts: options.ts
}); });
peer.once('close', function() { peer.once('close', function() {
@ -1122,8 +1121,7 @@ Pool.prototype._addLeech = function _addLeech(socket) {
socket: socket, socket: socket,
network: false, network: false,
spv: false, spv: false,
witness: false, witness: false
ts: utils.now()
}); });
bcoin.debug('Added leech peer (%s).', peer.hostname); bcoin.debug('Added leech peer (%s).', peer.hostname);