Get blocks by height.

This commit is contained in:
Braydon Fuller 2015-07-09 16:45:30 -04:00
parent 69ed6efcb2
commit 24e705b265
2 changed files with 60 additions and 34 deletions

View File

@ -45,7 +45,7 @@ describe('Basic Functionality', function() {
}); });
}); });
describe('will get correct block data', function() { describe('get blocks by hash', function() {
blockData.forEach(function(data) { blockData.forEach(function(data) {
var block = bitcore.Block.fromString(data); var block = bitcore.Block.fromString(data);
@ -58,4 +58,24 @@ describe('Basic Functionality', function() {
}); });
}); });
describe('get blocks by height', function() {
var knownHeights = [
[0, '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'],
[1, '00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048'],
[100000,'000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506'],
[314159, '00000000000000001bb82a7f5973618cfd3185ba1ded04dd852a653f92a27c45']
];
knownHeights.forEach(function(data) {
it('block at height ' + data[0], function(done) {
bitcoind.getBlock(data[0], function(err, response) {
var block = bitcore.Block.fromBuffer(response);
block.hash.should.equal(data[1]);
done();
});
});
});
});
}); });

View File

@ -837,14 +837,20 @@ static void
async_get_block(uv_work_t *req) { async_get_block(uv_work_t *req) {
async_block_data* data = static_cast<async_block_data*>(req->data); async_block_data* data = static_cast<async_block_data*>(req->data);
CBlockIndex* pblockindex;
std::string strHash = data->hash; std::string strHash = data->hash;
uint256 hash(strHash); uint256 hash(strHash);
if (data->height != -1) {
pblockindex = chainActive[data->height];
} else {
if (mapBlockIndex.count(hash) == 0) { if (mapBlockIndex.count(hash) == 0) {
data->err_msg = std::string("Block not found."); data->err_msg = std::string("Block not found.");
return;
} else { } else {
pblockindex = mapBlockIndex[hash];
CBlockIndex* pblockindex = mapBlockIndex[hash]; }
}
const CDiskBlockPos& pos = pblockindex->GetBlockPos(); const CDiskBlockPos& pos = pblockindex->GetBlockPos();
@ -876,7 +882,7 @@ async_get_block(uv_work_t *req) {
data->buffer = buffer; data->buffer = buffer;
data->size = size; data->size = size;
data->cblock_index = pblockindex; data->cblock_index = pblockindex;
}
} }
static void static void