bitcoind: subscribe to zmq event closer to 100% sync

Instead of subscribing at >= 0.995 subscribe at >= 0.9999 progress
This commit is contained in:
Braydon Fuller 2016-04-28 12:34:26 -04:00
parent c22f6505eb
commit 2e912af9b4
2 changed files with 25 additions and 17 deletions

View File

@ -28,7 +28,6 @@ var Transaction = require('../transaction');
* @param {Node} options.node - A reference to the node * @param {Node} options.node - A reference to the node
*/ */
function Bitcoin(options) { function Bitcoin(options) {
/* jshint maxstatements: 20 */
if (!(this instanceof Bitcoin)) { if (!(this instanceof Bitcoin)) {
return new Bitcoin(options); return new Bitcoin(options);
} }
@ -46,18 +45,8 @@ function Bitcoin(options) {
this.subscriptions.rawtransaction = []; this.subscriptions.rawtransaction = [];
this.subscriptions.hashblock = []; this.subscriptions.hashblock = [];
// limits // set initial settings
this.maxTransactionHistory = options.maxTransactionHistory || Bitcoin.DEFAULT_MAX_HISTORY; this._initDefaults(options);
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;
// available bitcoind nodes // available bitcoind nodes
this._initClients(); this._initClients();
@ -75,6 +64,7 @@ Bitcoin.dependencies = [];
Bitcoin.DEFAULT_MAX_HISTORY = 10; Bitcoin.DEFAULT_MAX_HISTORY = 10;
Bitcoin.DEFAULT_SHUTDOWN_TIMEOUT = 15000; Bitcoin.DEFAULT_SHUTDOWN_TIMEOUT = 15000;
Bitcoin.DEFAULT_ZMQ_SUBSCRIBE_PROGRESS = 0.9999;
Bitcoin.DEFAULT_MAX_ADDRESSES_QUERY = 10000; Bitcoin.DEFAULT_MAX_ADDRESSES_QUERY = 10000;
Bitcoin.DEFAULT_SPAWN_RESTART_TIME = 5000; Bitcoin.DEFAULT_SPAWN_RESTART_TIME = 5000;
Bitcoin.DEFAULT_SPAWN_STOP_TIME = 10000; Bitcoin.DEFAULT_SPAWN_STOP_TIME = 10000;
@ -97,6 +87,24 @@ Bitcoin.DEFAULT_CONFIG_SETTINGS = {
uacomment: 'bitcore' 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() { Bitcoin.prototype._initCaches = function() {
// caches valid until there is a new block // caches valid until there is a new block
this.utxosCache = LRU(50000); this.utxosCache = LRU(50000);
@ -498,8 +506,8 @@ Bitcoin.prototype._checkSyncedAndSubscribeZmqEvents = function(node) {
if (err) { if (err) {
return callback(self._wrapRPCError(err)); return callback(self._wrapRPCError(err));
} }
var percentSynced = response.result.verificationprogress * 100; var progress = response.result.verificationprogress;
if (Math.round(percentSynced) >= 99) { if (progress >= self.zmqSubscribeProgress) {
// subscribe to events for further updates // subscribe to events for further updates
self._subscribeZmqEvents(node); self._subscribeZmqEvents(node);
clearInterval(interval); clearInterval(interval);

View File

@ -810,8 +810,8 @@ describe('Bitcoin Service', function() {
bitcoind._checkSyncedAndSubscribeZmqEvents(node); bitcoind._checkSyncedAndSubscribeZmqEvents(node);
setTimeout(function() { setTimeout(function() {
log.error.callCount.should.equal(2); log.error.callCount.should.equal(2);
blockEvents.should.equal(10); blockEvents.should.equal(11);
bitcoind._updateTip.callCount.should.equal(10); bitcoind._updateTip.callCount.should.equal(11);
bitcoind._subscribeZmqEvents.callCount.should.equal(1); bitcoind._subscribeZmqEvents.callCount.should.equal(1);
done(); done();
}, 200); }, 200);