diff --git a/lib/services/block/index.js b/lib/services/block/index.js index 1663cb84..da34ad59 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -13,6 +13,8 @@ var bcoin = require('fcoin'); var _ = require('lodash'); var LRU = require('lru-cache'); +const MAX_IGNORED_BLOCK = 32; //Maximum ignored block allowed before trigging sync again + var BlockService = function(options) { BaseService.call(this, options); @@ -637,6 +639,7 @@ BlockService.prototype._startBlockSubscription = function() { } this._subscribedBlock = true; + this._ignoredBlockCount = 0; //SZ: reset the ignored count to 0 when subscription starts log.info('Block Service: starting p2p block subscription.'); this._bus.on('p2p/block', this._queueBlock.bind(this)); @@ -938,6 +941,15 @@ BlockService.prototype._processBlock = function(block, callback) { return self._saveBlock(block, callback); } + //SZ: count the ignored blocks. if many blocks ignored, trigger sync process + if(self._ignoredBlockCount < MAX_IGNORED_BLOCK) + self._ignoredBlockCount++; + else { + self._ignoredBlockCount = 0; + self._removeAllSubscriptions(); + self._startSync(); + } + // reorg -- in this case, we will not handle the reorg right away // instead, we will skip the block and wait for the eventual call to // "onHeaders" function. When the header service calls this function, @@ -953,6 +965,7 @@ BlockService.prototype._saveBlock = function(block, callback) { var self = this; block.__height = self._tip.height + 1; + self._ignoredBlockCount = 0; //SZ: a block is saved, reset the ignored count var services = self.node.services; @@ -1097,7 +1110,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._logProgress.bind(this), 5000); + this._reportInterval = setInterval(this._logProgress.bind(this), 5000); return this._sync(); }