promisify blocks
This commit is contained in:
parent
7c3231ac3c
commit
591f28f56b
@ -5,6 +5,8 @@ var _ = bitcore.deps._;
|
||||
var $ = bitcore.util.preconditions;
|
||||
var Block = bitcore.Block;
|
||||
|
||||
var BitcoreNode = require('../../');
|
||||
|
||||
var Blocks = {};
|
||||
|
||||
var node;
|
||||
@ -21,14 +23,14 @@ Blocks.setNode = function(aNode) {
|
||||
* Finds a block by its hash
|
||||
*/
|
||||
Blocks.blockHashParam = function(req, res, next, blockHash) {
|
||||
var block = node.getBlock(blockHash);
|
||||
|
||||
if (_.isUndefined(block)) {
|
||||
res.status(404).send('Block with id ' + blockHash + ' not found');
|
||||
return;
|
||||
}
|
||||
req.block = block;
|
||||
next();
|
||||
node.getBlock(blockHash)
|
||||
.then(function(block) {
|
||||
req.block = block;
|
||||
})
|
||||
.then(next)
|
||||
.catch(BitcoreNode.errors.Blocks.NotFound, function() {
|
||||
res.status(404).send('Block with id ' + blockHash + ' not found');
|
||||
});
|
||||
};
|
||||
|
||||
/*
|
||||
@ -36,14 +38,14 @@ Blocks.blockHashParam = function(req, res, next, blockHash) {
|
||||
*/
|
||||
Blocks.heightParam = function(req, res, next, height) {
|
||||
height = parseInt(height);
|
||||
var block = node.getBlock(height);
|
||||
|
||||
if (_.isUndefined(block)) {
|
||||
res.status(404).send('Block with height ' + height + ' not found');
|
||||
return;
|
||||
}
|
||||
req.block = block;
|
||||
next();
|
||||
node.getBlock(height)
|
||||
.then(function(block) {
|
||||
req.block = block;
|
||||
})
|
||||
.then(next)
|
||||
.catch(BitcoreNode.errors.Blocks.NotFound, function() {
|
||||
res.status(404).send('Block with height ' + height + ' not found');
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -5,8 +5,13 @@ var should = chai.should();
|
||||
var request = require('supertest');
|
||||
|
||||
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||
var Promise = require('bluebird');
|
||||
Promise.longStackTraces();
|
||||
var bitcore = require('bitcore');
|
||||
var _ = bitcore.deps._;
|
||||
|
||||
var BitcoreHTTP = require('../../lib/http');
|
||||
var BitcoreNode = require('../../../');
|
||||
var mockBlocks = require('../data/blocks');
|
||||
|
||||
describe('BitcoreHTTP v1 blocks routes', function() {
|
||||
@ -18,11 +23,18 @@ describe('BitcoreHTTP v1 blocks routes', function() {
|
||||
beforeEach(function() {
|
||||
nodeMock = new EventEmitter();
|
||||
nodeMock.getBlock = function(blockHash) {
|
||||
var block;
|
||||
if (typeof blockHash === 'number') {
|
||||
var height = blockHash;
|
||||
return mockBlocks[Object.keys(mockBlocks)[height]];
|
||||
block = mockBlocks[Object.keys(mockBlocks)[height]];
|
||||
} else {
|
||||
block = mockBlocks[blockHash];
|
||||
}
|
||||
return mockBlocks[blockHash];
|
||||
if (_.isUndefined(block)) {
|
||||
return Promise.reject(new BitcoreNode.errors.Blocks.NotFound(blockHash));
|
||||
}
|
||||
return Promise.resolve(block);
|
||||
|
||||
};
|
||||
nodeMock.getLatestBlock = function() {
|
||||
return mockBlocks[Object.keys(mockBlocks).splice(-1)[0]];
|
||||
|
||||
26
lib/errors.js
Normal file
26
lib/errors.js
Normal file
@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
var spec = {
|
||||
name: 'BitcoreNode',
|
||||
message: 'Internal Error on BitcoreNode',
|
||||
errors: [{
|
||||
name: 'Transactions',
|
||||
message: 'Internal Transactions error on BitcoreNode',
|
||||
errors: [{
|
||||
name: 'NotFound',
|
||||
message: 'Transaction {0} not found'
|
||||
}, {
|
||||
name: 'CantBroadcast',
|
||||
message: 'Unable to broadcast transaction {0}'
|
||||
}]
|
||||
}, {
|
||||
name: 'Blocks',
|
||||
message: 'Internal Blocks error on BitcoreNode',
|
||||
errors: [{
|
||||
name: 'NotFound',
|
||||
message: 'Block {0} not found'
|
||||
}]
|
||||
}]
|
||||
};
|
||||
|
||||
module.exports = require('bitcore').errors.extend(spec);
|
||||
Loading…
Reference in New Issue
Block a user