From 1a4984d533f865cc4c096f9bef9dae130e22938b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 21 Dec 2016 15:31:55 -0800 Subject: [PATCH] net: add hasServices method. --- lib/net/packets.js | 10 ++++++++++ lib/net/pool.js | 26 ++++++++++++-------------- lib/primitives/netaddress.js | 10 ++++++++++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/net/packets.js b/lib/net/packets.js index ea5e578e..f2c3132f 100644 --- a/lib/net/packets.js +++ b/lib/net/packets.js @@ -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} diff --git a/lib/net/pool.js b/lib/net/pool.js index 17c532d2..94bf5361 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -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) diff --git a/lib/primitives/netaddress.js b/lib/primitives/netaddress.js index 5af7451b..d4831b69 100644 --- a/lib/primitives/netaddress.js +++ b/lib/primitives/netaddress.js @@ -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}