From e650561b7e52ed3ec5165ee1b3c74d90cd8f81e3 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 25 Jul 2016 17:28:54 -0400 Subject: [PATCH 1/3] Use last block time for moreTs --- lib/blocks.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/blocks.js b/lib/blocks.js index 4d286a523..e8ae49056 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -168,13 +168,10 @@ BlockController.prototype.blockIndex = function(req, res) { }); }; -BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next) { +BlockController.prototype._getBlockSummary = function(hash, next) { var self = this; function finish(result) { - if (moreTimestamp > result.time) { - moreTimestamp = result.time; - } return next(null, result); } @@ -257,7 +254,6 @@ BlockController.prototype.list = function(req, res) { var next = lte ? this.formatTimestamp(new Date(lte * 1000)) : null; var limit = parseInt(req.query.limit || BLOCK_LIMIT); var more = false; - var moreTimestamp = lte; self.node.services.bitcoind.getBlockHashesByTimestamp(lte, gte, function(err, hashes) { if(err) { @@ -274,7 +270,7 @@ BlockController.prototype.list = function(req, res) { async.mapSeries( hashes, function(hash, next) { - self._getBlockSummary(hash, moreTimestamp, next); + self._getBlockSummary(hash, next); }, function(err, blocks) { if(err) { @@ -299,7 +295,7 @@ BlockController.prototype.list = function(req, res) { }; if(more) { - data.pagination.moreTs = moreTimestamp; + data.pagination.moreTs = blocks[blocks.length - 1].time; } res.jsonp(data); From 3bbdb09c9bd5fa7648613038e2fc32011e8e2997 Mon Sep 17 00:00:00 2001 From: Chethan Krishna Date: Thu, 28 Jul 2016 16:38:29 -0400 Subject: [PATCH 2/3] Use logical timestamp to display blocks --- lib/blocks.js | 21 +++++++++------------ test/blocks.js | 6 +++--- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/blocks.js b/lib/blocks.js index e8ae49056..a46a6aa76 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -246,16 +246,17 @@ BlockController.prototype.list = function(req, res) { isToday = true; } - var gte = Math.round((new Date(dateStr)).getTime() / 1000); + var low = Math.round((new Date(dateStr)).getTime() / 1000); //pagination - var lte = parseInt(req.query.startTimestamp) || gte + 86400; - var prev = this.formatTimestamp(new Date((gte - 86400) * 1000)); - var next = lte ? this.formatTimestamp(new Date(lte * 1000)) : null; + var high = parseInt(req.query.startTimestamp) || low + 86400; + var prev = this.formatTimestamp(new Date((low - 86400) * 1000)); + var next = high ? this.formatTimestamp(new Date(high * 1000)) : null; var limit = parseInt(req.query.limit || BLOCK_LIMIT); var more = false; - self.node.services.bitcoind.getBlockHashesByTimestamp(lte, gte, function(err, hashes) { + var options = {'noOrphans':false, 'logicalTimes':true}; + self.node.services.bitcoind.getBlockHashesByTimestamp(high, low, options, function(err, hashes) { if(err) { return self.common.handleErrors(err, res); } @@ -270,24 +271,20 @@ BlockController.prototype.list = function(req, res) { async.mapSeries( hashes, function(hash, next) { - self._getBlockSummary(hash, next); + self._getBlockSummary(hash.blockhash, next); }, function(err, blocks) { if(err) { return self.common.handleErrors(err, res); } - blocks.sort(function(a, b) { - return b.height - a.height; - }); - var data = { blocks: blocks, length: blocks.length, pagination: { next: next, prev: prev, - currentTs: lte - 1, + currentTs: high - 1, current: dateStr, isToday: isToday, more: more @@ -295,7 +292,7 @@ BlockController.prototype.list = function(req, res) { }; if(more) { - data.pagination.moreTs = blocks[blocks.length - 1].time; + data.pagination.moreTs = hashes[hashes.length - 1].logicalts - 1; } res.jsonp(data); diff --git a/test/blocks.js b/test/blocks.js index f88cdec5b..8673a2967 100644 --- a/test/blocks.js +++ b/test/blocks.js @@ -179,8 +179,8 @@ describe('Blocks', function() { stub.onSecondCall().callsArgWith(1, null, new Buffer(blocks['00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441'], 'hex')); var hashes = [ - '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441', - '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7' + {blockhash: '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441', logicalts: 12345678}, + {blockhash: '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7', logicalts: 12345678} ]; var node = { log: sinon.stub(), @@ -190,7 +190,7 @@ describe('Blocks', function() { getBlockHeader: function(hash, callback) { callback(null, blockIndexes[hash]); }, - getBlockHashesByTimestamp: sinon.stub().callsArgWith(2, null, hashes) + getBlockHashesByTimestamp: sinon.stub().callsArgWith(3, null, hashes) } } }; From 1ee92ea57edbf50a7e70215e8f2706c28f39cd45 Mon Sep 17 00:00:00 2001 From: Chethan Krishna Date: Fri, 19 Aug 2016 16:13:44 -0400 Subject: [PATCH 3/3] remove -1 as returned result is inclusive --- lib/blocks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/blocks.js b/lib/blocks.js index a46a6aa76..d7fa09cd2 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -292,7 +292,7 @@ BlockController.prototype.list = function(req, res) { }; if(more) { - data.pagination.moreTs = hashes[hashes.length - 1].logicalts - 1; + data.pagination.moreTs = hashes[hashes.length - 1].logicalts; } res.jsonp(data);