fix race condition.
This commit is contained in:
parent
92e8a9c4bc
commit
d605af7ed8
@ -1545,52 +1545,61 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype.has = function has(type, hash, force, callback) {
|
Pool.prototype.has = function has(type, hash, force, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
callback = force;
|
callback = force;
|
||||||
force = false;
|
force = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (force) {
|
function check(err, exists) {
|
||||||
callback = utils.asyncify(callback);
|
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);
|
return callback(null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the pending requests.
|
if (force) {
|
||||||
if (this.request.map[hash]) {
|
check = utils.asyncify(check);
|
||||||
callback = utils.asyncify(callback);
|
return check(null, false);
|
||||||
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 (type === this.tx.type) {
|
if (type === this.tx.type) {
|
||||||
// Check the TX filter if
|
// Check the TX filter if
|
||||||
// we don't have a mempool.
|
// we don't have a mempool.
|
||||||
if (!this.mempool) {
|
if (!this.mempool) {
|
||||||
callback = utils.asyncify(callback);
|
check = utils.asyncify(check);
|
||||||
if (this.tx.filter.added(hash, 'hex'))
|
if (this.tx.filter.added(hash, 'hex'))
|
||||||
return callback(null, false);
|
return check(null, false);
|
||||||
return callback(null, true);
|
return check(null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the mempool.
|
// Check the mempool.
|
||||||
return this.mempool.has(hash, callback);
|
return this.mempool.has(hash, check);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the chain.
|
// Check the chain.
|
||||||
return this.chain.has(hash, callback);
|
return this.chain.has(hash, check);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user