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.code = constants.reject.INVALID;
this.reason = '';
this.data = null;
this.hash = null;
if (options)
this.fromOptions(options);
@ -1878,8 +1878,8 @@ RejectPacket.prototype.fromOptions = function fromOptions(options) {
if (options.reason)
this.reason = options.reason;
if (options.data)
this.data = options.data;
if (options.hash)
this.hash = options.hash;
return this;
};
@ -1906,7 +1906,7 @@ RejectPacket.prototype.getSize = function getSize() {
size += 1;
size += encoding.sizeVarString(this.reason, 'ascii');
if (this.data)
if (this.hash)
size += 32;
return size;
@ -1925,8 +1925,8 @@ RejectPacket.prototype.toWriter = function toWriter(bw) {
bw.writeU8(this.code);
bw.writeVarString(this.reason, 'ascii');
if (this.data)
bw.writeHash(this.data);
if (this.hash)
bw.writeHash(this.hash);
return bw;
};
@ -1953,9 +1953,9 @@ RejectPacket.prototype.fromReader = function fromReader(br) {
this.reason = br.readVarString('ascii', 111);
if (this.message === 'block' || this.message === 'tx')
this.data = br.readHash('hex');
this.hash = br.readHash('hex');
else
this.data = null;
this.hash = null;
return this;
};
@ -2017,7 +2017,7 @@ RejectPacket.prototype.fromReason = function fromReason(code, reason, obj) {
if (obj) {
this.message = (obj instanceof TX) ? 'tx' : 'block';
this.data = obj.hash('hex');
this.hash = obj.hash('hex');
}
return this;
@ -2056,7 +2056,7 @@ RejectPacket.prototype.inspect = function inspect() {
+ ' msg=' + this.message
+ ' code=' + (constants.rejectByVal[this.code] || this.code)
+ ' 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.emit('connect');
if (self.connectTimeout != null) {
clearTimeout(self.connectTimeout);
self.connectTimeout = null;
}
resolve();
});
@ -1090,6 +1095,7 @@ Peer.prototype.handlePacket = co(function* handlePacket(packet) {
case packetTypes.GETBLOCKS:
case packetTypes.GETHEADERS:
case packetTypes.GETUTXOS:
case packetTypes.GETBLOCKTXN:
unlock = yield this.locker.lock();
this.socket.pause();
@ -1113,8 +1119,6 @@ Peer.prototype.handlePacket = co(function* handlePacket(packet) {
*/
Peer.prototype.onPacket = co(function* onPacket(packet) {
this.lastRecv = util.ms();
if (this.destroyed)
throw new Error('Destroyed peer sent a packet.');
@ -1138,6 +1142,8 @@ Peer.prototype.onPacket = co(function* onPacket(packet) {
this._flushMerkle();
}
this.lastRecv = util.ms();
switch (packet.type) {
case packetTypes.VERSION:
return yield this.handleVersion(packet);
@ -2138,19 +2144,18 @@ Peer.prototype.handleTX = co(function* handleTX(packet) {
/**
* Handle `reject` packet.
* @private
* @param {RejectPacket}
* @param {RejectPacket} reject
*/
Peer.prototype.handleReject = co(function* handleReject(details) {
var hash, entry;
Peer.prototype.handleReject = co(function* handleReject(reject) {
var entry;
this.fire('reject', details);
this.fire('reject', reject);
if (!details.data)
if (!reject.hash)
return;
hash = details.data;
entry = this.pool.invMap[hash];
entry = this.pool.invMap[reject.hash];
if (!entry)
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}
*/
Peer.prototype.isMisbehaving = function isMisbehaving() {
return this.pool.hosts.isMisbehaving(this);
Peer.prototype.isBanned = function isBanned() {
return this.pool.hosts.isBanned(this);
};
/**

File diff suppressed because it is too large Load Diff