Skip to content

Commit 3bbdb09

Browse files
author
Chethan Krishna
committed
Use logical timestamp to display blocks
1 parent e650561 commit 3bbdb09

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

lib/blocks.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,17 @@ BlockController.prototype.list = function(req, res) {
246246
isToday = true;
247247
}
248248

249-
var gte = Math.round((new Date(dateStr)).getTime() / 1000);
249+
var low = Math.round((new Date(dateStr)).getTime() / 1000);
250250

251251
//pagination
252-
var lte = parseInt(req.query.startTimestamp) || gte + 86400;
253-
var prev = this.formatTimestamp(new Date((gte - 86400) * 1000));
254-
var next = lte ? this.formatTimestamp(new Date(lte * 1000)) : null;
252+
var high = parseInt(req.query.startTimestamp) || low + 86400;
253+
var prev = this.formatTimestamp(new Date((low - 86400) * 1000));
254+
var next = high ? this.formatTimestamp(new Date(high * 1000)) : null;
255255
var limit = parseInt(req.query.limit || BLOCK_LIMIT);
256256
var more = false;
257257

258-
self.node.services.bitcoind.getBlockHashesByTimestamp(lte, gte, function(err, hashes) {
258+
var options = {'noOrphans':false, 'logicalTimes':true};
259+
self.node.services.bitcoind.getBlockHashesByTimestamp(high, low, options, function(err, hashes) {
259260
if(err) {
260261
return self.common.handleErrors(err, res);
261262
}
@@ -270,32 +271,28 @@ BlockController.prototype.list = function(req, res) {
270271
async.mapSeries(
271272
hashes,
272273
function(hash, next) {
273-
self._getBlockSummary(hash, next);
274+
self._getBlockSummary(hash.blockhash, next);
274275
},
275276
function(err, blocks) {
276277
if(err) {
277278
return self.common.handleErrors(err, res);
278279
}
279280

280-
blocks.sort(function(a, b) {
281-
return b.height - a.height;
282-
});
283-
284281
var data = {
285282
blocks: blocks,
286283
length: blocks.length,
287284
pagination: {
288285
next: next,
289286
prev: prev,
290-
currentTs: lte - 1,
287+
currentTs: high - 1,
291288
current: dateStr,
292289
isToday: isToday,
293290
more: more
294291
}
295292
};
296293

297294
if(more) {
298-
data.pagination.moreTs = blocks[blocks.length - 1].time;
295+
data.pagination.moreTs = hashes[hashes.length - 1].logicalts - 1;
299296
}
300297

301298
res.jsonp(data);

test/blocks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ describe('Blocks', function() {
179179
stub.onSecondCall().callsArgWith(1, null, new Buffer(blocks['00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441'], 'hex'));
180180

181181
var hashes = [
182-
'00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
183-
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'
182+
{blockhash: '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441', logicalts: 12345678},
183+
{blockhash: '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7', logicalts: 12345678}
184184
];
185185
var node = {
186186
log: sinon.stub(),
@@ -190,7 +190,7 @@ describe('Blocks', function() {
190190
getBlockHeader: function(hash, callback) {
191191
callback(null, blockIndexes[hash]);
192192
},
193-
getBlockHashesByTimestamp: sinon.stub().callsArgWith(2, null, hashes)
193+
getBlockHashesByTimestamp: sinon.stub().callsArgWith(3, null, hashes)
194194
}
195195
}
196196
};

0 commit comments

Comments
 (0)