fixes
This commit is contained in:
parent
895e46dcac
commit
4ae6377548
|
@ -69,17 +69,17 @@ describe('Node Functionality', function() {
|
|||
{
|
||||
name: 'db',
|
||||
module: DBService,
|
||||
dependencies: DBService.dependencies
|
||||
config: {}
|
||||
},
|
||||
{
|
||||
name: 'bitcoind',
|
||||
module: BitcoinService,
|
||||
dependencies: BitcoinService.dependencies
|
||||
config: {}
|
||||
},
|
||||
{
|
||||
name: 'address',
|
||||
module: AddressService,
|
||||
dependencies: AddressService.dependencies
|
||||
config: {}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
|
|
@ -235,16 +235,20 @@ describe('Daemon Binding Functionality', function() {
|
|||
});
|
||||
|
||||
describe('get block index by height', function() {
|
||||
it('should get block index by height', function() {
|
||||
var blockIndex = bitcoind.getBlockIndex(2);
|
||||
should.exist(blockIndex);
|
||||
should.exist(blockIndex.chainWork);
|
||||
var work = new BN(blockIndex.chainWork, 'hex');
|
||||
work.cmp(new BN(8)).should.equal(0);
|
||||
should.exist(blockIndex.prevHash);
|
||||
blockIndex.hash.should.equal(blockHashes[1]);
|
||||
blockIndex.prevHash.should.equal(blockHashes[0]);
|
||||
blockIndex.height.should.equal(2);
|
||||
var expectedWork = new BN(6);
|
||||
[2,3,4,5,6,7,8,9].forEach(function(i) {
|
||||
it('generate block ' + i, function() {
|
||||
var blockIndex = bitcoind.getBlockIndex(i);
|
||||
should.exist(blockIndex);
|
||||
should.exist(blockIndex.chainWork);
|
||||
var work = new BN(blockIndex.chainWork, 'hex');
|
||||
work.cmp(expectedWork).should.equal(0);
|
||||
expectedWork = expectedWork.add(new BN(2));
|
||||
should.exist(blockIndex.prevHash);
|
||||
blockIndex.hash.should.equal(blockHashes[i - 1]);
|
||||
blockIndex.prevHash.should.equal(blockHashes[i - 2]);
|
||||
blockIndex.height.should.equal(i);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
10
lib/node.js
10
lib/node.js
|
@ -6,6 +6,7 @@ var async = require('async');
|
|||
var bitcore = require('bitcore');
|
||||
var Networks = bitcore.Networks;
|
||||
var $ = bitcore.util.preconditions;
|
||||
var _ = bitcore.deps._;
|
||||
var index = require('./');
|
||||
var log = index.log;
|
||||
var Bus = require('./bus');
|
||||
|
@ -19,6 +20,8 @@ function Node(config) {
|
|||
|
||||
var self = this;
|
||||
|
||||
this.errors = errors; // So services can use errors without having to have bitcore-node as a dependency
|
||||
this.log = log;
|
||||
this.network = null;
|
||||
this.services = {};
|
||||
this._unloadedServices = [];
|
||||
|
@ -118,7 +121,7 @@ Node.prototype.getServiceOrder = function() {
|
|||
$.checkState(service, 'Required dependency "' + name + '" not available.');
|
||||
|
||||
// first add the dependencies
|
||||
addToStack(service.dependencies);
|
||||
addToStack(service.module.dependencies);
|
||||
|
||||
// add to the stack if it hasn't been added
|
||||
if(!stackNames[name]) {
|
||||
|
@ -137,7 +140,10 @@ Node.prototype.getServiceOrder = function() {
|
|||
Node.prototype._instantiateService = function(service) {
|
||||
var self = this;
|
||||
|
||||
var config = service.config || {};
|
||||
$.checkState(_.isObject(service.config));
|
||||
$.checkState(!service.config.node);
|
||||
|
||||
var config = service.config;
|
||||
config.node = this;
|
||||
var mod = new service.module(config);
|
||||
|
||||
|
|
|
@ -24,14 +24,8 @@ function start(options) {
|
|||
if (config.services) {
|
||||
for (var i = 0; i < config.services.length; i++) {
|
||||
var service = {};
|
||||
if(typeof config.services[i] === 'object') {
|
||||
$.checkState(config.services[i].name, 'Service name must be specified in config');
|
||||
service.name = config.services[i].name;
|
||||
service.config = config.services[i].config || {};
|
||||
} else {
|
||||
service.name = config.services[i];
|
||||
service.config = {};
|
||||
}
|
||||
service.name = config.services[i];
|
||||
service.config = config.servicesConfig && config.servicesConfig[service.name] ? config.servicesConfig[service.name] : {};
|
||||
|
||||
try {
|
||||
// first try in the built-in bitcore-node services directory
|
||||
|
@ -57,7 +51,6 @@ function start(options) {
|
|||
);
|
||||
}
|
||||
|
||||
service.dependencies = service.module.dependencies;
|
||||
services.push(service);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ var http = require('http');
|
|||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var socketio = require('socket.io');
|
||||
var BaseService = require('../service');
|
||||
var inherits = require('util').inherits;
|
||||
|
||||
var WebService = function(options) {
|
||||
var self = this;
|
||||
|
@ -17,6 +19,10 @@ var WebService = function(options) {
|
|||
});
|
||||
};
|
||||
|
||||
inherits(WebService, BaseService);
|
||||
|
||||
WebService.dependencies = [];
|
||||
|
||||
WebService.prototype.start = function(callback) {
|
||||
var self = this;
|
||||
this.app = express();
|
||||
|
@ -43,8 +49,8 @@ WebService.prototype.stop = function(callback) {
|
|||
};
|
||||
|
||||
WebService.prototype.setupAllRoutes = function() {
|
||||
for(var key in this.node.modules) {
|
||||
this.node.modules[key].setupRoutes(this.app);
|
||||
for(var key in this.node.services) {
|
||||
this.node.services[key].setupRoutes(this.app);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -183,19 +183,27 @@ describe('Bitcore Node', function() {
|
|||
node._unloadedServices = [
|
||||
{
|
||||
name: 'chain',
|
||||
dependencies: ['db']
|
||||
module: {
|
||||
dependencies: ['db']
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'db',
|
||||
module: {
|
||||
dependencies: ['daemon', 'p2p']
|
||||
}
|
||||
},
|
||||
{
|
||||
name:'daemon',
|
||||
dependencies: []
|
||||
module: {
|
||||
dependencies: []
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'p2p',
|
||||
dependencies: []
|
||||
module: {
|
||||
dependencies: []
|
||||
}
|
||||
}
|
||||
];
|
||||
var order = node.getServiceOrder();
|
||||
|
@ -219,7 +227,8 @@ describe('Bitcore Node', function() {
|
|||
};
|
||||
var service = {
|
||||
name: 'testservice',
|
||||
module: TestService
|
||||
module: TestService,
|
||||
config: {}
|
||||
};
|
||||
node._instantiateService(service);
|
||||
should.exist(node.services.testservice);
|
||||
|
@ -254,11 +263,13 @@ describe('Bitcore Node', function() {
|
|||
node.getServiceOrder = sinon.stub().returns([
|
||||
{
|
||||
name: 'test1',
|
||||
module: TestService
|
||||
module: TestService,
|
||||
config: {}
|
||||
},
|
||||
{
|
||||
name: 'test2',
|
||||
module: TestService2
|
||||
module: TestService2,
|
||||
config: {}
|
||||
}
|
||||
]);
|
||||
node.start(function() {
|
||||
|
@ -295,11 +306,13 @@ describe('Bitcore Node', function() {
|
|||
node.getServiceOrder = sinon.stub().returns([
|
||||
{
|
||||
name: 'test',
|
||||
module: TestService
|
||||
module: TestService,
|
||||
config: {}
|
||||
},
|
||||
{
|
||||
name: 'conflict',
|
||||
module: ConflictService
|
||||
module: ConflictService,
|
||||
config: {}
|
||||
}
|
||||
]);
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ describe('#start', function() {
|
|||
options.services[0].should.deep.equal({
|
||||
name: 'address',
|
||||
module: AddressService,
|
||||
dependencies: ['bitcoind', 'db'],
|
||||
config: {}
|
||||
});
|
||||
};
|
||||
|
@ -47,7 +46,6 @@ describe('#start', function() {
|
|||
options.services[0].should.deep.equal({
|
||||
name: 'address',
|
||||
module: AddressService,
|
||||
dependencies: ['bitcoind', 'db'],
|
||||
config: {
|
||||
param: 'test'
|
||||
}
|
||||
|
@ -66,13 +64,13 @@ describe('#start', function() {
|
|||
path: __dirname,
|
||||
config: {
|
||||
services: [
|
||||
{
|
||||
name: 'address',
|
||||
config: {
|
||||
param: 'test'
|
||||
}
|
||||
}
|
||||
'address'
|
||||
],
|
||||
servicesConfig: {
|
||||
'address': {
|
||||
param: 'test'
|
||||
}
|
||||
},
|
||||
datadir: './data'
|
||||
}
|
||||
});
|
||||
|
|
|
@ -37,7 +37,7 @@ describe('WebService', function() {
|
|||
it('should call setupRoutes on each module', function() {
|
||||
var node = {
|
||||
on: sinon.spy(),
|
||||
modules: {
|
||||
services: {
|
||||
one: {
|
||||
setupRoutes: sinon.spy()
|
||||
},
|
||||
|
@ -50,8 +50,8 @@ describe('WebService', function() {
|
|||
var web = new WebService({node: node});
|
||||
|
||||
web.setupAllRoutes();
|
||||
node.modules.one.setupRoutes.callCount.should.equal(1);
|
||||
node.modules.two.setupRoutes.callCount.should.equal(1);
|
||||
node.services.one.setupRoutes.callCount.should.equal(1);
|
||||
node.services.two.setupRoutes.callCount.should.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue