From 51a25bf15f128ac4045b84eaae4b55ee51df7bac Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 21 Jan 2017 15:20:03 -0800 Subject: [PATCH] net/nodeclient: always resend bloom filter. see #119. --- lib/net/pool.js | 43 ++++++++++++++++++++++++++++-------------- lib/node/nodeclient.js | 2 ++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/net/pool.js b/lib/net/pool.js index 3af29b08..3a774809 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -111,7 +111,7 @@ function Pool(options) { this.requestMap = new Map(); this.compactBlocks = new Map(); this.invMap = new Map(); - this.pendingWatch = null; + this.pendingFilter = null; this.pendingRefill = null; this.headersFirst = false; @@ -360,9 +360,9 @@ Pool.prototype._disconnect = co(function* disconnect() { this.requestMap.reset(); - if (this.pendingWatch != null) { - clearTimeout(this.pendingWatch); - this.pendingWatch = null; + if (this.pendingFilter != null) { + clearTimeout(this.pendingFilter); + this.pendingFilter = null; } if (this.pendingRefill != null) { @@ -2755,7 +2755,7 @@ Pool.prototype.setFilter = function setFilter(filter) { return; this.spvFilter = filter; - this.sendFilterLoad(); + this.queueFilterLoad(); }; /** @@ -2769,7 +2769,7 @@ Pool.prototype.watch = function watch(data, enc) { return; this.spvFilter.add(data, enc); - this.sendFilterLoad(); + this.queueFilterLoad(); }; /** @@ -2781,7 +2781,26 @@ Pool.prototype.unwatch = function unwatch() { return; this.spvFilter.reset(); - this.sendFilterLoad(); + this.queueFilterLoad(); +}; + +/** + * Queue a resend of the bloom filter. + */ + +Pool.prototype.queueFilterLoad = function queueFilterLoad() { + var self = this; + + if (!this.options.spv) + return; + + if (this.pendingFilter != null) + return; + + this.pendingFilter = setTimeout(function() { + self.pendingFilter = null; + self.sendFilterLoad(); + }, 100); }; /** @@ -2789,19 +2808,15 @@ Pool.prototype.unwatch = function unwatch() { */ Pool.prototype.sendFilterLoad = function sendFilterLoad() { - var self = this; var peer; - if (this.pendingWatch != null) + if (!this.options.spv) return; assert(this.spvFilter); - this.pendingWatch = setTimeout(function() { - self.pendingWatch = null; - for (peer = self.peers.head(); peer; peer = peer.next) - peer.sendFilterLoad(self.spvFilter); - }, 50); + for (peer = this.peers.head(); peer; peer = peer.next) + peer.sendFilterLoad(this.spvFilter); }; /** diff --git a/lib/node/nodeclient.js b/lib/node/nodeclient.js index edd5aeab..92abffa3 100644 --- a/lib/node/nodeclient.js +++ b/lib/node/nodeclient.js @@ -146,6 +146,7 @@ NodeClient.prototype.setFilter = function setFilter(filter) { */ NodeClient.prototype.addFilter = function addFilter(data) { + this.node.pool.queueFilterLoad(); return Promise.resolve(); }; @@ -155,6 +156,7 @@ NodeClient.prototype.addFilter = function addFilter(data) { */ NodeClient.prototype.resetFilter = function resetFilter() { + this.node.pool.queueFilterLoad(); return Promise.resolve(); };