2015-07-16 12:53:44 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var should = require('chai').should();
|
|
|
|
var sinon = require('sinon');
|
2015-10-16 21:56:29 -07:00
|
|
|
var bitcore = require('bitcore-lib');
|
2015-07-16 12:53:44 -07:00
|
|
|
var Networks = bitcore.Networks;
|
|
|
|
var proxyquire = require('proxyquire');
|
2015-08-27 13:09:27 -07:00
|
|
|
var util = require('util');
|
2015-08-31 06:00:00 -07:00
|
|
|
var BaseService = require('../lib/service');
|
2015-07-16 12:53:44 -07:00
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
describe('Bitcore Node', function() {
|
2015-08-26 12:18:58 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
var baseConfig = {
|
|
|
|
datadir: 'testdir'
|
|
|
|
};
|
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
var Node;
|
|
|
|
|
|
|
|
before(function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
Node = proxyquire('../lib/node', {});
|
2015-08-26 12:18:58 -07:00
|
|
|
Node.prototype._loadConfiguration = sinon.spy();
|
|
|
|
Node.prototype._initialize = sinon.spy();
|
|
|
|
});
|
2015-08-31 06:00:00 -07:00
|
|
|
after(function() {
|
|
|
|
var regtest = Networks.get('regtest');
|
|
|
|
if (regtest) {
|
|
|
|
Networks.remove(regtest);
|
|
|
|
}
|
|
|
|
// restore testnet
|
|
|
|
Networks.add({
|
|
|
|
name: 'testnet',
|
|
|
|
alias: 'testnet',
|
|
|
|
pubkeyhash: 0x6f,
|
|
|
|
privatekey: 0xef,
|
|
|
|
scripthash: 0xc4,
|
|
|
|
xpubkey: 0x043587cf,
|
|
|
|
xprivkey: 0x04358394,
|
|
|
|
networkMagic: 0x0b110907,
|
|
|
|
port: 18333,
|
|
|
|
dnsSeeds: [
|
|
|
|
'testnet-seed.bitcoin.petertodd.org',
|
|
|
|
'testnet-seed.bluematt.me',
|
|
|
|
'testnet-seed.alexykot.me',
|
|
|
|
'testnet-seed.bitcoin.schildbach.de'
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
2015-08-26 12:18:58 -07:00
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
describe('@constructor', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
var TestService;
|
2015-08-31 06:00:00 -07:00
|
|
|
before(function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
TestService = function TestService() {};
|
|
|
|
util.inherits(TestService, BaseService);
|
2015-08-31 06:00:00 -07:00
|
|
|
});
|
|
|
|
it('will set properties', function() {
|
2015-08-27 13:09:27 -07:00
|
|
|
var config = {
|
2015-08-31 06:00:00 -07:00
|
|
|
datadir: 'testdir',
|
2015-08-31 06:00:00 -07:00
|
|
|
services: [
|
2015-08-27 13:09:27 -07:00
|
|
|
{
|
|
|
|
name: 'test1',
|
2015-08-31 06:00:00 -07:00
|
|
|
module: TestService
|
2015-08-27 13:09:27 -07:00
|
|
|
}
|
|
|
|
],
|
|
|
|
};
|
|
|
|
var TestNode = proxyquire('../lib/node', {});
|
2015-08-31 06:00:00 -07:00
|
|
|
TestNode.prototype.start = sinon.spy();
|
2015-08-27 13:09:27 -07:00
|
|
|
var node = new TestNode(config);
|
2015-08-31 06:00:00 -07:00
|
|
|
node._unloadedServices.length.should.equal(1);
|
|
|
|
node._unloadedServices[0].name.should.equal('test1');
|
|
|
|
node._unloadedServices[0].module.should.equal(TestService);
|
2015-08-31 06:00:00 -07:00
|
|
|
node.network.should.equal(Networks.defaultNetwork);
|
|
|
|
});
|
|
|
|
it('will set network to testnet', function() {
|
|
|
|
var config = {
|
|
|
|
network: 'testnet',
|
|
|
|
datadir: 'testdir',
|
2015-08-31 06:00:00 -07:00
|
|
|
services: [
|
2015-08-31 06:00:00 -07:00
|
|
|
{
|
|
|
|
name: 'test1',
|
2015-08-31 06:00:00 -07:00
|
|
|
module: TestService
|
2015-08-31 06:00:00 -07:00
|
|
|
}
|
|
|
|
],
|
|
|
|
};
|
|
|
|
var TestNode = proxyquire('../lib/node', {});
|
|
|
|
TestNode.prototype.start = sinon.spy();
|
|
|
|
var node = new TestNode(config);
|
|
|
|
node.network.should.equal(Networks.testnet);
|
|
|
|
});
|
|
|
|
it('will set network to regtest', function() {
|
|
|
|
var config = {
|
|
|
|
network: 'regtest',
|
|
|
|
datadir: 'testdir',
|
2015-08-31 06:00:00 -07:00
|
|
|
services: [
|
2015-08-31 06:00:00 -07:00
|
|
|
{
|
|
|
|
name: 'test1',
|
2015-08-31 06:00:00 -07:00
|
|
|
module: TestService
|
2015-08-31 06:00:00 -07:00
|
|
|
}
|
|
|
|
],
|
|
|
|
};
|
|
|
|
var TestNode = proxyquire('../lib/node', {});
|
|
|
|
TestNode.prototype.start = sinon.spy();
|
|
|
|
var node = new TestNode(config);
|
|
|
|
var regtest = Networks.get('regtest');
|
|
|
|
should.exist(regtest);
|
|
|
|
node.network.should.equal(regtest);
|
|
|
|
});
|
2015-08-27 13:09:27 -07:00
|
|
|
});
|
|
|
|
|
2015-07-29 14:22:34 -07:00
|
|
|
describe('#openBus', function() {
|
|
|
|
it('will create a new bus', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
var node = new Node(baseConfig);
|
2015-07-29 14:22:34 -07:00
|
|
|
var bus = node.openBus();
|
2015-08-28 13:16:51 -07:00
|
|
|
bus.node.should.equal(node);
|
2015-07-29 14:22:34 -07:00
|
|
|
});
|
|
|
|
});
|
2015-08-27 13:09:27 -07:00
|
|
|
|
2015-08-04 13:34:53 -07:00
|
|
|
describe('#getAllAPIMethods', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
it('should return db methods and service methods', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
var node = new Node(baseConfig);
|
2015-08-31 06:00:00 -07:00
|
|
|
node.services = {
|
2015-08-31 06:00:00 -07:00
|
|
|
db: {
|
|
|
|
getAPIMethods: sinon.stub().returns(['db1', 'db2']),
|
|
|
|
},
|
2015-08-31 06:00:00 -07:00
|
|
|
service1: {
|
2015-08-27 13:09:27 -07:00
|
|
|
getAPIMethods: sinon.stub().returns(['mda1', 'mda2'])
|
|
|
|
},
|
2015-08-31 06:00:00 -07:00
|
|
|
service2: {
|
2015-08-27 13:09:27 -07:00
|
|
|
getAPIMethods: sinon.stub().returns(['mdb1', 'mdb2'])
|
|
|
|
}
|
2015-08-28 08:43:38 -07:00
|
|
|
};
|
2015-08-04 13:34:53 -07:00
|
|
|
|
|
|
|
var methods = node.getAllAPIMethods();
|
|
|
|
methods.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
|
|
|
|
});
|
|
|
|
});
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-08-04 13:34:53 -07:00
|
|
|
describe('#getAllPublishEvents', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
it('should return services publish events', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
var node = new Node(baseConfig);
|
2015-08-31 06:00:00 -07:00
|
|
|
node.services = {
|
2015-08-31 06:00:00 -07:00
|
|
|
db: {
|
|
|
|
getPublishEvents: sinon.stub().returns(['db1', 'db2']),
|
|
|
|
},
|
2015-08-31 06:00:00 -07:00
|
|
|
service1: {
|
2015-08-27 13:09:27 -07:00
|
|
|
getPublishEvents: sinon.stub().returns(['mda1', 'mda2'])
|
|
|
|
},
|
2015-08-31 06:00:00 -07:00
|
|
|
service2: {
|
2015-08-27 13:09:27 -07:00
|
|
|
getPublishEvents: sinon.stub().returns(['mdb1', 'mdb2'])
|
|
|
|
}
|
2015-08-28 08:43:38 -07:00
|
|
|
};
|
2015-08-04 13:34:53 -07:00
|
|
|
var events = node.getAllPublishEvents();
|
2015-08-06 13:19:36 -07:00
|
|
|
events.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
|
2015-08-04 13:34:53 -07:00
|
|
|
});
|
|
|
|
});
|
2015-08-27 07:10:07 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
describe('#getServiceOrder', function() {
|
|
|
|
it('should return the services in the correct order', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
var node = new Node(baseConfig);
|
2015-08-31 06:00:00 -07:00
|
|
|
node._unloadedServices = [
|
2015-08-31 06:00:00 -07:00
|
|
|
{
|
|
|
|
name: 'chain',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: {
|
|
|
|
dependencies: ['db']
|
|
|
|
}
|
2015-07-24 10:45:31 -07:00
|
|
|
},
|
2015-08-31 06:00:00 -07:00
|
|
|
{
|
|
|
|
name: 'db',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: {
|
2015-08-31 06:00:00 -07:00
|
|
|
dependencies: ['daemon', 'p2p']
|
2015-08-31 10:38:21 -07:00
|
|
|
}
|
2015-07-16 12:53:44 -07:00
|
|
|
},
|
2015-08-31 06:00:00 -07:00
|
|
|
{
|
|
|
|
name:'daemon',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: {
|
|
|
|
dependencies: []
|
|
|
|
}
|
2015-08-21 13:13:11 -07:00
|
|
|
},
|
2015-08-31 06:00:00 -07:00
|
|
|
{
|
|
|
|
name: 'p2p',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: {
|
|
|
|
dependencies: []
|
|
|
|
}
|
2015-08-21 13:13:11 -07:00
|
|
|
}
|
2015-08-31 06:00:00 -07:00
|
|
|
];
|
|
|
|
var order = node.getServiceOrder();
|
|
|
|
order[0].name.should.equal('daemon');
|
|
|
|
order[1].name.should.equal('p2p');
|
|
|
|
order[2].name.should.equal('db');
|
|
|
|
order[3].name.should.equal('chain');
|
2015-08-21 13:13:11 -07:00
|
|
|
});
|
2015-07-16 12:53:44 -07:00
|
|
|
});
|
2015-08-21 13:13:11 -07:00
|
|
|
|
2015-09-04 12:05:32 -07:00
|
|
|
describe('#_startService', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
it('will instantiate an instance and load api methods', function() {
|
|
|
|
var node = new Node(baseConfig);
|
2015-08-31 06:00:00 -07:00
|
|
|
function TestService() {}
|
|
|
|
util.inherits(TestService, BaseService);
|
2015-09-04 12:05:32 -07:00
|
|
|
TestService.prototype.start = sinon.stub().callsArg(0);
|
2015-08-31 06:00:00 -07:00
|
|
|
TestService.prototype.getData = function() {};
|
|
|
|
TestService.prototype.getAPIMethods = function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
return [
|
|
|
|
['getData', this, this.getData, 1]
|
|
|
|
];
|
2015-07-16 12:53:44 -07:00
|
|
|
};
|
2015-08-31 06:00:00 -07:00
|
|
|
var service = {
|
2015-08-31 06:00:00 -07:00
|
|
|
name: 'testservice',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: TestService,
|
|
|
|
config: {}
|
2015-08-31 06:00:00 -07:00
|
|
|
};
|
2015-09-04 12:05:32 -07:00
|
|
|
node._startService(service, function(err) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
TestService.prototype.start.callCount.should.equal(1);
|
|
|
|
should.exist(node.services.testservice);
|
|
|
|
should.exist(node.getData);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('will give an error from start', function() {
|
|
|
|
var node = new Node(baseConfig);
|
|
|
|
function TestService() {}
|
|
|
|
util.inherits(TestService, BaseService);
|
|
|
|
TestService.prototype.start = sinon.stub().callsArgWith(0, new Error('test'));
|
|
|
|
var service = {
|
|
|
|
name: 'testservice',
|
|
|
|
module: TestService,
|
|
|
|
config: {}
|
|
|
|
};
|
|
|
|
node._startService(service, function(err) {
|
|
|
|
err.message.should.equal('test');
|
|
|
|
});
|
2015-07-16 12:53:44 -07:00
|
|
|
});
|
|
|
|
});
|
2015-08-26 12:18:58 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
describe('#start', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
it('will call start for each service', function(done) {
|
2015-08-31 06:00:00 -07:00
|
|
|
var node = new Node(baseConfig);
|
2015-08-26 12:18:58 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
function TestService() {}
|
|
|
|
util.inherits(TestService, BaseService);
|
|
|
|
TestService.prototype.start = sinon.stub().callsArg(0);
|
|
|
|
TestService.prototype.getData = function() {};
|
|
|
|
TestService.prototype.getAPIMethods = function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
return [
|
|
|
|
['getData', this, this.getData, 1]
|
|
|
|
];
|
2015-08-24 11:46:48 -07:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
function TestService2() {}
|
|
|
|
util.inherits(TestService2, BaseService);
|
|
|
|
TestService2.prototype.start = sinon.stub().callsArg(0);
|
|
|
|
TestService2.prototype.getData2 = function() {};
|
|
|
|
TestService2.prototype.getAPIMethods = function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
return [
|
|
|
|
['getData2', this, this.getData2, 1]
|
|
|
|
];
|
|
|
|
};
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
node.getServiceOrder = sinon.stub().returns([
|
|
|
|
{
|
|
|
|
name: 'test1',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: TestService,
|
|
|
|
config: {}
|
2015-08-31 06:00:00 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'test2',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: TestService2,
|
|
|
|
config: {}
|
2015-08-31 06:00:00 -07:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
node.start(function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
TestService2.prototype.start.callCount.should.equal(1);
|
|
|
|
TestService.prototype.start.callCount.should.equal(1);
|
2015-08-31 06:00:00 -07:00
|
|
|
should.exist(node.getData2);
|
|
|
|
should.exist(node.getData);
|
2015-08-31 06:00:00 -07:00
|
|
|
done();
|
2015-07-16 12:53:44 -07:00
|
|
|
});
|
2015-08-21 13:13:11 -07:00
|
|
|
});
|
2015-08-31 06:00:00 -07:00
|
|
|
it('will error if there are conflicting API methods', function(done) {
|
2015-08-31 06:00:00 -07:00
|
|
|
var node = new Node(baseConfig);
|
2015-08-20 14:50:14 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
function TestService() {}
|
|
|
|
util.inherits(TestService, BaseService);
|
|
|
|
TestService.prototype.start = sinon.stub().callsArg(0);
|
|
|
|
TestService.prototype.getData = function() {};
|
|
|
|
TestService.prototype.getAPIMethods = function() {
|
2015-08-27 13:09:27 -07:00
|
|
|
return [
|
|
|
|
['getData', this, this.getData, 1]
|
|
|
|
];
|
|
|
|
};
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
function ConflictService() {}
|
|
|
|
util.inherits(ConflictService, BaseService);
|
|
|
|
ConflictService.prototype.start = sinon.stub().callsArg(0);
|
|
|
|
ConflictService.prototype.getData = function() {};
|
|
|
|
ConflictService.prototype.getAPIMethods = function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
return [
|
|
|
|
['getData', this, this.getData, 1]
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
node.getServiceOrder = sinon.stub().returns([
|
|
|
|
{
|
2015-08-31 06:00:00 -07:00
|
|
|
name: 'test',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: TestService,
|
|
|
|
config: {}
|
2015-08-27 13:09:27 -07:00
|
|
|
},
|
|
|
|
{
|
2015-08-31 06:00:00 -07:00
|
|
|
name: 'conflict',
|
2015-08-31 10:38:21 -07:00
|
|
|
module: ConflictService,
|
|
|
|
config: {}
|
2015-08-27 13:09:27 -07:00
|
|
|
}
|
|
|
|
]);
|
2015-08-31 06:00:00 -07:00
|
|
|
|
|
|
|
node.start(function(err) {
|
|
|
|
should.exist(err);
|
2015-09-04 12:05:32 -07:00
|
|
|
err.message.should.match(/^Existing API method\(s\) exists\:/);
|
2015-08-27 13:09:27 -07:00
|
|
|
done();
|
|
|
|
});
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#stop', function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
it('will call stop for each service', function(done) {
|
2015-08-31 06:00:00 -07:00
|
|
|
var node = new Node(baseConfig);
|
2015-08-31 06:00:00 -07:00
|
|
|
function TestService() {}
|
|
|
|
util.inherits(TestService, BaseService);
|
|
|
|
TestService.prototype.stop = sinon.stub().callsArg(0);
|
|
|
|
TestService.prototype.getData = function() {};
|
|
|
|
TestService.prototype.getAPIMethods = function() {
|
2015-08-27 13:09:27 -07:00
|
|
|
return [
|
|
|
|
['getData', this, this.getData, 1]
|
|
|
|
];
|
|
|
|
};
|
2015-08-31 06:00:00 -07:00
|
|
|
node.services = {
|
|
|
|
'test1': new TestService({node: node})
|
2015-08-27 13:09:27 -07:00
|
|
|
};
|
|
|
|
node.test2 = {};
|
|
|
|
node.test2.stop = sinon.stub().callsArg(0);
|
|
|
|
node.getServiceOrder = sinon.stub().returns([
|
|
|
|
{
|
|
|
|
name: 'test1',
|
2015-08-31 06:00:00 -07:00
|
|
|
module: TestService
|
2015-08-27 13:09:27 -07:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
node.stop(function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
TestService.prototype.stop.callCount.should.equal(1);
|
2015-08-27 13:09:27 -07:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-07-16 12:53:44 -07:00
|
|
|
});
|