Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions lib/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -249,17 +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;
var moreTimestamp = lte;

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);
}
Expand All @@ -274,32 +271,28 @@ BlockController.prototype.list = function(req, res) {
async.mapSeries(
hashes,
function(hash, next) {
self._getBlockSummary(hash, moreTimestamp, 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
}
};

if(more) {
data.pagination.moreTs = moreTimestamp;
data.pagination.moreTs = hashes[hashes.length - 1].logicalts;
}

res.jsonp(data);
Expand Down
6 changes: 3 additions & 3 deletions test/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
}
}
};
Expand Down