Fixed timestamp service for the proper block hashes.

This commit is contained in:
Chris Kleeschulte 2017-08-09 19:40:40 -04:00
parent cbf59e8e72
commit 3575b6eda3
3 changed files with 29 additions and 22 deletions

View File

@ -76,15 +76,15 @@ BlockService.prototype.getBlockOverview = function(hash, callback) {
var header = block.toHeaders().toJSON();
var blockOverview = {
hash: block.hash,
version: header.version,
hash: block.rhash(),
version: block.version,
confirmations: null,
height: header.height,
chainWork: header.chainwork,
prevHash: header.prevBlock,
nextHash: null,
merkleRoot: block.merkleroot,
time: header.timestamp,
time: block.ts,
medianTime: null,
nonce: block.nonce,
bits: block.bits,
@ -296,11 +296,10 @@ BlockService.prototype._getHash = function(blockArg) {
};
BlockService.prototype._handleReorg = function(block, allHeaders) {
BlockService.prototype._handleReorg = function(hash, allHeaders) {
this._reorging = true; // while this is set, we won't be sending blocks
var hash = block.rhash();
log.warn('Block Service: Chain reorganization detected! Our current block tip is: ' +
this._tip.hash + ' the current block: ' + hash + '.');
@ -487,7 +486,8 @@ BlockService.prototype._onBlock = function(block, header) {
var reorg = this._detectReorg(block);
if (reorg) {
this._handleReorg(block, this._header.getAllHeaders());
log.debug('Block Service: detected a reorg during onBlock handler');
this._handleReorg(block.rhash(), this._header.getAllHeaders());
return;
}
@ -498,8 +498,14 @@ BlockService.prototype._onBlock = function(block, header) {
BlockService.prototype._setListeners = function() {
this._header.once('headers', this._onAllHeaders.bind(this));
this._header.on('reorg', this._handleReorg.bind(this));
var self = this;
self._header.once('headers', self._onAllHeaders.bind(self));
self._header.on('reorg', function(hash, headers) {
if (!self._reorging) {
log.debug('Block Service: detected a reorg from the header service.');
self._handleReorg(hash, headers);
}
});
};

View File

@ -72,7 +72,7 @@ HeaderService.prototype.getBlockHeader = function(arg, callback) {
return callback(null, this._headers.getIndex(arg));
}
return callback(this._headers.get(arg));
return callback(null, this._headers.get(arg));
};

View File

@ -29,29 +29,29 @@ TimestampService.prototype.getAPIMethods = function() {
];
};
TimestampService.prototype.getBlockHashesByTimestamp = function(low, high, callback) {
TimestampService.prototype.getBlockHashesByTimestamp = function(high, low, callback) {
assert(_.isNumber(low) && _.isNumber(high) && low < high,
'start time and end time must be integers representing the number of seconds since epoch.');
var self = this;
var result;
var result = [];
var lastEntry;
var start = self._encoding.encodeTimestampBlockKey(low);
var end = self._encoding.encodeTimestampBlockKey(high);
var criteria = {
gte: start,
lt: utils.getTerminalKey(start)
lte: end
};
var tsStream = self._db.createReadStream(criteria);
tsStream.on('data', function(data) {
var value = self._encoding.decodeTimestampBlockValue(data.value);
if (!result) {
result.push(value);
}
console.log(value);
result.push(value);
lastEntry = value;
});
@ -155,6 +155,7 @@ TimestampService.prototype.onBlock = function(block, callback) {
var operations = [];
var ts = block.ts;
var hash = block.rhash();
if (ts <= this._lastBlockTimestamp) {
ts = this._lastBlockTimestamp + 1;
@ -162,9 +163,9 @@ TimestampService.prototype.onBlock = function(block, callback) {
this._lastBlockTimestamp = ts;
this._tip.hash = block.hash;
this._tip.hash = hash;
this._tip.height++;
this._cache.set(block.hash, ts);
this._cache.set(hash, ts);
var tipInfo = utils.encodeTip(this._tip, this.name);
@ -172,11 +173,11 @@ TimestampService.prototype.onBlock = function(block, callback) {
{
type: 'put',
key: this._encoding.encodeTimestampBlockKey(ts),
value: this._encoding.encodeTimestampBlockValue(block.hash)
value: this._encoding.encodeTimestampBlockValue(hash)
},
{
type: 'put',
key: this._encoding.encodeBlockTimestampKey(block.hash),
key: this._encoding.encodeBlockTimestampKey(hash),
value: this._encoding.encodeBlockTimestampValue(ts)
},
{
@ -214,11 +215,11 @@ TimestampService.prototype._onReorg = function(oldBlockList, newBlockList, commo
removalOps.concat([
{
type: 'del',
key: this.encoding.encodeTimestampBlockKey(block.header.timestamp),
key: this.encoding.encodeTimestampBlockKey(block.ts),
},
{
type: 'del',
key: this.encoding.encodeBlockTimestampKey(block.hash),
key: this.encoding.encodeBlockTimestampKey(block.rhash()),
}
]);
});
@ -226,7 +227,7 @@ TimestampService.prototype._onReorg = function(oldBlockList, newBlockList, commo
this._db.batch(removalOps);
// set the last time stamp to the common ancestor
this._lastBlockTimestamp = commonAncestor.header.timestamp;
this._lastBlockTimestamp = commonAncestor.ts;
//call onBlock for each of the new blocks
newBlockList.forEach(this._onBlock.bind(this));