Merge pull request #7 from ranchimall/dev2

Dev2
This commit is contained in:
Sai Raj 2023-01-17 21:56:20 +05:30 committed by GitHub
commit ca54b05618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -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();
}

View File

@ -533,8 +533,10 @@ HeaderService.prototype._onHeadersSave = function(callback) {
}
//SZ: clear the interval check as sync is completed
if(self._syncCheckInterval)
if(self._syncCheckInterval){
clearInterval(self._syncCheckInterval);
self._syncCheckInterval = null;
}
self._endHeaderSubscription(); // we don't need headers any more
self._startBlockSubscription(); // we need new blocks coming tu us aynchronuously