net: refactor pool.

This commit is contained in:
Christopher Jeffrey 2016-12-16 16:53:47 -08:00
parent 8ccefb8e71
commit af8194112e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 592 additions and 528 deletions

View File

@ -1842,7 +1842,7 @@ function RejectPacket(options) {
this.message = ''; this.message = '';
this.code = constants.reject.INVALID; this.code = constants.reject.INVALID;
this.reason = ''; this.reason = '';
this.data = null; this.hash = null;
if (options) if (options)
this.fromOptions(options); this.fromOptions(options);
@ -1878,8 +1878,8 @@ RejectPacket.prototype.fromOptions = function fromOptions(options) {
if (options.reason) if (options.reason)
this.reason = options.reason; this.reason = options.reason;
if (options.data) if (options.hash)
this.data = options.data; this.hash = options.hash;
return this; return this;
}; };
@ -1906,7 +1906,7 @@ RejectPacket.prototype.getSize = function getSize() {
size += 1; size += 1;
size += encoding.sizeVarString(this.reason, 'ascii'); size += encoding.sizeVarString(this.reason, 'ascii');
if (this.data) if (this.hash)
size += 32; size += 32;
return size; return size;
@ -1925,8 +1925,8 @@ RejectPacket.prototype.toWriter = function toWriter(bw) {
bw.writeU8(this.code); bw.writeU8(this.code);
bw.writeVarString(this.reason, 'ascii'); bw.writeVarString(this.reason, 'ascii');
if (this.data) if (this.hash)
bw.writeHash(this.data); bw.writeHash(this.hash);
return bw; return bw;
}; };
@ -1953,9 +1953,9 @@ RejectPacket.prototype.fromReader = function fromReader(br) {
this.reason = br.readVarString('ascii', 111); this.reason = br.readVarString('ascii', 111);
if (this.message === 'block' || this.message === 'tx') if (this.message === 'block' || this.message === 'tx')
this.data = br.readHash('hex'); this.hash = br.readHash('hex');
else else
this.data = null; this.hash = null;
return this; return this;
}; };
@ -2017,7 +2017,7 @@ RejectPacket.prototype.fromReason = function fromReason(code, reason, obj) {
if (obj) { if (obj) {
this.message = (obj instanceof TX) ? 'tx' : 'block'; this.message = (obj instanceof TX) ? 'tx' : 'block';
this.data = obj.hash('hex'); this.hash = obj.hash('hex');
} }
return this; return this;
@ -2056,7 +2056,7 @@ RejectPacket.prototype.inspect = function inspect() {
+ ' msg=' + this.message + ' msg=' + this.message
+ ' code=' + (constants.rejectByVal[this.code] || this.code) + ' code=' + (constants.rejectByVal[this.code] || this.code)
+ ' reason=' + this.reason + ' reason=' + this.reason
+ ' data=' + this.data + ' hash=' + (this.hash ? util.revHex(this.hash) : null)
+ '>'; + '>';
}; };

View File

@ -378,6 +378,11 @@ Peer.prototype.initConnect = function initConnect() {
self.connected = true; self.connected = true;
self.emit('connect'); self.emit('connect');
if (self.connectTimeout != null) {
clearTimeout(self.connectTimeout);
self.connectTimeout = null;
}
resolve(); resolve();
}); });
@ -1090,6 +1095,7 @@ Peer.prototype.handlePacket = co(function* handlePacket(packet) {
case packetTypes.GETBLOCKS: case packetTypes.GETBLOCKS:
case packetTypes.GETHEADERS: case packetTypes.GETHEADERS:
case packetTypes.GETUTXOS: case packetTypes.GETUTXOS:
case packetTypes.GETBLOCKTXN:
unlock = yield this.locker.lock(); unlock = yield this.locker.lock();
this.socket.pause(); this.socket.pause();
@ -1113,8 +1119,6 @@ Peer.prototype.handlePacket = co(function* handlePacket(packet) {
*/ */
Peer.prototype.onPacket = co(function* onPacket(packet) { Peer.prototype.onPacket = co(function* onPacket(packet) {
this.lastRecv = util.ms();
if (this.destroyed) if (this.destroyed)
throw new Error('Destroyed peer sent a packet.'); throw new Error('Destroyed peer sent a packet.');
@ -1138,6 +1142,8 @@ Peer.prototype.onPacket = co(function* onPacket(packet) {
this._flushMerkle(); this._flushMerkle();
} }
this.lastRecv = util.ms();
switch (packet.type) { switch (packet.type) {
case packetTypes.VERSION: case packetTypes.VERSION:
return yield this.handleVersion(packet); return yield this.handleVersion(packet);
@ -2138,19 +2144,18 @@ Peer.prototype.handleTX = co(function* handleTX(packet) {
/** /**
* Handle `reject` packet. * Handle `reject` packet.
* @private * @private
* @param {RejectPacket} * @param {RejectPacket} reject
*/ */
Peer.prototype.handleReject = co(function* handleReject(details) { Peer.prototype.handleReject = co(function* handleReject(reject) {
var hash, entry; var entry;
this.fire('reject', details); this.fire('reject', reject);
if (!details.data) if (!reject.hash)
return; return;
hash = details.data; entry = this.pool.invMap[reject.hash];
entry = this.pool.invMap[hash];
if (!entry) if (!entry)
return; return;
@ -2583,12 +2588,12 @@ Peer.prototype.sendCompact = function sendCompact() {
}; };
/** /**
* Check whether the peer is misbehaving (banScore >= 100). * Check whether the peer is banned (banScore >= 100).
* @returns {Boolean} * @returns {Boolean}
*/ */
Peer.prototype.isMisbehaving = function isMisbehaving() { Peer.prototype.isBanned = function isBanned() {
return this.pool.hosts.isMisbehaving(this); return this.pool.hosts.isBanned(this);
}; };
/** /**

File diff suppressed because it is too large Load Diff