diff --git a/lib/services/header/index.js b/lib/services/header/index.js index 18369321..d1469007 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -259,7 +259,11 @@ HeaderService.prototype._onHeaders = function(headers) { HeaderService.prototype._detectReorg = function() { // is our original tip's height and hash the same after we rewound by the checkpoint amount of blocks // and re-imported? If so, then we've reorg'ed since we've been shut down. - if (this._originalTip.hash !== this._headers.getIndex(this._originalTip.height).hash) { + + var headerHash = this._headers.getIndex(this._originalTip.height).hash; + assert(headerHash, 'Expected a header to exist at height ' + this._originalTip.height); + + if (this._originalTip.hash !== headerHash) { return true; } return false; @@ -285,7 +289,7 @@ HeaderService.prototype._onBestHeight = function(height) { HeaderService.prototype._startSync = function() { - this._numNeeded = this._bestHeight - this._tip.height; + this._numNeeded = Math.max(this._bestHeight - this._tip.height, this._checkpoint); log.info('Header Service: Gathering: ' + this._numNeeded + ' ' + 'header(s) from the peer-to-peer network.'); diff --git a/test/services/header/index.unit.js b/test/services/header/index.unit.js index 8c8e211c..02951e83 100644 --- a/test/services/header/index.unit.js +++ b/test/services/header/index.unit.js @@ -109,6 +109,11 @@ describe('Header Service', function() { headerService._tip = { height: 0 }; headerService._originalTip = { height: 0, hash: header.hash }; headerService._bestHeight = { height: 1 }; + headerService._headers = { + getIndex: function() { return { hash: header.hash }; }, + getLastIndex: sinon.stub(), + set: sinon.stub() + }; var getChainwork = sandbox.stub(headerService, '_getChainwork').returns(new BN(1)); var headers = [ header ];