promisify blocks
This commit is contained in:
parent
7c3231ac3c
commit
591f28f56b
@ -5,6 +5,8 @@ var _ = bitcore.deps._;
|
|||||||
var $ = bitcore.util.preconditions;
|
var $ = bitcore.util.preconditions;
|
||||||
var Block = bitcore.Block;
|
var Block = bitcore.Block;
|
||||||
|
|
||||||
|
var BitcoreNode = require('../../');
|
||||||
|
|
||||||
var Blocks = {};
|
var Blocks = {};
|
||||||
|
|
||||||
var node;
|
var node;
|
||||||
@ -21,14 +23,14 @@ Blocks.setNode = function(aNode) {
|
|||||||
* Finds a block by its hash
|
* Finds a block by its hash
|
||||||
*/
|
*/
|
||||||
Blocks.blockHashParam = function(req, res, next, blockHash) {
|
Blocks.blockHashParam = function(req, res, next, blockHash) {
|
||||||
var block = node.getBlock(blockHash);
|
node.getBlock(blockHash)
|
||||||
|
.then(function(block) {
|
||||||
if (_.isUndefined(block)) {
|
req.block = block;
|
||||||
res.status(404).send('Block with id ' + blockHash + ' not found');
|
})
|
||||||
return;
|
.then(next)
|
||||||
}
|
.catch(BitcoreNode.errors.Blocks.NotFound, function() {
|
||||||
req.block = block;
|
res.status(404).send('Block with id ' + blockHash + ' not found');
|
||||||
next();
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -36,14 +38,14 @@ Blocks.blockHashParam = function(req, res, next, blockHash) {
|
|||||||
*/
|
*/
|
||||||
Blocks.heightParam = function(req, res, next, height) {
|
Blocks.heightParam = function(req, res, next, height) {
|
||||||
height = parseInt(height);
|
height = parseInt(height);
|
||||||
var block = node.getBlock(height);
|
node.getBlock(height)
|
||||||
|
.then(function(block) {
|
||||||
if (_.isUndefined(block)) {
|
req.block = block;
|
||||||
res.status(404).send('Block with height ' + height + ' not found');
|
})
|
||||||
return;
|
.then(next)
|
||||||
}
|
.catch(BitcoreNode.errors.Blocks.NotFound, function() {
|
||||||
req.block = block;
|
res.status(404).send('Block with height ' + height + ' not found');
|
||||||
next();
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,13 @@ var should = chai.should();
|
|||||||
var request = require('supertest');
|
var request = require('supertest');
|
||||||
|
|
||||||
var EventEmitter = require('eventemitter2').EventEmitter2;
|
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||||
|
var Promise = require('bluebird');
|
||||||
|
Promise.longStackTraces();
|
||||||
|
var bitcore = require('bitcore');
|
||||||
|
var _ = bitcore.deps._;
|
||||||
|
|
||||||
var BitcoreHTTP = require('../../lib/http');
|
var BitcoreHTTP = require('../../lib/http');
|
||||||
|
var BitcoreNode = require('../../../');
|
||||||
var mockBlocks = require('../data/blocks');
|
var mockBlocks = require('../data/blocks');
|
||||||
|
|
||||||
describe('BitcoreHTTP v1 blocks routes', function() {
|
describe('BitcoreHTTP v1 blocks routes', function() {
|
||||||
@ -18,11 +23,18 @@ describe('BitcoreHTTP v1 blocks routes', function() {
|
|||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
nodeMock = new EventEmitter();
|
nodeMock = new EventEmitter();
|
||||||
nodeMock.getBlock = function(blockHash) {
|
nodeMock.getBlock = function(blockHash) {
|
||||||
|
var block;
|
||||||
if (typeof blockHash === 'number') {
|
if (typeof blockHash === 'number') {
|
||||||
var height = blockHash;
|
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() {
|
nodeMock.getLatestBlock = function() {
|
||||||
return mockBlocks[Object.keys(mockBlocks).splice(-1)[0]];
|
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