Fixed timestamp service for the proper block hashes.
This commit is contained in:
parent
cbf59e8e72
commit
3575b6eda3
@ -76,15 +76,15 @@ BlockService.prototype.getBlockOverview = function(hash, callback) {
|
|||||||
var header = block.toHeaders().toJSON();
|
var header = block.toHeaders().toJSON();
|
||||||
|
|
||||||
var blockOverview = {
|
var blockOverview = {
|
||||||
hash: block.hash,
|
hash: block.rhash(),
|
||||||
version: header.version,
|
version: block.version,
|
||||||
confirmations: null,
|
confirmations: null,
|
||||||
height: header.height,
|
height: header.height,
|
||||||
chainWork: header.chainwork,
|
chainWork: header.chainwork,
|
||||||
prevHash: header.prevBlock,
|
prevHash: header.prevBlock,
|
||||||
nextHash: null,
|
nextHash: null,
|
||||||
merkleRoot: block.merkleroot,
|
merkleRoot: block.merkleroot,
|
||||||
time: header.timestamp,
|
time: block.ts,
|
||||||
medianTime: null,
|
medianTime: null,
|
||||||
nonce: block.nonce,
|
nonce: block.nonce,
|
||||||
bits: block.bits,
|
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
|
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: ' +
|
log.warn('Block Service: Chain reorganization detected! Our current block tip is: ' +
|
||||||
this._tip.hash + ' the current block: ' + hash + '.');
|
this._tip.hash + ' the current block: ' + hash + '.');
|
||||||
|
|
||||||
@ -487,7 +486,8 @@ BlockService.prototype._onBlock = function(block, header) {
|
|||||||
|
|
||||||
var reorg = this._detectReorg(block);
|
var reorg = this._detectReorg(block);
|
||||||
if (reorg) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -498,8 +498,14 @@ BlockService.prototype._onBlock = function(block, header) {
|
|||||||
|
|
||||||
BlockService.prototype._setListeners = function() {
|
BlockService.prototype._setListeners = function() {
|
||||||
|
|
||||||
this._header.once('headers', this._onAllHeaders.bind(this));
|
var self = this;
|
||||||
this._header.on('reorg', this._handleReorg.bind(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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -72,7 +72,7 @@ HeaderService.prototype.getBlockHeader = function(arg, callback) {
|
|||||||
return callback(null, this._headers.getIndex(arg));
|
return callback(null, this._headers.getIndex(arg));
|
||||||
}
|
}
|
||||||
|
|
||||||
return callback(this._headers.get(arg));
|
return callback(null, this._headers.get(arg));
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -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,
|
assert(_.isNumber(low) && _.isNumber(high) && low < high,
|
||||||
'start time and end time must be integers representing the number of seconds since epoch.');
|
'start time and end time must be integers representing the number of seconds since epoch.');
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var result;
|
var result = [];
|
||||||
var lastEntry;
|
var lastEntry;
|
||||||
|
|
||||||
var start = self._encoding.encodeTimestampBlockKey(low);
|
var start = self._encoding.encodeTimestampBlockKey(low);
|
||||||
|
var end = self._encoding.encodeTimestampBlockKey(high);
|
||||||
|
|
||||||
var criteria = {
|
var criteria = {
|
||||||
gte: start,
|
gte: start,
|
||||||
lt: utils.getTerminalKey(start)
|
lte: end
|
||||||
};
|
};
|
||||||
|
|
||||||
var tsStream = self._db.createReadStream(criteria);
|
var tsStream = self._db.createReadStream(criteria);
|
||||||
|
|
||||||
tsStream.on('data', function(data) {
|
tsStream.on('data', function(data) {
|
||||||
var value = self._encoding.decodeTimestampBlockValue(data.value);
|
var value = self._encoding.decodeTimestampBlockValue(data.value);
|
||||||
if (!result) {
|
console.log(value);
|
||||||
result.push(value);
|
result.push(value);
|
||||||
}
|
|
||||||
lastEntry = value;
|
lastEntry = value;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -155,6 +155,7 @@ TimestampService.prototype.onBlock = function(block, callback) {
|
|||||||
var operations = [];
|
var operations = [];
|
||||||
|
|
||||||
var ts = block.ts;
|
var ts = block.ts;
|
||||||
|
var hash = block.rhash();
|
||||||
|
|
||||||
if (ts <= this._lastBlockTimestamp) {
|
if (ts <= this._lastBlockTimestamp) {
|
||||||
ts = this._lastBlockTimestamp + 1;
|
ts = this._lastBlockTimestamp + 1;
|
||||||
@ -162,9 +163,9 @@ TimestampService.prototype.onBlock = function(block, callback) {
|
|||||||
|
|
||||||
this._lastBlockTimestamp = ts;
|
this._lastBlockTimestamp = ts;
|
||||||
|
|
||||||
this._tip.hash = block.hash;
|
this._tip.hash = hash;
|
||||||
this._tip.height++;
|
this._tip.height++;
|
||||||
this._cache.set(block.hash, ts);
|
this._cache.set(hash, ts);
|
||||||
|
|
||||||
var tipInfo = utils.encodeTip(this._tip, this.name);
|
var tipInfo = utils.encodeTip(this._tip, this.name);
|
||||||
|
|
||||||
@ -172,11 +173,11 @@ TimestampService.prototype.onBlock = function(block, callback) {
|
|||||||
{
|
{
|
||||||
type: 'put',
|
type: 'put',
|
||||||
key: this._encoding.encodeTimestampBlockKey(ts),
|
key: this._encoding.encodeTimestampBlockKey(ts),
|
||||||
value: this._encoding.encodeTimestampBlockValue(block.hash)
|
value: this._encoding.encodeTimestampBlockValue(hash)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'put',
|
type: 'put',
|
||||||
key: this._encoding.encodeBlockTimestampKey(block.hash),
|
key: this._encoding.encodeBlockTimestampKey(hash),
|
||||||
value: this._encoding.encodeBlockTimestampValue(ts)
|
value: this._encoding.encodeBlockTimestampValue(ts)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -214,11 +215,11 @@ TimestampService.prototype._onReorg = function(oldBlockList, newBlockList, commo
|
|||||||
removalOps.concat([
|
removalOps.concat([
|
||||||
{
|
{
|
||||||
type: 'del',
|
type: 'del',
|
||||||
key: this.encoding.encodeTimestampBlockKey(block.header.timestamp),
|
key: this.encoding.encodeTimestampBlockKey(block.ts),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'del',
|
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);
|
this._db.batch(removalOps);
|
||||||
|
|
||||||
// set the last time stamp to the common ancestor
|
// 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
|
//call onBlock for each of the new blocks
|
||||||
newBlockList.forEach(this._onBlock.bind(this));
|
newBlockList.forEach(this._onBlock.bind(this));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user