diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 0ce7b480..7cba2e03 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -435,11 +435,12 @@ Bitcoin.prototype._zmqTransactionHandler = function(node, message) { Bitcoin.prototype._checkSyncedAndSubscribeZmqEvents = function(node) { var self = this; var interval; - interval = setInterval(function() { + + function checkAndSubscribe(callback) { // update tip node.client.getBestBlockHash(function(err, response) { if (err) { - return log.error(self._wrapRPCError(err)); + return callback(self._wrapRPCError(err)); } var blockhash = new Buffer(response.result, 'hex'); self._updateTip(node, blockhash); @@ -447,17 +448,36 @@ Bitcoin.prototype._checkSyncedAndSubscribeZmqEvents = function(node) { // check if synced node.client.getBlockchainInfo(function(err, response) { if (err) { - return log.error(self._wrapRPCError(err)); + return callback(self._wrapRPCError(err)); } var percentSynced = response.result.verificationprogress * 100; if (Math.round(percentSynced) >= 99) { // subscribe to events for further updates self._subscribeZmqEvents(node); clearInterval(interval); + callback(null, true); + } else { + callback(null, false); } }); }); - }, node._tipUpdateInterval || Bitcoin.DEFAULT_TIP_UPDATE_INTERVAL).unref(); + } + + checkAndSubscribe(function(err, synced) { + if (err) { + log.error(err); + } + if (!synced) { + interval = setInterval(function() { + checkAndSubscribe(function(err) { + if (err) { + log.error(err); + } + }); + }, node._tipUpdateInterval || Bitcoin.DEFAULT_TIP_UPDATE_INTERVAL).unref(); + } + }); + }; Bitcoin.prototype._subscribeZmqEvents = function(node) { diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index b8a2e778..7a2ab905 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -685,11 +685,20 @@ describe('Bitcoin Service', function() { result: '00000000000000001bb82a7f5973618cfd3185ba1ded04dd852a653f92a27c45' }); getBestBlockHash.onCall(0).callsArgWith(0, {code: -1 , message: 'Test error'}); - var getBlockchainInfo = sinon.stub().callsArgWith(0, null, { - result: { - verificationprogress: 0.99 + var progress = 0.90; + function getProgress() { + progress = progress + 0.01; + return progress; + } + var info = {}; + Object.defineProperty(info, 'result', { + get: function() { + return { + verificationprogress: getProgress() + }; } }); + var getBlockchainInfo = sinon.stub().callsArgWith(0, null, info); getBlockchainInfo.onCall(0).callsArgWith(0, {code: -1, message: 'Test error'}); var node = { _reindex: true, @@ -703,10 +712,10 @@ describe('Bitcoin Service', function() { bitcoind._checkSyncedAndSubscribeZmqEvents(node); setTimeout(function() { log.error.callCount.should.equal(2); - bitcoind._updateTip.callCount.should.equal(2); + bitcoind._updateTip.callCount.should.equal(10); bitcoind._subscribeZmqEvents.callCount.should.equal(1); done(); - }, 10); + }, 20); }); });