Fixed issue with tip.

This commit is contained in:
Chris Kleeschulte 2017-08-09 16:43:56 -04:00
parent 613cf6ebfb
commit 9875d81df1

View File

@ -26,6 +26,7 @@ var HeaderService = function(options) {
this.subscriptions.block = []; this.subscriptions.block = [];
this._checkpoint = options.checkpoint || 2000; this._checkpoint = options.checkpoint || 2000;
this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.network]; this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.network];
this._newBlocksHeight = 0;
}; };
inherits(HeaderService, BaseService); inherits(HeaderService, BaseService);
@ -65,14 +66,14 @@ HeaderService.prototype.getAPIMethods = function() {
}; };
HeaderService.prototype.getBlockHeader = function(arg) { HeaderService.prototype.getBlockHeader = function(arg, callback) {
if (utils.isHeight(arg)) { if (utils.isHeight(arg)) {
var header = this._headers.getIndex(arg); return callback(null, this._headers.getIndex(arg));
return header ? header : null;
} }
return this._headers.get(arg); return callback(this._headers.get(arg));
}; };
HeaderService.prototype.getBestHeight = function() { HeaderService.prototype.getBestHeight = function() {
@ -186,6 +187,7 @@ HeaderService.prototype._onBlock = function(block) {
header = block.toHeaders().toJSON(); header = block.toHeaders().toJSON();
header.timestamp = header.ts; header.timestamp = header.ts;
header.prevHash = header.prevBlock; header.prevHash = header.prevBlock;
header.height = ++this._newBlocksHeight;
this._onHeaders([header]); this._onHeaders([header]);
} }
@ -198,7 +200,6 @@ HeaderService.prototype._onHeaders = function(headers) {
var self = this; var self = this;
log.debug('Header Service: Received: ' + headers.length + ' header(s).'); log.debug('Header Service: Received: ' + headers.length + ' header(s).');
var dbOps = []; var dbOps = [];
@ -208,9 +209,9 @@ HeaderService.prototype._onHeaders = function(headers) {
var header = headers[i]; var header = headers[i];
if (header instanceof Header) { if (header instanceof Header) {
header = header.toObject(); header = header.toObject();
header.height = ++self._tip.height;
} }
header.height = ++self._tip.height;
header.chainwork = self._getChainwork(header).toString(16, 64); header.chainwork = self._getChainwork(header).toString(16, 64);
self._lastChainwork = header.chainwork; self._lastChainwork = header.chainwork;
@ -292,6 +293,7 @@ HeaderService.prototype._onBestHeight = function(height) {
height + ' tip height: ' + this._tip.height); height + ' tip height: ' + this._tip.height);
log.debug('Header Service: Best Height is: ' + height); log.debug('Header Service: Best Height is: ' + height);
this._bestHeight = height; this._bestHeight = height;
this._newBlocksHeight = this._bestHeight;
this._startSync(); this._startSync();
}; };
@ -375,7 +377,7 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
}; };
HeaderService.prototype._getChainwork = function(header, prevHeader) { HeaderService.prototype._getChainwork = function(header) {
var prevChainwork = new BN(new Buffer(this._lastChainwork || HeaderService.STARTING_CHAINWORK, 'hex')); var prevChainwork = new BN(new Buffer(this._lastChainwork || HeaderService.STARTING_CHAINWORK, 'hex'));