more refactoring.

This commit is contained in:
Christopher Jeffrey 2016-05-19 16:59:15 -07:00
parent 9966718e9c
commit e65da93ce1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -921,7 +921,7 @@ Pool.prototype._createPeer = function _createPeer(options) {
: null; : null;
bcoin.debug( bcoin.debug(
'Reject (%s): msg=%s ccode=%s reason=%s data=%s', 'Reject (%s): msg=%s ccode=%s reason=%s data=%s.',
peer.hostname, peer.hostname,
payload.message, payload.message,
payload.ccode, payload.ccode,
@ -1007,8 +1007,12 @@ Pool.prototype._createPeer = function _createPeer(options) {
self.block.versionHeight = version.height; self.block.versionHeight = version.height;
bcoin.debug( bcoin.debug(
'Received version (%s): version=%d height=%d agent=%s', 'Received version (%s): version=%d height=%d services=%s agent=%s',
peer.hostname, version.version, version.height, version.agent); peer.hostname,
version.version,
version.height,
version.services.toString(2),
version.agent);
bcoin.time.add(peer.host, version.ts); bcoin.time.add(peer.host, version.ts);
@ -1231,7 +1235,7 @@ Pool.prototype._removePeer = function _removePeer(peer) {
Object.keys(this.request.map).forEach(function(hash) { Object.keys(this.request.map).forEach(function(hash) {
var item = this.request.map[hash]; var item = this.request.map[hash];
if (item.peer === peer) if (item.peer === peer)
item.finish(); item.finish(new Error('Peer closed.'));
}, this); }, this);
bcoin.debug('Removed loader peer (%s).', peer.hostname); bcoin.debug('Removed loader peer (%s).', peer.hostname);
this.peers.load = null; this.peers.load = null;
@ -1695,7 +1699,7 @@ Pool.prototype.destroy = function destroy(callback) {
}); });
Object.keys(this.request.map).forEach(function(hash) { Object.keys(this.request.map).forEach(function(hash) {
this.request.map[hash].finish(); this.request.map[hash].finish(new Error('Pool closed.'));
}, this); }, this);
if (this.peers.load) if (this.peers.load)
@ -2014,16 +2018,30 @@ function LoadRequest(pool, peer, type, hash, callback) {
this.callback = []; this.callback = [];
this.active = false; this.active = false;
this.id = this.pool.uid++; this.id = this.pool.uid++;
this.timeout = null;
if (callback) this.addCallback(callback);
this.callback.push(callback);
assert(!this.pool.request.map[this.hash]); assert(!this.pool.request.map[this.hash]);
this.pool.request.map[this.hash] = this; this.pool.request.map[this.hash] = this;
this._finish = this.finish.bind(this); this._finish = this._ontimeout.bind(this);
} }
LoadRequest.prototype._ontimeout = function _ontimeout() {
this.finish(new Error('Timeout.'));
};
/**
* Add a callback to be executed when item is received.
* @param {Function} callback
*/
LoadRequest.prototype.addCallback = function addCallback(callback) {
if (callback)
this.callback.push(callback);
};
/** /**
* Mark the request as in-flight. Start timeout timer. * Mark the request as in-flight. Start timeout timer.
*/ */
@ -2034,6 +2052,7 @@ LoadRequest.prototype.start = function start() {
this.active = true; this.active = true;
this.pool.request.active++; this.pool.request.active++;
if (this.type === this.pool.tx.type) if (this.type === this.pool.tx.type)
this.pool.request.activeTX++; this.pool.request.activeTX++;
else else
@ -2045,6 +2064,7 @@ LoadRequest.prototype.start = function start() {
/** /**
* Mark the request as completed. * Mark the request as completed.
* Remove from queue and map. Clear timeout. * Remove from queue and map. Clear timeout.
* @param {Error?} err
*/ */
LoadRequest.prototype.finish = function finish(err) { LoadRequest.prototype.finish = function finish(err) {
@ -2053,12 +2073,12 @@ LoadRequest.prototype.finish = function finish(err) {
if (this.pool.request.map[this.hash]) { if (this.pool.request.map[this.hash]) {
delete this.pool.request.map[this.hash]; delete this.pool.request.map[this.hash];
if (this.active) { if (this.active) {
this.active = false;
this.pool.request.active--; this.pool.request.active--;
if (this.type === this.pool.tx.type) if (this.type === this.pool.tx.type)
this.pool.request.activeTX--; this.pool.request.activeTX--;
else else
this.pool.request.activeBlocks--; this.pool.request.activeBlocks--;
this.active = false;
} }
} }
@ -2071,7 +2091,7 @@ LoadRequest.prototype.finish = function finish(err) {
if (this.timeout != null) { if (this.timeout != null) {
clearTimeout(this.timeout); clearTimeout(this.timeout);
delete this.timeout; this.timeout = null;
} }
for (i = 0; i < this.callback.length; i++) for (i = 0; i < this.callback.length; i++)
@ -2137,7 +2157,7 @@ utils.inherits(BroadcastItem, EventEmitter);
/** /**
* Add a callback to be executed on ack, timeout, or reject. * Add a callback to be executed on ack, timeout, or reject.
* @param { * @param {Function} callback
*/ */
BroadcastItem.prototype.addCallback = function addCallback(callback) { BroadcastItem.prototype.addCallback = function addCallback(callback) {