pool: fix broadcasting. clean up loadrequest.
This commit is contained in:
parent
5b3f6bb042
commit
56aca55b1e
@ -1744,7 +1744,9 @@ Pool.prototype.broadcast = function broadcast(msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
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}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function LoadRequest(pool, peer, type, hash, callback) {
|
function LoadRequest(pool, peer, type, hash) {
|
||||||
if (!(this instanceof LoadRequest))
|
if (!(this instanceof LoadRequest))
|
||||||
return new LoadRequest(pool, peer, type, hash, callback);
|
return new LoadRequest(pool, peer, type, hash);
|
||||||
|
|
||||||
this.pool = pool;
|
this.pool = pool;
|
||||||
this.peer = peer;
|
this.peer = peer;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.hash = hash;
|
this.hash = hash;
|
||||||
this.callback = [];
|
|
||||||
this.active = false;
|
this.active = false;
|
||||||
this.id = this.pool.uid++;
|
this.id = this.pool.uid++;
|
||||||
this.timeout = null;
|
this.timeout = null;
|
||||||
this.onTimeout = this._onTimeout.bind(this);
|
this.onTimeout = this._onTimeout.bind(this);
|
||||||
|
|
||||||
this.addCallback(callback);
|
|
||||||
|
|
||||||
assert(!this.pool.requestMap[this.hash]);
|
assert(!this.pool.requestMap[this.hash]);
|
||||||
this.pool.requestMap[this.hash] = this;
|
this.pool.requestMap[this.hash] = this;
|
||||||
}
|
}
|
||||||
@ -2318,7 +2317,7 @@ function LoadRequest(pool, peer, type, hash, callback) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
LoadRequest.prototype.destroy = function destroy() {
|
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.');
|
'Loader took too long serving a block. Finding a new one.');
|
||||||
this.peer.destroy();
|
this.peer.destroy();
|
||||||
}
|
}
|
||||||
return this.finish(new Error('Timed out.'));
|
return this.finish();
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a callback to be executed when item is received.
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
|
|
||||||
LoadRequest.prototype.addCallback = function addCallback(callback) {
|
|
||||||
if (callback)
|
|
||||||
this.callback.push(callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2366,12 +2355,9 @@ 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() {
|
||||||
var i;
|
|
||||||
|
|
||||||
if (this.pool.requestMap[this.hash]) {
|
if (this.pool.requestMap[this.hash]) {
|
||||||
delete this.pool.requestMap[this.hash];
|
delete this.pool.requestMap[this.hash];
|
||||||
if (this.active) {
|
if (this.active) {
|
||||||
@ -2393,9 +2379,6 @@ LoadRequest.prototype.finish = function finish(err) {
|
|||||||
clearTimeout(this.timeout);
|
clearTimeout(this.timeout);
|
||||||
this.timeout = null;
|
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
|
* @private
|
||||||
* @param {Pool} pool
|
* @param {Pool} pool
|
||||||
* @param {TX|Block|InvItem} item
|
* @param {TX|Block|InvItem} item
|
||||||
* @param {Function?} callback
|
|
||||||
* @emits BroadcastItem#ack
|
* @emits BroadcastItem#ack
|
||||||
* @emits BroadcastItem#reject
|
* @emits BroadcastItem#reject
|
||||||
* @emits BroadcastItem#timeout
|
* @emits BroadcastItem#timeout
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function BroadcastItem(pool, item, callback) {
|
function BroadcastItem(pool, item) {
|
||||||
if (!(this instanceof BroadcastItem))
|
if (!(this instanceof BroadcastItem))
|
||||||
return new BroadcastItem(pool, item);
|
return new BroadcastItem(pool, item);
|
||||||
|
|
||||||
@ -2455,17 +2437,12 @@ function BroadcastItem(pool, item, callback) {
|
|||||||
this.hash = item.hash;
|
this.hash = item.hash;
|
||||||
this.type = item.type;
|
this.type = item.type;
|
||||||
|
|
||||||
if (typeof this.type === 'string')
|
|
||||||
this.type = constants.inv[this.type.toUpperCase()];
|
|
||||||
|
|
||||||
assert(this.type != null);
|
assert(this.type != null);
|
||||||
assert(typeof this.hash === 'string');
|
assert(typeof this.hash === 'string');
|
||||||
|
|
||||||
// INV does not set the witness
|
// INV does not set the witness
|
||||||
// mask (only GETDATA does this).
|
// mask (only GETDATA does this).
|
||||||
assert((this.type & constants.WITNESS_MASK) === 0);
|
assert((this.type & constants.WITNESS_MASK) === 0);
|
||||||
|
|
||||||
this.addCallback(callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.inherits(BroadcastItem, EventEmitter);
|
utils.inherits(BroadcastItem, EventEmitter);
|
||||||
@ -2476,8 +2453,7 @@ utils.inherits(BroadcastItem, EventEmitter);
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
BroadcastItem.prototype.addCallback = function addCallback(callback) {
|
BroadcastItem.prototype.addCallback = function addCallback(callback) {
|
||||||
if (callback)
|
this.callback.push(callback);
|
||||||
this.callback.push(callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1147,6 +1147,9 @@ WalletDB.prototype.resend = co(function* resend() {
|
|||||||
|
|
||||||
tx = TX.fromExtended(data);
|
tx = TX.fromExtended(data);
|
||||||
|
|
||||||
|
if (tx.isCoinbase())
|
||||||
|
continue;
|
||||||
|
|
||||||
this.emit('send', tx);
|
this.emit('send', tx);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user