Merge pull request #8 from ranchimall/block-subscribe-stuck-fix

Fix: Block subscribe getting stuck
This commit is contained in:
Sai Raj 2023-01-18 00:24:09 +05:30 committed by GitHub
commit 25eb992cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,8 @@ var bcoin = require('fcoin');
var _ = require('lodash');
var LRU = require('lru-cache');
const MAX_IGNORED_BLOCK = 16; //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();
}