diff --git a/lib/services/block/index.js b/lib/services/block/index.js index c5cfc061..0b3f7dcf 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -24,8 +24,8 @@ var BlockService = function(options) { this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.network]; this._initialSync = false; + this._serviceIniting = false; this._reorgBackToBlock = null; // use this to rewind your indexes to a specific point by height or hash - this._timeOfLastBlockReport = Date.now() - 30000; this._blocksInQueue = 0; }; @@ -524,20 +524,40 @@ BlockService.prototype._removeAllSubscriptions = function() { this._bus.removeAllListeners(); this.removeAllListeners(); // will remove listeners for 'next block' and 'synced' this._subscribedBlock = false; + if (this._reportInterval) { + clearInterval(this._reportInterval); + } + if (this._getBlocksTimer) { + clearTimeout(this._getBlocksTimer); + } }; BlockService.prototype.onHeaders = function(callback) { var self = this; - // this fires under 2 conditions: - // 1. on initial boot right after all headers are synced by the header service - // 2. right after the header service handles a reorg + // at this point, we need to ensure the block header is in a very specific state + // 1. we should have no listeners of any kind that could cause side effects + // 2. we should have no pending events, yet to be fired, after this routine yields self._initialSync = true; + self._serviceIniting = true; self._removeAllSubscriptions(); + // this should ensure that any handlers, yet to be fired, will fire + setImmediate(function() { + self._onHeaders(callback); + }); + +}; + +BlockService.prototype._onHeaders = function(callback) { + + var self = this; + + self._serviceIniting = false; + // check whether or not we need build a new tip (unlikely) self._resetTip(function(err) { @@ -720,7 +740,7 @@ BlockService.prototype._onBlock = function(block, callback) { var self = this; - if (self._reorging) { + if (self._reorging || self._serviceIniting) { return callback(); } @@ -872,6 +892,10 @@ BlockService.prototype._onSynced = function() { clearInterval(this._reportInterval); } + if (this._serviceIniting) { + return; + } + self._logProgress(); self._initialSync = false; self._startBlockSubscription(); @@ -888,7 +912,7 @@ BlockService.prototype._startSync = function() { this.on('next block', this._sync.bind(this)); this.on('synced', this._onSynced.bind(this)); clearInterval(this._reportInterval); - this._reportingInterval = setInterval(this._reportStatus.bind(this), 5000); + this._reportingInterval = setInterval(this._logProgress.bind(this), 5000); return this._sync(); } @@ -896,13 +920,6 @@ BlockService.prototype._startSync = function() { }; -BlockService.prototype._reportStatus = function() { - if (this._tip.height % 144 === 0 || Date.now() - this._timeOfLastBlockReport > 10000) { - this._timeOfLastBlockReport = Date.now(); - this._logProgress(); - } -}; - BlockService.prototype._sync = function() { var self = this; diff --git a/package-lock.json b/package-lock.json index 540c0e31..ceb1a2c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4266,6 +4266,14 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz", "integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4=" }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "requires": { + "safe-buffer": "5.1.1" + } + }, "string-length": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/string-length/-/string-length-1.0.1.tgz", @@ -4285,14 +4293,6 @@ "strip-ansi": "3.0.1" } }, - "string_decoder": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", - "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", - "requires": { - "safe-buffer": "5.1.1" - } - }, "stringstream": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz",