diff --git a/lib/services/block/index.js b/lib/services/block/index.js index d21dec8d..12f4c883 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -28,7 +28,8 @@ var BlockService = function(options) { this._blockCount = 0; this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.network]; this._initialSync = true; - this._reorgBackToBlock = null; + this._reorgBackToBlock = null; // use this to rewind your indexes to a specific point by height or hash + this._timeOfLastBestBlockReport = Date.now() - 30000; }; inherits(BlockService, BaseService); @@ -264,7 +265,7 @@ BlockService.prototype._resetTip = function(callback) { return callback(err || new Error('headers required')); } - log.info('Block Service: retrieved all the headers for lookups'); + log.info('Block Service: retrieved all the headers for lookups.'); async.until(function() { return block; @@ -352,7 +353,6 @@ BlockService.prototype.start = function(callback) { } self._blockProcessor = async.queue(self._onBlock.bind(self)); - self._setListeners(); self._setTip(tip); self._bus = self.node.openBus({remoteAddress: 'localhost-block'}); @@ -382,6 +382,11 @@ BlockService.prototype._queueBlock = function(block) { return self._handleError(err); } + if (Date.now() - self._timeOfLastBestBlockReport > 30000) { // 30 seconds + log.info('Block Service: The best block hash is: ' + self._tip.hash + + ' at height: ' + self._tip.height); + } + log.debug('Block Service: completed processing block: ' + block.rhash() + ' prev hash: ' + bcoin.util.revHex(block.prevBlock) + ' height: ' + self._tip.height); @@ -518,6 +523,8 @@ BlockService.prototype.onHeaders = function(callback) { var self = this; + self._removeAllSubscriptions(); + self._resetTip(function(err) { if (err) { return callback(err); @@ -722,15 +729,6 @@ BlockService.prototype._saveBlock = function(block, callback) { }); }; -BlockService.prototype._onBestHeight = function(height) { - log.info('Block Service: Best Height is: ' + height); - this._removeAllSubscriptions(); -}; - -BlockService.prototype._setListeners = function() { - this._p2p.on('bestHeight', this._onBestHeight.bind(this)); -}; - BlockService.prototype._handleError = function(err) { if (!this.node.stopping) { log.error('Block Service: ' + err); diff --git a/package-lock.json b/package-lock.json index 7bed5eac..25f09f2d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4233,6 +4233,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", @@ -4252,14 +4260,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", diff --git a/test/services/block/index.unit.js b/test/services/block/index.unit.js index 800b9d9a..22ca27b9 100644 --- a/test/services/block/index.unit.js +++ b/test/services/block/index.unit.js @@ -126,17 +126,6 @@ describe('Block Service', function() { }); }); - describe('#_setListeners', function() { - - it('should set listeners for best height', function() { - var on = sandbox.stub(); - blockService._p2p = { on: on }; - blockService._setListeners(); - expect(on.calledOnce).to.be.true; - }); - - }); - describe('#_setTip', function() { it('should set the tip if given a block', function() { @@ -165,7 +154,7 @@ describe('Block Service', function() { it('should get the prefix', function(done) { var getPrefix = sandbox.stub().callsArgWith(1, null, blockService._encoding); var getServiceTip = sandbox.stub().callsArgWith(1, null, { height: 1, hash: 'aa' }); - var setListeners = sandbox.stub(blockService, '_setListeners'); + var performSanityCheck = sandbox.stub(blockService, '_performSanityCheck').callsArgWith(1, null, { hash: 'aa', height: 123 }); var setTip = sandbox.stub(blockService, '_setTip'); blockService.node = { openBus: sandbox.stub() }; blockService._db = { getPrefix: getPrefix, getServiceTip: getServiceTip };