net: add hasServices method.

This commit is contained in:
Christopher Jeffrey 2016-12-21 15:31:55 -08:00
parent 14f2851929
commit 1a4984d533
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 32 additions and 14 deletions

View File

@ -308,6 +308,16 @@ VersionPacket.prototype.hasWitness = function hasWitness() {
return (this.services & constants.services.WITNESS) !== 0;
};
/**
* Test whether required services are available.
* @param {Number} services
* @returns {Boolean}
*/
VersionPacket.prototype.hasServices = function hasServices(services) {
return (this.services & services) === services;
};
/**
* Test whether the protocol version supports getheaders.
* @returns {Boolean}

View File

@ -121,6 +121,10 @@ function Pool(options) {
this.banTime = constants.BAN_TIME;
this.banScore = constants.BAN_SCORE;
// Required services.
this.needed = constants.services.NETWORK;
this.needed |= constants.services.WITNESS;
this.syncing = false;
this.loadTimeout = 120000;
@ -178,8 +182,10 @@ Pool.prototype._initOptions = function _initOptions() {
if (this.options.headers == null)
this.options.headers = this.options.spv;
if (!this.options.witness)
if (!this.options.witness) {
this.address.services &= ~constants.services.WITNESS;
this.needed &= ~constants.services.WITNESS;
}
if (this.options.host != null) {
assert(typeof this.options.host === 'string');
@ -265,8 +271,10 @@ Pool.prototype._initOptions = function _initOptions() {
if (this.options.preferredSeed)
this.hosts.setSeeds([this.options.preferredSeed]);
if (this.options.spv)
if (this.options.spv) {
this.spvFilter = Bloom.fromRate(10000, 0.001, constants.bloom.ALL);
this.needed |= constants.services.BLOOM;
}
if (!this.options.mempool)
this.txFilter = new Bloom.Rolling(50000, 0.000001);
@ -1031,19 +1039,9 @@ Pool.prototype.handleAddr = function handleAddr(addrs, peer) {
if (addr.isNull())
continue;
if (!addr.hasNetwork())
if (!addr.hasServices(this.needed))
continue;
if (this.options.spv) {
if (!addr.hasBloom())
continue;
}
if (this.options.witness) {
if (!addr.hasWitness())
continue;
}
if (this.hosts.add(addr, peer.address))
this.emit('host', addr, peer);
}
@ -1575,7 +1573,7 @@ Pool.prototype.getHost = function getHost(unique) {
if (addr.isNull())
continue;
if (!addr.hasNetwork())
if (!addr.hasServices(this.needed))
continue;
if (now - entry.lastAttempt < 600 && i < 30)

View File

@ -119,6 +119,16 @@ NetAddress.prototype.hasWitness = function hasWitness() {
return (this.services & constants.services.WITNESS) !== 0;
};
/**
* Test whether required services are available.
* @param {Number} services
* @returns {Boolean}
*/
NetAddress.prototype.hasServices = function hasServices(services) {
return (this.services & services) === services;
};
/**
* Test whether the host is null.
* @returns {Boolean}