fix race condition.

This commit is contained in:
Christopher Jeffrey 2016-05-22 07:22:55 -07:00
parent 92e8a9c4bc
commit d605af7ed8
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1545,52 +1545,61 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) {
*/
Pool.prototype.has = function has(type, hash, force, callback) {
var self = this;
if (!callback) {
callback = force;
force = false;
}
if (force) {
callback = utils.asyncify(callback);
function check(err, exists) {
if (err)
return callback(err);
if (exists)
return callback(null, true);
// Check the pending requests.
if (self.request.map[hash])
return callback(null, true);
// We need to reset the rejects filter periodically.
// There may be a locktime in a TX that is now valid.
if (self.rejects.tip !== self.chain.tip.hash) {
self.rejects.tip = self.chain.tip.hash;
self.rejects.reset();
} else {
// If we recently rejected this item. Ignore.
if (self.rejects.test(hash, 'hex')) {
bcoin.debug('Peer sent a known reject: %s.', hash);
return callback(null, true);
}
}
return callback(null, false);
}
// Check the pending requests.
if (this.request.map[hash]) {
callback = utils.asyncify(callback);
return callback(null, true);
}
// We need to reset the rejects filter periodically.
// There may be a locktime in a TX that is now valid.
if (this.rejects.tip !== this.chain.tip.hash) {
this.rejects.tip = this.chain.tip.hash;
this.rejects.reset();
} else {
// If we recently rejected this item. Ignore.
if (this.rejects.test(hash, 'hex')) {
callback = utils.asyncify(callback);
bcoin.debug('Peer sent a known reject: %s.', hash);
return callback(null, true);
}
if (force) {
check = utils.asyncify(check);
return check(null, false);
}
if (type === this.tx.type) {
// Check the TX filter if
// we don't have a mempool.
if (!this.mempool) {
callback = utils.asyncify(callback);
check = utils.asyncify(check);
if (this.tx.filter.added(hash, 'hex'))
return callback(null, false);
return callback(null, true);
return check(null, false);
return check(null, true);
}
// Check the mempool.
return this.mempool.has(hash, callback);
return this.mempool.has(hash, check);
}
// Check the chain.
return this.chain.has(hash, callback);
return this.chain.has(hash, check);
};
/**