timestamp service fixes

This commit is contained in:
Patrick Nagurny 2017-01-19 16:48:29 -05:00
parent efa8480651
commit c6e2c57778

View File

@ -1,5 +1,5 @@
'use strict';
var BaseService = require('../../service');
var BaseService = require('../service');
var inherits = require('util').inherits;
function TimestampService(options) {
@ -44,11 +44,12 @@ TimestampService.prototype.blockHandler = function(block, connectBlock, callback
var operations = [];
function getLastTimestamp(next) {
var timestamp;
if(!block.header.prevHash) {
// Genesis block
return next(null, 0);
} else if(block.__height === 1) {
// TODO fix bug where genesis block doesn't get indexed
return next(null, 0);
}
self.getTimestamp(block.header.prevHash.reverse().toString('hex'), next);
@ -64,33 +65,44 @@ TimestampService.prototype.blockHandler = function(block, connectBlock, callback
timestamp = lastTimestamp + 1;
}
operations.push({
type: action,
key: self._encodeTimestampBlockKey(timestamp),
value: self._encodeTimestampBlockValue(block.header.hash)
});
operations = operations.concat(
[
{
type: action,
key: self._encodeTimestampBlockKey(timestamp),
value: self._encodeTimestampBlockValue(block.header.hash)
},
{
type: action,
key: self._encodeBlockTimestampKey(block.header.hash),
value: self._encodeBlockTimestampValue(timestamp)
}
]
);
callback(null, operations);
});
};
Timestamp.prototype.getTimestamp = function(hash, callback) {
var key = this._encodeBlockTimestampKey(hash);
this.store.get(key, function(err, buffer) {
TimestampService.prototype.getTimestamp = function(hash, callback) {
var self = this;
var key = self._encodeBlockTimestampKey(hash);
self.store.get(key, function(err, buffer) {
if(err) {
return callback(err);
}
return callback(null, this._decodeBlockTimestampValue(buffer));
return callback(null, self._decodeBlockTimestampValue(buffer));
});
};
TimestampService.prototype._encodeBlockTimestampKey = function(hash) {
return Buffer.concat([self.prefix, new Buffer(hash, 'hex')]);
return Buffer.concat([this.prefix, new Buffer(hash, 'hex')]);
};
TimestampService.prototype._decodeBlockTimestampKey = function(buffer) {
return buffer.slice(1).toString('hex');
return buffer.slice(2).toString('hex');
};
TimestampService.prototype._encodeBlockTimestampValue = function(timestamp) {
@ -106,11 +118,11 @@ TimestampService.prototype._decodeBlockTimestampValue = function(buffer) {
TimestampService.prototype._encodeTimestampBlockKey = function(timestamp) {
var timestampBuffer = new Buffer(new Array(8));
timestampBuffer.writeDoubleBE(timestamp);
return Buffer.concat([self.prefix, timestampBuffer]);
return Buffer.concat([this.prefix, timestampBuffer]);
};
TimestampService.prototype._decodeTimestampBlockKey = function(buffer) {
return buffer.readDoubleBE(1);
return buffer.readDoubleBE(2);
};
TimestampService.prototype._encodeTimestampBlockValue = function(hash) {