diff --git a/api/controllers/blocks.js b/api/controllers/blocks.js index 7a907896..3ba0d04d 100644 --- a/api/controllers/blocks.js +++ b/api/controllers/blocks.js @@ -2,6 +2,7 @@ var bitcore = require('bitcore'); var _ = bitcore.deps._; +var $ = bitcore.util.preconditions; var Block = bitcore.Block; // mocks @@ -22,21 +23,33 @@ Blocks.blockHashParam = function(req, res, next, blockHash) { var block = mockBlocks[blockHash]; if (_.isUndefined(block)) { - res.status(404).send('Block ' + blockHash + ' not found'); + res.status(404).send('Block with id ' + blockHash + ' not found'); return; } req.block = block; next(); }; +Blocks.heightParam = function(req, res, next, height) { + // TODO: fetch block from service + var block = mockBlocks[Object.keys(mockBlocks)[height]]; + + if (_.isUndefined(block)) { + res.status(404).send('Block with height ' + height + ' not found'); + return; + } + req.block = block; + next(); +}; Blocks.getBlock = function(req, res) { + $.checkState(req.block instanceof Block); res.send(req.block.toObject()); }; Blocks.getBlockError = function(req, res) { res.status(422); - res.send('blockHash parameter must be a 64 digit hex'); + res.send('/v1/blocks/ parameter must be a 64 digit hex or block height integer'); }; module.exports = Blocks; diff --git a/api/routes/v1.js b/api/routes/v1.js index afc98aa6..d4452f89 100644 --- a/api/routes/v1.js +++ b/api/routes/v1.js @@ -20,6 +20,7 @@ function initRouter(node) { // parameter middleware router.param('blockHash', Blocks.blockHashParam); + router.param('height', Blocks.heightParam); // Node routes router.get('/node', NodeStatus.getStatus); @@ -28,8 +29,8 @@ function initRouter(node) { router.get('/blocks', mockResponse); router.get('/blocks/latest', mockResponse); router.get('/blocks/:blockHash([A-Fa-f0-9]{64})', Blocks.getBlock); + router.get('/blocks/:height([0-9]+)', Blocks.getBlock); router.get('/blocks/*', Blocks.getBlockError); - router.get('/blocks/:height([0-9]+)', mockResponse); router.get('/blocks/:blockHash/transactions/:txIndex', mockResponse); // Transaction routes diff --git a/api/test/v1/blocks.js b/api/test/v1/blocks.js index b5a597f0..d10f63e0 100644 --- a/api/test/v1/blocks.js +++ b/api/test/v1/blocks.js @@ -33,12 +33,12 @@ describe('BitcoreHTTP v1 blocks routes', function() { it('fails with invalid blockHash', function(cb) { agent.get('/v1/blocks/abad1dea') .expect(422) - .expect('blockHash parameter must be a 64 digit hex', cb); + .expect('/v1/blocks/ parameter must be a 64 digit hex or block height integer', cb); }); it('returns 404 with non existent block', function(cb) { agent.get('/v1/blocks/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b600000000') .expect(404) - .expect('Block 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b600000000 not found', cb); + .expect('Block with id 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b600000000 not found', cb); }); it('works with valid blockHash', function(cb) { agent.get('/v1/blocks/' + b1.hash) @@ -46,5 +46,22 @@ describe('BitcoreHTTP v1 blocks routes', function() { .expect(b1.toJSON(), cb); }); }); + describe('/blocks/:height', function() { + it('fails with invalid height', function(cb) { + agent.get('/v1/blocks/-15') + .expect(422) + .expect('/v1/blocks/ parameter must be a 64 digit hex or block height integer', cb); + }); + it('returns 404 with non existent block', function(cb) { + agent.get('/v1/blocks/876543') + .expect(404) + .expect('Block with height 876543 not found', cb); + }); + it('works with valid height', function(cb) { + agent.get('/v1/blocks/0') + .expect(200) + .expect(b1.toJSON(), cb); + }); + }); });