From 2e912af9b4047c587226c5274bfabfe93579b567 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 28 Apr 2016 12:34:26 -0400 Subject: [PATCH] bitcoind: subscribe to zmq event closer to 100% sync Instead of subscribing at >= 0.995 subscribe at >= 0.9999 progress --- lib/services/bitcoind.js | 38 ++++++++++++++++++++-------------- test/services/bitcoind.unit.js | 4 ++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 4077c848..4d24d1d7 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -28,7 +28,6 @@ var Transaction = require('../transaction'); * @param {Node} options.node - A reference to the node */ function Bitcoin(options) { - /* jshint maxstatements: 20 */ if (!(this instanceof Bitcoin)) { return new Bitcoin(options); } @@ -46,18 +45,8 @@ function Bitcoin(options) { this.subscriptions.rawtransaction = []; this.subscriptions.hashblock = []; - // limits - this.maxTransactionHistory = options.maxTransactionHistory || Bitcoin.DEFAULT_MAX_HISTORY; - this.maxAddressesQuery = options.maxAddressesQuery || Bitcoin.DEFAULT_MAX_ADDRESSES_QUERY; - this.shutdownTimeout = options.shutdownTimeout || Bitcoin.DEFAULT_SHUTDOWN_TIMEOUT; - - // spawn restart setting - this.spawnRestartTime = options.spawnRestartTime || Bitcoin.DEFAULT_SPAWN_RESTART_TIME; - this.spawnStopTime = options.spawnStopTime || Bitcoin.DEFAULT_SPAWN_STOP_TIME; - - // try all interval - this.tryAllInterval = options.tryAllInterval || Bitcoin.DEFAULT_TRY_ALL_INTERVAL; - this.startRetryInterval = options.startRetryInterval || Bitcoin.DEFAULT_START_RETRY_INTERVAL; + // set initial settings + this._initDefaults(options); // available bitcoind nodes this._initClients(); @@ -75,6 +64,7 @@ Bitcoin.dependencies = []; Bitcoin.DEFAULT_MAX_HISTORY = 10; Bitcoin.DEFAULT_SHUTDOWN_TIMEOUT = 15000; +Bitcoin.DEFAULT_ZMQ_SUBSCRIBE_PROGRESS = 0.9999; Bitcoin.DEFAULT_MAX_ADDRESSES_QUERY = 10000; Bitcoin.DEFAULT_SPAWN_RESTART_TIME = 5000; Bitcoin.DEFAULT_SPAWN_STOP_TIME = 10000; @@ -97,6 +87,24 @@ Bitcoin.DEFAULT_CONFIG_SETTINGS = { uacomment: 'bitcore' }; +Bitcoin.prototype._initDefaults = function(options) { + // limits + this.maxTransactionHistory = options.maxTransactionHistory || Bitcoin.DEFAULT_MAX_HISTORY; + this.maxAddressesQuery = options.maxAddressesQuery || Bitcoin.DEFAULT_MAX_ADDRESSES_QUERY; + this.shutdownTimeout = options.shutdownTimeout || Bitcoin.DEFAULT_SHUTDOWN_TIMEOUT; + + // spawn restart setting + this.spawnRestartTime = options.spawnRestartTime || Bitcoin.DEFAULT_SPAWN_RESTART_TIME; + this.spawnStopTime = options.spawnStopTime || Bitcoin.DEFAULT_SPAWN_STOP_TIME; + + // try all interval + this.tryAllInterval = options.tryAllInterval || Bitcoin.DEFAULT_TRY_ALL_INTERVAL; + this.startRetryInterval = options.startRetryInterval || Bitcoin.DEFAULT_START_RETRY_INTERVAL; + + // sync progress level when zmq subscribes to events + this.zmqSubscribeProgress = options.zmqSubscribeProgress || Bitcoin.DEFAULT_ZMQ_SUBSCRIBE_PROGRESS; +}; + Bitcoin.prototype._initCaches = function() { // caches valid until there is a new block this.utxosCache = LRU(50000); @@ -498,8 +506,8 @@ Bitcoin.prototype._checkSyncedAndSubscribeZmqEvents = function(node) { if (err) { return callback(self._wrapRPCError(err)); } - var percentSynced = response.result.verificationprogress * 100; - if (Math.round(percentSynced) >= 99) { + var progress = response.result.verificationprogress; + if (progress >= self.zmqSubscribeProgress) { // subscribe to events for further updates self._subscribeZmqEvents(node); clearInterval(interval); diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 7f030668..1e642461 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -810,8 +810,8 @@ describe('Bitcoin Service', function() { bitcoind._checkSyncedAndSubscribeZmqEvents(node); setTimeout(function() { log.error.callCount.should.equal(2); - blockEvents.should.equal(10); - bitcoind._updateTip.callCount.should.equal(10); + blockEvents.should.equal(11); + bitcoind._updateTip.callCount.should.equal(11); bitcoind._subscribeZmqEvents.callCount.should.equal(1); done(); }, 200);