fix edge case for listBlocks

This commit is contained in:
Manuel Araoz 2015-04-28 19:39:29 -03:00
parent 68a9e92955
commit 50413c8204
2 changed files with 12 additions and 5 deletions

7
api/test/app.js Normal file
View File

@ -0,0 +1,7 @@
'use strict';
var BitcoreHTTP = require('../lib/http');
module.exports = function(nodeMock) {
return process.env.INTEGRATION === 'true' ? BitcoreHTTP.create().app : new BitcoreHTTP(nodeMock).app;
};

View File

@ -205,6 +205,7 @@ BlockService.prototype.listBlocks = function(from, to, offset, limit) {
var start = from + offset;
var end = Math.min(to, start + limit - 1);
var blocks = [];
// TODO: optimize: precompute heights and fetch all blocks in parallel?
var fetchBlock = function(height) {
if (height > end) {
return;
@ -212,12 +213,11 @@ BlockService.prototype.listBlocks = function(from, to, offset, limit) {
console.log('fetching block', height);
return self.getBlockByHeight(height)
.then(function(block) {
if (!block) {
// TODO: report?
return;
}
blocks.push(block);
blocks.push(block.toObject());
return fetchBlock(height + 1);
})
.catch(function(err) {
console.log(err);
});
};
return fetchBlock(start)