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