pool: fix broadcasting. clean up loadrequest.

This commit is contained in:
Christopher Jeffrey 2016-10-06 01:58:05 -07:00
parent 5b3f6bb042
commit 56aca55b1e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 13 additions and 34 deletions

View File

@ -1744,7 +1744,9 @@ Pool.prototype.broadcast = function broadcast(msg) {
}
return new Promise(function(resolve, reject) {
item.addCallback(co.wrap(resolve, reject));
item.addCallback(function(err) {
resolve(err ? false : true);
});
});
};
@ -2293,22 +2295,19 @@ HostList.prototype.addSeed = function addSeed(hostname) {
* @returns {Promise}
*/
function LoadRequest(pool, peer, type, hash, callback) {
function LoadRequest(pool, peer, type, hash) {
if (!(this instanceof LoadRequest))
return new LoadRequest(pool, peer, type, hash, callback);
return new LoadRequest(pool, peer, type, hash);
this.pool = pool;
this.peer = peer;
this.type = type;
this.hash = hash;
this.callback = [];
this.active = false;
this.id = this.pool.uid++;
this.timeout = null;
this.onTimeout = this._onTimeout.bind(this);
this.addCallback(callback);
assert(!this.pool.requestMap[this.hash]);
this.pool.requestMap[this.hash] = this;
}
@ -2318,7 +2317,7 @@ function LoadRequest(pool, peer, type, hash, callback) {
*/
LoadRequest.prototype.destroy = function destroy() {
return this.finish(new Error('Destroyed.'));
return this.finish();
};
/**
@ -2332,17 +2331,7 @@ LoadRequest.prototype._onTimeout = function _onTimeout() {
'Loader took too long serving a block. Finding a new one.');
this.peer.destroy();
}
return this.finish(new Error('Timed out.'));
};
/**
* Add a callback to be executed when item is received.
* @returns {Promise}
*/
LoadRequest.prototype.addCallback = function addCallback(callback) {
if (callback)
this.callback.push(callback);
return this.finish();
};
/**
@ -2366,12 +2355,9 @@ LoadRequest.prototype.start = function start() {
/**
* Mark the request as completed.
* Remove from queue and map. Clear timeout.
* @param {Error?} err
*/
LoadRequest.prototype.finish = function finish(err) {
var i;
LoadRequest.prototype.finish = function finish() {
if (this.pool.requestMap[this.hash]) {
delete this.pool.requestMap[this.hash];
if (this.active) {
@ -2393,9 +2379,6 @@ LoadRequest.prototype.finish = function finish(err) {
clearTimeout(this.timeout);
this.timeout = null;
}
for (i = 0; i < this.callback.length; i++)
this.callback[i](err);
};
/**
@ -2428,13 +2411,12 @@ LoadRequest.prototype.toInv = function toInv() {
* @private
* @param {Pool} pool
* @param {TX|Block|InvItem} item
* @param {Function?} callback
* @emits BroadcastItem#ack
* @emits BroadcastItem#reject
* @emits BroadcastItem#timeout
*/
function BroadcastItem(pool, item, callback) {
function BroadcastItem(pool, item) {
if (!(this instanceof BroadcastItem))
return new BroadcastItem(pool, item);
@ -2455,17 +2437,12 @@ function BroadcastItem(pool, item, callback) {
this.hash = item.hash;
this.type = item.type;
if (typeof this.type === 'string')
this.type = constants.inv[this.type.toUpperCase()];
assert(this.type != null);
assert(typeof this.hash === 'string');
// INV does not set the witness
// mask (only GETDATA does this).
assert((this.type & constants.WITNESS_MASK) === 0);
this.addCallback(callback);
}
utils.inherits(BroadcastItem, EventEmitter);
@ -2476,8 +2453,7 @@ utils.inherits(BroadcastItem, EventEmitter);
*/
BroadcastItem.prototype.addCallback = function addCallback(callback) {
if (callback)
this.callback.push(callback);
this.callback.push(callback);
};
/**

View File

@ -1147,6 +1147,9 @@ WalletDB.prototype.resend = co(function* resend() {
tx = TX.fromExtended(data);
if (tx.isCoinbase())
continue;
this.emit('send', tx);
}
});