Rename modules to services.
This commit is contained in:
parent
16eef1279c
commit
7551f487f8
|
@ -10,7 +10,7 @@ process.title = 'libbitcoind';
|
|||
/**
|
||||
* daemon
|
||||
*/
|
||||
var daemon = require('../').modules.BitcoinModule({
|
||||
var daemon = require('../').services.Bitcoin({
|
||||
node: {
|
||||
datadir: process.env.BITCORENODE_DIR || process.env.HOME + '/.bitcoin',
|
||||
network: {
|
||||
|
|
10
index.js
10
index.js
|
@ -3,13 +3,13 @@
|
|||
module.exports = require('./lib');
|
||||
module.exports.Node = require('./lib/node');
|
||||
module.exports.Transaction = require('./lib/transaction');
|
||||
module.exports.Module = require('./lib/module');
|
||||
module.exports.Service = require('./lib/service');
|
||||
module.exports.errors = require('./lib/errors');
|
||||
|
||||
module.exports.modules = {};
|
||||
module.exports.modules.AddressModule = require('./lib/modules/address');
|
||||
module.exports.modules.BitcoinModule = require('./lib/modules/bitcoind');
|
||||
module.exports.modules.DBModule = require('./lib/modules/db');
|
||||
module.exports.services = {};
|
||||
module.exports.services.Address = require('./lib/services/address');
|
||||
module.exports.services.Bitcoin = require('./lib/services/bitcoind');
|
||||
module.exports.services.DB = require('./lib/services/db');
|
||||
|
||||
module.exports.scaffold = {};
|
||||
module.exports.scaffold.create = require('./lib/scaffold/create');
|
||||
|
|
|
@ -25,9 +25,9 @@ var should = chai.should();
|
|||
var BitcoinRPC = require('bitcoind-rpc');
|
||||
var index = require('..');
|
||||
var BitcoreNode = index.Node;
|
||||
var AddressModule = index.modules.AddressModule;
|
||||
var BitcoinModule = index.modules.BitcoinModule;
|
||||
var DBModule = index.modules.DBModule;
|
||||
var AddressService = index.services.Address;
|
||||
var BitcoinService = index.services.Bitcoin;
|
||||
var DBService = index.services.DB;
|
||||
var testWIF = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG';
|
||||
var testKey;
|
||||
var client;
|
||||
|
@ -65,21 +65,21 @@ describe('Node Functionality', function() {
|
|||
var configuration = {
|
||||
datadir: datadir,
|
||||
network: 'regtest',
|
||||
modules: [
|
||||
services: [
|
||||
{
|
||||
name: 'db',
|
||||
module: DBModule,
|
||||
dependencies: DBModule.dependencies
|
||||
module: DBService,
|
||||
dependencies: DBService.dependencies
|
||||
},
|
||||
{
|
||||
name: 'bitcoind',
|
||||
module: BitcoinModule,
|
||||
dependencies: BitcoinModule.dependencies
|
||||
module: BitcoinService,
|
||||
dependencies: BitcoinService.dependencies
|
||||
},
|
||||
{
|
||||
name: 'address',
|
||||
module: AddressModule,
|
||||
dependencies: AddressModule.dependencies
|
||||
module: AddressService,
|
||||
dependencies: AddressService.dependencies
|
||||
}
|
||||
]
|
||||
};
|
||||
|
@ -102,7 +102,7 @@ describe('Node Functionality', function() {
|
|||
});
|
||||
|
||||
var syncedHandler = function() {
|
||||
if (node.modules.db.tip.__height === 150) {
|
||||
if (node.services.db.tip.__height === 150) {
|
||||
node.removeListener('synced', syncedHandler);
|
||||
done();
|
||||
}
|
||||
|
@ -178,18 +178,18 @@ describe('Node Functionality', function() {
|
|||
blocksRemoved++;
|
||||
};
|
||||
|
||||
node.modules.db.on('removeblock', removeBlock);
|
||||
node.services.db.on('removeblock', removeBlock);
|
||||
|
||||
var addBlock = function() {
|
||||
blocksAdded++;
|
||||
if (blocksAdded === 2 && blocksRemoved === 1) {
|
||||
node.modules.db.removeListener('addblock', addBlock);
|
||||
node.modules.db.removeListener('removeblock', removeBlock);
|
||||
node.services.db.removeListener('addblock', addBlock);
|
||||
node.services.db.removeListener('removeblock', removeBlock);
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
node.modules.db.on('addblock', addBlock);
|
||||
node.services.db.on('addblock', addBlock);
|
||||
|
||||
// We need to add a transaction to the mempool so that the next block will
|
||||
// have a different hash as the hash has been invalidated.
|
||||
|
|
|
@ -61,7 +61,7 @@ describe('Daemon Binding Functionality', function() {
|
|||
throw err;
|
||||
}
|
||||
|
||||
bitcoind = require('../').modules.BitcoinModule({
|
||||
bitcoind = require('../').services.Bitcoin({
|
||||
node: {
|
||||
datadir: datadir,
|
||||
network: {
|
||||
|
|
24
lib/bus.js
24
lib/bus.js
|
@ -11,11 +11,11 @@ function Bus(params) {
|
|||
util.inherits(Bus, events.EventEmitter);
|
||||
|
||||
Bus.prototype.subscribe = function(name) {
|
||||
var events = this.node.db.getPublishEvents();
|
||||
var events = [];
|
||||
|
||||
for(var i in this.node.modules) {
|
||||
var mod = this.node.modules[i];
|
||||
events = events.concat(mod.getPublishEvents());
|
||||
for(var i in this.node.services) {
|
||||
var service = this.node.services[i];
|
||||
events = events.concat(service.getPublishEvents());
|
||||
}
|
||||
|
||||
for (var j = 0; j < events.length; j++) {
|
||||
|
@ -29,11 +29,11 @@ Bus.prototype.subscribe = function(name) {
|
|||
};
|
||||
|
||||
Bus.prototype.unsubscribe = function(name) {
|
||||
var events = this.node.db.getPublishEvents();
|
||||
var events = [];
|
||||
|
||||
for(var i in this.node.modules) {
|
||||
var mod = this.node.modules[i];
|
||||
events = events.concat(mod.getPublishEvents());
|
||||
for(var i in this.node.services) {
|
||||
var service = this.node.services[i];
|
||||
events = events.concat(service.getPublishEvents());
|
||||
}
|
||||
|
||||
for (var j = 0; j < events.length; j++) {
|
||||
|
@ -47,11 +47,11 @@ Bus.prototype.unsubscribe = function(name) {
|
|||
};
|
||||
|
||||
Bus.prototype.close = function() {
|
||||
var events = this.node.db.getPublishEvents();
|
||||
var events = [];
|
||||
|
||||
for(var i in this.node.modules) {
|
||||
var mod = this.node.modules[i];
|
||||
events = events.concat(mod.getPublishEvents());
|
||||
for(var i in this.node.services) {
|
||||
var service = this.node.services[i];
|
||||
events = events.concat(service.getPublishEvents());
|
||||
}
|
||||
|
||||
// Unsubscribe from all events
|
||||
|
|
42
lib/node.js
42
lib/node.js
|
@ -9,7 +9,7 @@ var $ = bitcore.util.preconditions;
|
|||
var index = require('./');
|
||||
var log = index.log;
|
||||
var Bus = require('./bus');
|
||||
var BaseModule = require('./module');
|
||||
var BaseService = require('./service');
|
||||
|
||||
function Node(config) {
|
||||
if(!(this instanceof Node)) {
|
||||
|
@ -19,13 +19,13 @@ function Node(config) {
|
|||
var self = this;
|
||||
|
||||
this.network = null;
|
||||
this.modules = {};
|
||||
this._unloadedModules = [];
|
||||
this.services = {};
|
||||
this._unloadedServices = [];
|
||||
|
||||
// TODO type check the arguments of config.modules
|
||||
if (config.modules) {
|
||||
$.checkArgument(Array.isArray(config.modules));
|
||||
this._unloadedModules = config.modules;
|
||||
// TODO type check the arguments of config.services
|
||||
if (config.services) {
|
||||
$.checkArgument(Array.isArray(config.services));
|
||||
this._unloadedServices = config.services;
|
||||
}
|
||||
|
||||
$.checkState(config.datadir, 'Node config expects "datadir"');
|
||||
|
@ -71,13 +71,13 @@ Node.prototype._setNetwork = function(config) {
|
|||
};
|
||||
|
||||
Node.prototype.openBus = function() {
|
||||
return new Bus({db: this.modules.db});
|
||||
return new Bus({node: this});
|
||||
};
|
||||
|
||||
Node.prototype.getAllAPIMethods = function() {
|
||||
var methods = [];
|
||||
for(var i in this.modules) {
|
||||
var mod = this.modules[i];
|
||||
for(var i in this.services) {
|
||||
var mod = this.services[i];
|
||||
methods = methods.concat(mod.getAPIMethods());
|
||||
}
|
||||
return methods;
|
||||
|
@ -85,8 +85,8 @@ Node.prototype.getAllAPIMethods = function() {
|
|||
|
||||
Node.prototype.getAllPublishEvents = function() {
|
||||
var events = [];
|
||||
for (var i in this.modules) {
|
||||
var mod = this.modules[i];
|
||||
for (var i in this.services) {
|
||||
var mod = this.services[i];
|
||||
events = events.concat(mod.getPublishEvents());
|
||||
}
|
||||
return events;
|
||||
|
@ -94,7 +94,7 @@ Node.prototype.getAllPublishEvents = function() {
|
|||
|
||||
Node.prototype.getServiceOrder = function() {
|
||||
|
||||
var services = this._unloadedModules;
|
||||
var services = this._unloadedServices;
|
||||
|
||||
// organize data for sorting
|
||||
var names = [];
|
||||
|
@ -132,19 +132,19 @@ Node.prototype.getServiceOrder = function() {
|
|||
return stack;
|
||||
};
|
||||
|
||||
Node.prototype._instantiateModule = function(service) {
|
||||
Node.prototype._instantiateService = function(service) {
|
||||
var self = this;
|
||||
var mod = new service.module({
|
||||
node: this
|
||||
});
|
||||
|
||||
$.checkState(
|
||||
mod instanceof BaseModule,
|
||||
'Unexpected module instance type for module:' + service.name
|
||||
mod instanceof BaseService,
|
||||
'Unexpected module instance type for service:' + service.name
|
||||
);
|
||||
|
||||
// include in loaded modules
|
||||
this.modules[service.name] = mod;
|
||||
// include in loaded services
|
||||
this.services[service.name] = mod;
|
||||
|
||||
// add API methods
|
||||
var methodData = mod.getAPIMethods();
|
||||
|
@ -172,11 +172,11 @@ Node.prototype.start = function(callback) {
|
|||
function(service, next) {
|
||||
log.info('Starting ' + service.name);
|
||||
try {
|
||||
self._instantiateModule(service);
|
||||
self._instantiateService(service);
|
||||
} catch(err) {
|
||||
return callback(err);
|
||||
}
|
||||
self.modules[service.name].start(next);
|
||||
self.services[service.name].start(next);
|
||||
},
|
||||
callback
|
||||
);
|
||||
|
@ -194,7 +194,7 @@ Node.prototype.stop = function(callback) {
|
|||
services,
|
||||
function(service, next) {
|
||||
log.info('Stopping ' + service.name);
|
||||
self.modules[service.name].stop(next);
|
||||
self.services[service.name].stop(next);
|
||||
},
|
||||
callback
|
||||
);
|
||||
|
|
|
@ -10,10 +10,10 @@ var _ = bitcore.deps._;
|
|||
|
||||
/**
|
||||
* @param {String} configFilePath - The absolute path to the configuration file
|
||||
* @param {String} module - The name of the module
|
||||
* @param {String} service - The name of the service
|
||||
* @param {Function} done
|
||||
*/
|
||||
function addConfig(configFilePath, module, done) {
|
||||
function addConfig(configFilePath, service, done) {
|
||||
$.checkState(path.isAbsolute(configFilePath), 'An absolute path is expected');
|
||||
fs.readFile(configFilePath, function(err, data) {
|
||||
if (err) {
|
||||
|
@ -21,12 +21,12 @@ function addConfig(configFilePath, module, done) {
|
|||
}
|
||||
var config = JSON.parse(data);
|
||||
$.checkState(
|
||||
Array.isArray(config.modules),
|
||||
'Configuration file is expected to have a modules array.'
|
||||
Array.isArray(config.services),
|
||||
'Configuration file is expected to have a services array.'
|
||||
);
|
||||
config.modules.push(module);
|
||||
config.modules = _.unique(config.modules);
|
||||
config.modules.sort(function(a, b) {
|
||||
config.services.push(service);
|
||||
config.services = _.unique(config.services);
|
||||
config.services.sort(function(a, b) {
|
||||
return a > b;
|
||||
});
|
||||
fs.writeFile(configFilePath, JSON.stringify(config, null, 2), done);
|
||||
|
@ -35,12 +35,12 @@ function addConfig(configFilePath, module, done) {
|
|||
|
||||
/**
|
||||
* @param {String} configDir - The absolute configuration directory path
|
||||
* @param {String} module - The name of the module
|
||||
* @param {String} service - The name of the service
|
||||
* @param {Function} done
|
||||
*/
|
||||
function addModule(configDir, module, done) {
|
||||
function addService(configDir, service, done) {
|
||||
$.checkState(path.isAbsolute(configDir), 'An absolute path is expected');
|
||||
var npm = spawn('npm', ['install', module, '--save'], {cwd: configDir});
|
||||
var npm = spawn('npm', ['install', service, '--save'], {cwd: configDir});
|
||||
|
||||
npm.stdout.on('data', function(data) {
|
||||
process.stdout.write(data);
|
||||
|
@ -52,7 +52,7 @@ function addModule(configDir, module, done) {
|
|||
|
||||
npm.on('close', function(code) {
|
||||
if (code !== 0) {
|
||||
return done(new Error('There was an error installing module: ' + module));
|
||||
return done(new Error('There was an error installing service: ' + service));
|
||||
} else {
|
||||
return done();
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ function addModule(configDir, module, done) {
|
|||
/**
|
||||
* @param {String} options.cwd - The current working directory
|
||||
* @param {String} options.dirname - The bitcore-node configuration directory
|
||||
* @param {Array} options.modules - An array of strings of module names
|
||||
* @param {Array} options.services - An array of strings of service names
|
||||
* @param {Function} done - A callback function called when finished
|
||||
*/
|
||||
function add(options, done) {
|
||||
|
@ -72,10 +72,10 @@ function add(options, done) {
|
|||
_.isString(options.path) && path.isAbsolute(options.path),
|
||||
'An absolute path is expected'
|
||||
);
|
||||
$.checkArgument(Array.isArray(options.modules));
|
||||
$.checkArgument(Array.isArray(options.services));
|
||||
|
||||
var configPath = options.path;
|
||||
var modules = options.modules;
|
||||
var services = options.services;
|
||||
|
||||
var bitcoreConfigPath = path.resolve(configPath, 'bitcore-node.json');
|
||||
var packagePath = path.resolve(configPath, 'package.json');
|
||||
|
@ -87,15 +87,15 @@ function add(options, done) {
|
|||
}
|
||||
|
||||
async.eachSeries(
|
||||
modules,
|
||||
function(module, next) {
|
||||
// npm install <module_name> --save
|
||||
addModule(configPath, module, function(err) {
|
||||
services,
|
||||
function(service, next) {
|
||||
// npm install <service_name> --save
|
||||
addService(configPath, service, function(err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
// add module to bitcore-node.json
|
||||
addConfig(bitcoreConfigPath, module, next);
|
||||
// add service to bitcore-node.json
|
||||
addConfig(bitcoreConfigPath, service, next);
|
||||
});
|
||||
}, done
|
||||
);
|
||||
|
|
|
@ -12,7 +12,7 @@ var fs = require('fs');
|
|||
|
||||
var BASE_CONFIG = {
|
||||
name: 'My Node',
|
||||
modules: [
|
||||
services: [
|
||||
'address'
|
||||
],
|
||||
datadir: './data',
|
||||
|
@ -61,7 +61,7 @@ function createBitcoinDirectory(datadir, done) {
|
|||
* @param {String} configDir - The absolute path
|
||||
* @param {String} name - The name of the node
|
||||
* @param {String} datadir - The bitcoin database directory
|
||||
* @param {Boolean} isGlobal - If the configuration depends on globally installed node modules.
|
||||
* @param {Boolean} isGlobal - If the configuration depends on globally installed node services.
|
||||
* @param {Function} done - The callback function called when finished
|
||||
*/
|
||||
function createConfigDirectory(configDir, name, datadir, isGlobal, done) {
|
||||
|
|
|
@ -13,7 +13,7 @@ function getDefaultConfig() {
|
|||
datadir: process.env.BITCORENODE_DIR || path.resolve(process.env.HOME, '.bitcoin'),
|
||||
network: process.env.BITCORENODE_NETWORK || 'livenet',
|
||||
port: process.env.BITCORENODE_PORT || 3001,
|
||||
modules: ['bitcoind', 'db', 'address']
|
||||
services: ['bitcoind', 'db', 'address']
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -15,42 +15,42 @@ var interval = false;
|
|||
function start(options) {
|
||||
/* jshint maxstatements: 100 */
|
||||
|
||||
var bitcoreModules = [];
|
||||
var services = [];
|
||||
|
||||
var configPath = options.path;
|
||||
var config = options.config;
|
||||
|
||||
if (config.modules) {
|
||||
for (var i = 0; i < config.modules.length; i++) {
|
||||
var moduleName = config.modules[i];
|
||||
var bitcoreModule;
|
||||
if (config.services) {
|
||||
for (var i = 0; i < config.services.length; i++) {
|
||||
var serviceName = config.services[i];
|
||||
var service;
|
||||
try {
|
||||
// first try in the built-in bitcore-node modules directory
|
||||
bitcoreModule = require(path.resolve(__dirname, '../modules/' + moduleName));
|
||||
// first try in the built-in bitcore-node services directory
|
||||
service = require(path.resolve(__dirname, '../services/' + serviceName));
|
||||
} catch(e) {
|
||||
|
||||
// check if the package.json specifies a specific file to use
|
||||
var modulePackage = require(moduleName + '/package.json');
|
||||
var bitcoreNodeModule = moduleName;
|
||||
if (modulePackage.bitcoreNode) {
|
||||
bitcoreNodeModule = moduleName + '/' + modulePackage.bitcoreNode;
|
||||
var servicePackage = require(serviceName + '/package.json');
|
||||
var serviceModule = serviceName;
|
||||
if (servicePackage.bitcoreNode) {
|
||||
serviceModule = serviceName + '/' + servicePackage.bitcoreNode;
|
||||
}
|
||||
bitcoreModule = require(bitcoreNodeModule);
|
||||
service = require(serviceModule);
|
||||
}
|
||||
|
||||
// check that the module supports expected methods
|
||||
if (!bitcoreModule.prototype ||
|
||||
!bitcoreModule.dependencies ||
|
||||
!bitcoreModule.prototype.start ||
|
||||
!bitcoreModule.prototype.stop) {
|
||||
// check that the service supports expected methods
|
||||
if (!service.prototype ||
|
||||
!service.dependencies ||
|
||||
!service.prototype.start ||
|
||||
!service.prototype.stop) {
|
||||
throw new Error(
|
||||
'Could not load module "' + moduleName + '" as it does not support necessary methods.'
|
||||
'Could not load service "' + serviceName + '" as it does not support necessary methods.'
|
||||
);
|
||||
}
|
||||
bitcoreModules.push({
|
||||
name: moduleName,
|
||||
module: bitcoreModule,
|
||||
dependencies: bitcoreModule.dependencies
|
||||
services.push({
|
||||
name: serviceName,
|
||||
module: service,
|
||||
dependencies: service.dependencies
|
||||
});
|
||||
|
||||
}
|
||||
|
@ -61,15 +61,15 @@ function start(options) {
|
|||
// expand to the full path
|
||||
fullConfig.datadir = path.resolve(configPath, config.datadir);
|
||||
|
||||
// load the modules
|
||||
fullConfig.modules = bitcoreModules;
|
||||
// load the services
|
||||
fullConfig.services = services;
|
||||
|
||||
var node = new BitcoreNode(fullConfig);
|
||||
|
||||
function logSyncStatus() {
|
||||
log.info(
|
||||
'Sync Status: Tip:', node.modules.db.tip.hash,
|
||||
'Height:', node.modules.db.tip.__height,
|
||||
'Sync Status: Tip:', node.services.db.tip.hash,
|
||||
'Height:', node.services.db.tip.__height,
|
||||
'Rate:', count/10, 'blocks per second'
|
||||
);
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ function start(options) {
|
|||
});
|
||||
|
||||
node.on('ready', function() {
|
||||
node.modules.db.on('addblock', function(block) {
|
||||
node.services.db.on('addblock', function(block) {
|
||||
count++;
|
||||
// Initialize logging if not already instantiated
|
||||
if (!interval) {
|
||||
|
|
|
@ -3,18 +3,18 @@
|
|||
var util = require('util');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var Module = function(options) {
|
||||
var Service = function(options) {
|
||||
EventEmitter.call(this);
|
||||
|
||||
this.node = options.node;
|
||||
};
|
||||
|
||||
util.inherits(Module, EventEmitter);
|
||||
util.inherits(Service, EventEmitter);
|
||||
|
||||
/**
|
||||
* Describes the dependencies that should be loaded before this module.
|
||||
* Describes the dependencies that should be loaded before this service.
|
||||
*/
|
||||
Module.dependencies = [];
|
||||
Service.dependencies = [];
|
||||
|
||||
/**
|
||||
* blockHandler
|
||||
|
@ -22,7 +22,7 @@ Module.dependencies = [];
|
|||
* @param {Boolean} add - whether the block is being added or removed
|
||||
* @param {Function} callback - call with the leveldb database operations to perform
|
||||
*/
|
||||
Module.prototype.blockHandler = function(block, add, callback) {
|
||||
Service.prototype.blockHandler = function(block, add, callback) {
|
||||
// implement in the child class
|
||||
setImmediate(callback);
|
||||
};
|
||||
|
@ -31,7 +31,7 @@ Module.prototype.blockHandler = function(block, add, callback) {
|
|||
* the bus events available for subscription
|
||||
* @return {Array} an array of event info
|
||||
*/
|
||||
Module.prototype.getPublishEvents = function() {
|
||||
Service.prototype.getPublishEvents = function() {
|
||||
// Example:
|
||||
// return [
|
||||
// ['eventname', this, this.subscribeEvent, this.unsubscribeEvent],
|
||||
|
@ -43,7 +43,7 @@ Module.prototype.getPublishEvents = function() {
|
|||
* the API methods to expose
|
||||
* @return {Array} return array of methods
|
||||
*/
|
||||
Module.prototype.getAPIMethods = function() {
|
||||
Service.prototype.getAPIMethods = function() {
|
||||
// Example:
|
||||
// return [
|
||||
// ['getData', this, this.getData, 1]
|
||||
|
@ -53,16 +53,16 @@ Module.prototype.getAPIMethods = function() {
|
|||
};
|
||||
|
||||
// Example:
|
||||
// Module.prototype.getData = function(arg1, callback) {
|
||||
// Service.prototype.getData = function(arg1, callback) {
|
||||
//
|
||||
// };
|
||||
|
||||
Module.prototype.start = function(done) {
|
||||
Service.prototype.start = function(done) {
|
||||
setImmediate(done);
|
||||
};
|
||||
|
||||
Module.prototype.stop = function(done) {
|
||||
Service.prototype.stop = function(done) {
|
||||
setImmediate(done);
|
||||
};
|
||||
|
||||
module.exports = Module;
|
||||
module.exports = Service;
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
var BaseModule = require('../module');
|
||||
var BaseService = require('../service');
|
||||
var inherits = require('util').inherits;
|
||||
var async = require('async');
|
||||
var index = require('../');
|
||||
|
@ -14,30 +14,30 @@ var EventEmitter = require('events').EventEmitter;
|
|||
var PublicKey = bitcore.PublicKey;
|
||||
var Address = bitcore.Address;
|
||||
|
||||
var AddressModule = function(options) {
|
||||
BaseModule.call(this, options);
|
||||
var AddressService = function(options) {
|
||||
BaseService.call(this, options);
|
||||
|
||||
this.subscriptions = {};
|
||||
this.subscriptions['address/transaction'] = {};
|
||||
this.subscriptions['address/balance'] = {};
|
||||
|
||||
this.node.modules.bitcoind.on('tx', this.transactionHandler.bind(this));
|
||||
this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this));
|
||||
|
||||
};
|
||||
|
||||
inherits(AddressModule, BaseModule);
|
||||
inherits(AddressService, BaseService);
|
||||
|
||||
AddressModule.dependencies = [
|
||||
AddressService.dependencies = [
|
||||
'bitcoind',
|
||||
'db'
|
||||
];
|
||||
|
||||
AddressModule.PREFIXES = {
|
||||
AddressService.PREFIXES = {
|
||||
OUTPUTS: 'outs',
|
||||
SPENTS: 'sp'
|
||||
};
|
||||
|
||||
AddressModule.prototype.getAPIMethods = function() {
|
||||
AddressService.prototype.getAPIMethods = function() {
|
||||
return [
|
||||
['getBalance', this, this.getBalance, 2],
|
||||
['getOutputs', this, this.getOutputs, 2],
|
||||
|
@ -47,7 +47,7 @@ AddressModule.prototype.getAPIMethods = function() {
|
|||
];
|
||||
};
|
||||
|
||||
AddressModule.prototype.getPublishEvents = function() {
|
||||
AddressService.prototype.getPublishEvents = function() {
|
||||
return [
|
||||
{
|
||||
name: 'address/transaction',
|
||||
|
@ -73,7 +73,7 @@ AddressModule.prototype.getPublishEvents = function() {
|
|||
* @param {Number} outputIndex - The index of the output in the transaction
|
||||
* @param {Boolean} rejected - If the transaction was rejected by the mempool
|
||||
*/
|
||||
AddressModule.prototype.transactionOutputHandler = function(messages, tx, outputIndex, rejected) {
|
||||
AddressService.prototype.transactionOutputHandler = function(messages, tx, outputIndex, rejected) {
|
||||
var script = tx.outputs[outputIndex].script;
|
||||
|
||||
// If the script is invalid skip
|
||||
|
@ -112,7 +112,7 @@ AddressModule.prototype.transactionOutputHandler = function(messages, tx, output
|
|||
* @param {Boolean} txInfo.mempool - If the transaction was accepted in the mempool
|
||||
* @param {String} txInfo.hash - The hash of the transaction
|
||||
*/
|
||||
AddressModule.prototype.transactionHandler = function(txInfo) {
|
||||
AddressService.prototype.transactionHandler = function(txInfo) {
|
||||
|
||||
// Basic transaction format is handled by the daemon
|
||||
// and we can safely assume the buffer is properly formatted.
|
||||
|
@ -130,7 +130,7 @@ AddressModule.prototype.transactionHandler = function(txInfo) {
|
|||
}
|
||||
};
|
||||
|
||||
AddressModule.prototype.blockHandler = function(block, addOutput, callback) {
|
||||
AddressService.prototype.blockHandler = function(block, addOutput, callback) {
|
||||
var txs = block.transactions;
|
||||
|
||||
var action = 'put';
|
||||
|
@ -178,7 +178,7 @@ AddressModule.prototype.blockHandler = function(block, addOutput, callback) {
|
|||
var addressStr = address.toString();
|
||||
var scriptHex = output._scriptBuffer.toString('hex');
|
||||
|
||||
var key = [AddressModule.PREFIXES.OUTPUTS, addressStr, timestamp, txid, outputIndex].join('-');
|
||||
var key = [AddressService.PREFIXES.OUTPUTS, addressStr, timestamp, txid, outputIndex].join('-');
|
||||
var value = [output.satoshis, scriptHex, height].join(':');
|
||||
|
||||
operations.push({
|
||||
|
@ -217,7 +217,7 @@ AddressModule.prototype.blockHandler = function(block, addOutput, callback) {
|
|||
var input = inputs[k].toObject();
|
||||
operations.push({
|
||||
type: action,
|
||||
key: [AddressModule.PREFIXES.SPENTS, input.prevTxId, input.outputIndex].join('-'),
|
||||
key: [AddressService.PREFIXES.SPENTS, input.prevTxId, input.outputIndex].join('-'),
|
||||
value: [txid, k].join(':')
|
||||
});
|
||||
}
|
||||
|
@ -238,7 +238,7 @@ AddressModule.prototype.blockHandler = function(block, addOutput, callback) {
|
|||
* @param {Number} [obj.height] - The height of the block the transaction was included
|
||||
* @param {Boolean} [obj.rejected] - If the transaction was not accepted in the mempool
|
||||
*/
|
||||
AddressModule.prototype.transactionEventHandler = function(obj) {
|
||||
AddressService.prototype.transactionEventHandler = function(obj) {
|
||||
if(this.subscriptions['address/transaction'][obj.address]) {
|
||||
var emitters = this.subscriptions['address/transaction'][obj.address];
|
||||
for(var i = 0; i < emitters.length; i++) {
|
||||
|
@ -247,7 +247,7 @@ AddressModule.prototype.transactionEventHandler = function(obj) {
|
|||
}
|
||||
};
|
||||
|
||||
AddressModule.prototype.balanceEventHandler = function(block, address) {
|
||||
AddressService.prototype.balanceEventHandler = function(block, address) {
|
||||
if(this.subscriptions['address/balance'][address]) {
|
||||
var emitters = this.subscriptions['address/balance'][address];
|
||||
this.getBalance(address, true, function(err, balance) {
|
||||
|
@ -262,7 +262,7 @@ AddressModule.prototype.balanceEventHandler = function(block, address) {
|
|||
}
|
||||
};
|
||||
|
||||
AddressModule.prototype.subscribe = function(name, emitter, addresses) {
|
||||
AddressService.prototype.subscribe = function(name, emitter, addresses) {
|
||||
$.checkArgument(emitter instanceof EventEmitter, 'First argument is expected to be an EventEmitter');
|
||||
$.checkArgument(Array.isArray(addresses), 'Second argument is expected to be an Array of addresses');
|
||||
|
||||
|
@ -274,7 +274,7 @@ AddressModule.prototype.subscribe = function(name, emitter, addresses) {
|
|||
}
|
||||
};
|
||||
|
||||
AddressModule.prototype.unsubscribe = function(name, emitter, addresses) {
|
||||
AddressService.prototype.unsubscribe = function(name, emitter, addresses) {
|
||||
$.checkArgument(emitter instanceof EventEmitter, 'First argument is expected to be an EventEmitter');
|
||||
$.checkArgument(Array.isArray(addresses) || _.isUndefined(addresses), 'Second argument is expected to be an Array of addresses or undefined');
|
||||
|
||||
|
@ -293,7 +293,7 @@ AddressModule.prototype.unsubscribe = function(name, emitter, addresses) {
|
|||
}
|
||||
};
|
||||
|
||||
AddressModule.prototype.unsubscribeAll = function(name, emitter) {
|
||||
AddressService.prototype.unsubscribeAll = function(name, emitter) {
|
||||
$.checkArgument(emitter instanceof EventEmitter, 'First argument is expected to be an EventEmitter');
|
||||
|
||||
for(var address in this.subscriptions[name]) {
|
||||
|
@ -305,7 +305,7 @@ AddressModule.prototype.unsubscribeAll = function(name, emitter) {
|
|||
}
|
||||
};
|
||||
|
||||
AddressModule.prototype.getBalance = function(address, queryMempool, callback) {
|
||||
AddressService.prototype.getBalance = function(address, queryMempool, callback) {
|
||||
this.getUnspentOutputs(address, queryMempool, function(err, outputs) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
|
@ -323,13 +323,13 @@ AddressModule.prototype.getBalance = function(address, queryMempool, callback) {
|
|||
});
|
||||
};
|
||||
|
||||
AddressModule.prototype.getOutputs = function(addressStr, queryMempool, callback) {
|
||||
AddressService.prototype.getOutputs = function(addressStr, queryMempool, callback) {
|
||||
var self = this;
|
||||
|
||||
var outputs = [];
|
||||
var key = [AddressModule.PREFIXES.OUTPUTS, addressStr].join('-');
|
||||
var key = [AddressService.PREFIXES.OUTPUTS, addressStr].join('-');
|
||||
|
||||
var stream = this.node.modules.db.store.createReadStream({
|
||||
var stream = this.node.services.db.store.createReadStream({
|
||||
start: key,
|
||||
end: key + '~'
|
||||
});
|
||||
|
@ -347,7 +347,7 @@ AddressModule.prototype.getOutputs = function(addressStr, queryMempool, callback
|
|||
satoshis: Number(value[0]),
|
||||
script: value[1],
|
||||
blockHeight: Number(value[2]),
|
||||
confirmations: self.node.modules.db.tip.__height - Number(value[2]) + 1
|
||||
confirmations: self.node.services.db.tip.__height - Number(value[2]) + 1
|
||||
};
|
||||
|
||||
outputs.push(output);
|
||||
|
@ -368,7 +368,7 @@ AddressModule.prototype.getOutputs = function(addressStr, queryMempool, callback
|
|||
}
|
||||
|
||||
if(queryMempool) {
|
||||
outputs = outputs.concat(self.node.modules.bitcoind.getMempoolOutputs(addressStr));
|
||||
outputs = outputs.concat(self.node.services.bitcoind.getMempoolOutputs(addressStr));
|
||||
}
|
||||
|
||||
callback(null, outputs);
|
||||
|
@ -378,7 +378,7 @@ AddressModule.prototype.getOutputs = function(addressStr, queryMempool, callback
|
|||
|
||||
};
|
||||
|
||||
AddressModule.prototype.getUnspentOutputs = function(addresses, queryMempool, callback) {
|
||||
AddressService.prototype.getUnspentOutputs = function(addresses, queryMempool, callback) {
|
||||
var self = this;
|
||||
|
||||
if(!Array.isArray(addresses)) {
|
||||
|
@ -403,7 +403,7 @@ AddressModule.prototype.getUnspentOutputs = function(addresses, queryMempool, ca
|
|||
});
|
||||
};
|
||||
|
||||
AddressModule.prototype.getUnspentOutputsForAddress = function(address, queryMempool, callback) {
|
||||
AddressService.prototype.getUnspentOutputsForAddress = function(address, queryMempool, callback) {
|
||||
|
||||
var self = this;
|
||||
|
||||
|
@ -424,26 +424,26 @@ AddressModule.prototype.getUnspentOutputsForAddress = function(address, queryMem
|
|||
});
|
||||
};
|
||||
|
||||
AddressModule.prototype.isUnspent = function(output, queryMempool, callback) {
|
||||
AddressService.prototype.isUnspent = function(output, queryMempool, callback) {
|
||||
this.isSpent(output, queryMempool, function(spent) {
|
||||
callback(!spent);
|
||||
});
|
||||
};
|
||||
|
||||
AddressModule.prototype.isSpent = function(output, queryMempool, callback) {
|
||||
AddressService.prototype.isSpent = function(output, queryMempool, callback) {
|
||||
var self = this;
|
||||
var txid = output.prevTxId ? output.prevTxId.toString('hex') : output.txid;
|
||||
|
||||
setImmediate(function() {
|
||||
callback(self.node.modules.bitcoind.isSpent(txid, output.outputIndex));
|
||||
callback(self.node.services.bitcoind.isSpent(txid, output.outputIndex));
|
||||
});
|
||||
};
|
||||
|
||||
AddressModule.prototype.getSpendInfoForOutput = function(txid, outputIndex, callback) {
|
||||
AddressService.prototype.getSpendInfoForOutput = function(txid, outputIndex, callback) {
|
||||
var self = this;
|
||||
|
||||
var key = [AddressModule.PREFIXES.SPENTS, txid, outputIndex].join('-');
|
||||
this.node.modules.db.store.get(key, function(err, value) {
|
||||
var key = [AddressService.PREFIXES.SPENTS, txid, outputIndex].join('-');
|
||||
this.node.services.db.store.get(key, function(err, value) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -459,7 +459,7 @@ AddressModule.prototype.getSpendInfoForOutput = function(txid, outputIndex, call
|
|||
});
|
||||
};
|
||||
|
||||
AddressModule.prototype.getAddressHistory = function(addresses, queryMempool, callback) {
|
||||
AddressService.prototype.getAddressHistory = function(addresses, queryMempool, callback) {
|
||||
var self = this;
|
||||
|
||||
if(!Array.isArray(addresses)) {
|
||||
|
@ -482,7 +482,7 @@ AddressModule.prototype.getAddressHistory = function(addresses, queryMempool, ca
|
|||
});
|
||||
};
|
||||
|
||||
AddressModule.prototype.getAddressHistoryForAddress = function(address, queryMempool, callback) {
|
||||
AddressService.prototype.getAddressHistoryForAddress = function(address, queryMempool, callback) {
|
||||
var self = this;
|
||||
|
||||
var txinfos = {};
|
||||
|
@ -492,19 +492,19 @@ AddressModule.prototype.getAddressHistoryForAddress = function(address, queryMem
|
|||
return callback(null, txinfos[txid]);
|
||||
}
|
||||
|
||||
self.node.modules.db.getTransactionWithBlockInfo(txid, queryMempool, function(err, transaction) {
|
||||
self.node.services.db.getTransactionWithBlockInfo(txid, queryMempool, function(err, transaction) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
transaction.populateInputs(self.node.modules.db, [], function(err) {
|
||||
transaction.populateInputs(self.node.services.db, [], function(err) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var confirmations = 0;
|
||||
if(transaction.__height >= 0) {
|
||||
confirmations = self.node.modules.db.tip.__height - transaction.__height;
|
||||
confirmations = self.node.services.db.tip.__height - transaction.__height;
|
||||
}
|
||||
|
||||
txinfos[transaction.hash] = {
|
||||
|
@ -581,4 +581,4 @@ AddressModule.prototype.getAddressHistoryForAddress = function(address, queryMem
|
|||
});
|
||||
};
|
||||
|
||||
module.exports = AddressModule;
|
||||
module.exports = AddressService;
|
|
@ -8,8 +8,7 @@ var bitcore = require('bitcore');
|
|||
var $ = bitcore.util.preconditions;
|
||||
var index = require('../');
|
||||
var log = index.log;
|
||||
var Module = require('../module');
|
||||
|
||||
var Service = require('../service');
|
||||
|
||||
/**
|
||||
* Provides an interface to native bindings to Bitcoin Core
|
||||
|
@ -24,7 +23,7 @@ function Bitcoin(options) {
|
|||
|
||||
var self = this;
|
||||
|
||||
Module.call(this, options);
|
||||
Service.call(this, options);
|
||||
|
||||
if (Object.keys(this.instances).length) {
|
||||
throw new Error('Bitcoin cannot be instantiated more than once.');
|
||||
|
@ -38,7 +37,7 @@ function Bitcoin(options) {
|
|||
|
||||
}
|
||||
|
||||
util.inherits(Bitcoin, Module);
|
||||
util.inherits(Bitcoin, Service);
|
||||
|
||||
Bitcoin.dependencies = [];
|
||||
|
|
@ -15,13 +15,13 @@ var index = require('../');
|
|||
var errors = index.errors;
|
||||
var log = index.log;
|
||||
var Transaction = require('../transaction');
|
||||
var Module = require('../module');
|
||||
var Service = require('../service');
|
||||
var utils = require('../utils');
|
||||
|
||||
var MAX_STACK_DEPTH = 1000;
|
||||
|
||||
/**
|
||||
* Represents the current state of the bitcoin blockchain. Other modules
|
||||
* Represents the current state of the bitcoin blockchain. Other services
|
||||
* can extend the data that is indexed by implementing a `blockHandler` method.
|
||||
*
|
||||
* @param {Object} options
|
||||
|
@ -38,7 +38,7 @@ function DB(options) {
|
|||
options = {};
|
||||
}
|
||||
|
||||
Module.call(this, options);
|
||||
Service.call(this, options);
|
||||
|
||||
this.tip = null;
|
||||
this.genesis = null;
|
||||
|
@ -66,7 +66,7 @@ function DB(options) {
|
|||
};
|
||||
}
|
||||
|
||||
util.inherits(DB, Module);
|
||||
util.inherits(DB, Service);
|
||||
|
||||
DB.dependencies = ['bitcoind'];
|
||||
|
||||
|
@ -90,17 +90,17 @@ DB.prototype.start = function(callback) {
|
|||
mkdirp.sync(this.dataPath);
|
||||
}
|
||||
|
||||
this.genesis = Block.fromBuffer(this.node.modules.bitcoind.genesisBuffer);
|
||||
this.genesis = Block.fromBuffer(this.node.services.bitcoind.genesisBuffer);
|
||||
this.store = levelup(this.dataPath, { db: this.levelupStore });
|
||||
this.node.modules.bitcoind.on('tx', this.transactionHandler.bind(this));
|
||||
this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this));
|
||||
|
||||
this.once('ready', function() {
|
||||
log.info('Bitcoin Database Ready');
|
||||
|
||||
// Notify that there is a new tip
|
||||
self.node.modules.bitcoind.on('tip', function(height) {
|
||||
self.node.services.bitcoind.on('tip', function(height) {
|
||||
if(!self.node.stopping) {
|
||||
var percentage = self.node.modules.bitcoind.syncPercentage();
|
||||
var percentage = self.node.services.bitcoind.syncPercentage();
|
||||
log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage);
|
||||
self.sync();
|
||||
}
|
||||
|
@ -189,7 +189,7 @@ DB.prototype.getAPIMethods = function() {
|
|||
};
|
||||
|
||||
DB.prototype.getBlock = function(hash, callback) {
|
||||
this.node.modules.bitcoind.getBlock(hash, function(err, blockData) {
|
||||
this.node.services.bitcoind.getBlock(hash, function(err, blockData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -198,7 +198,7 @@ DB.prototype.getBlock = function(hash, callback) {
|
|||
};
|
||||
|
||||
DB.prototype.getTransaction = function(txid, queryMempool, callback) {
|
||||
this.node.modules.bitcoind.getTransaction(txid, queryMempool, function(err, txBuffer) {
|
||||
this.node.services.bitcoind.getTransaction(txid, queryMempool, function(err, txBuffer) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -211,7 +211,7 @@ DB.prototype.getTransaction = function(txid, queryMempool, callback) {
|
|||
};
|
||||
|
||||
DB.prototype.getTransactionWithBlockInfo = function(txid, queryMempool, callback) {
|
||||
this.node.modules.bitcoind.getTransactionWithBlockInfo(txid, queryMempool, function(err, obj) {
|
||||
this.node.services.bitcoind.getTransactionWithBlockInfo(txid, queryMempool, function(err, obj) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -231,7 +231,7 @@ DB.prototype.sendTransaction = function(tx, callback) {
|
|||
$.checkArgument(typeof tx === 'string', 'Argument must be a hex string or Transaction');
|
||||
|
||||
try {
|
||||
var txid = this.node.modules.bitcoind.sendTransaction(tx);
|
||||
var txid = this.node.services.bitcoind.sendTransaction(tx);
|
||||
return callback(null, txid);
|
||||
} catch(err) {
|
||||
return callback(err);
|
||||
|
@ -241,7 +241,7 @@ DB.prototype.sendTransaction = function(tx, callback) {
|
|||
DB.prototype.estimateFee = function(blocks, callback) {
|
||||
var self = this;
|
||||
setImmediate(function() {
|
||||
callback(null, self.node.modules.bitcoind.estimateFee(blocks));
|
||||
callback(null, self.node.services.bitcoind.estimateFee(blocks));
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -279,7 +279,7 @@ DB.prototype.unsubscribe = function(name, emitter) {
|
|||
* @param {Function} callback
|
||||
*/
|
||||
DB.prototype.getPrevHash = function(blockHash, callback) {
|
||||
var blockIndex = this.node.modules.bitcoind.getBlockIndex(blockHash);
|
||||
var blockIndex = this.node.services.bitcoind.getBlockIndex(blockHash);
|
||||
setImmediate(function() {
|
||||
if (blockIndex) {
|
||||
callback(null, blockIndex.prevHash);
|
||||
|
@ -364,7 +364,7 @@ DB.prototype.disconnectBlock = function(block, callback) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Will collect all database operations for a block from other modules
|
||||
* Will collect all database operations for a block from other services
|
||||
* and save to the database.
|
||||
* @param {Block} block - The bitcore block
|
||||
* @param {Boolean} add - If the block is being added/connected or removed/disconnected
|
||||
|
@ -380,7 +380,7 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
|
|||
}
|
||||
|
||||
async.eachSeries(
|
||||
this.node.modules,
|
||||
this.node.services,
|
||||
function(mod, next) {
|
||||
mod.blockHandler.call(mod, block, add, function(err, ops) {
|
||||
if (err) {
|
||||
|
@ -501,7 +501,7 @@ DB.prototype.findCommonAncestor = function(block, done) {
|
|||
// and thus don't need to find the entire chain of hashes.
|
||||
|
||||
while(ancestorHash && !currentHashesMap[ancestorHash]) {
|
||||
var blockIndex = self.node.modules.bitcoind.getBlockIndex(ancestorHash);
|
||||
var blockIndex = self.node.services.bitcoind.getBlockIndex(ancestorHash);
|
||||
ancestorHash = blockIndex ? blockIndex.prevHash : null;
|
||||
}
|
||||
|
||||
|
@ -595,9 +595,9 @@ DB.prototype.sync = function() {
|
|||
|
||||
async.whilst(function() {
|
||||
height = self.tip.__height;
|
||||
return height < self.node.modules.bitcoind.height && !self.node.stopping;
|
||||
return height < self.node.services.bitcoind.height && !self.node.stopping;
|
||||
}, function(done) {
|
||||
self.node.modules.bitcoind.getBlock(height + 1, function(err, blockBuffer) {
|
||||
self.node.services.bitcoind.getBlock(height + 1, function(err, blockBuffer) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
@ -659,7 +659,7 @@ DB.prototype.sync = function() {
|
|||
self.lastSavedMetadataThreshold = 0;
|
||||
|
||||
// If bitcoind is completely synced
|
||||
if (self.node.modules.bitcoind.isSynced()) {
|
||||
if (self.node.services.bitcoind.isSynced()) {
|
||||
self.node.emit('synced');
|
||||
}
|
||||
|
122
test/bus.unit.js
122
test/bus.unit.js
|
@ -7,28 +7,26 @@ var Bus = require('../lib/bus');
|
|||
describe('Bus', function() {
|
||||
|
||||
describe('#subscribe', function() {
|
||||
it('will call db and modules subscribe function with the correct arguments', function() {
|
||||
it('will call db and services subscribe function with the correct arguments', function() {
|
||||
var subscribeDb = sinon.spy();
|
||||
var subscribeModule = sinon.spy();
|
||||
var db = {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'dbtest',
|
||||
scope: this,
|
||||
subscribe: subscribeDb
|
||||
}
|
||||
]
|
||||
)
|
||||
};
|
||||
var subscribeService = sinon.spy();
|
||||
var node = {
|
||||
db: db,
|
||||
modules: {
|
||||
module1: {
|
||||
services: {
|
||||
db: {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'dbtest',
|
||||
scope: this,
|
||||
subscribe: subscribeDb
|
||||
}
|
||||
])
|
||||
},
|
||||
service1: {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'test',
|
||||
scope: this,
|
||||
subscribe: subscribeModule,
|
||||
subscribe: subscribeService,
|
||||
}
|
||||
])
|
||||
}
|
||||
|
@ -37,42 +35,40 @@ describe('Bus', function() {
|
|||
var bus = new Bus({node: node});
|
||||
bus.subscribe('dbtest', 'a', 'b', 'c');
|
||||
bus.subscribe('test', 'a', 'b', 'c');
|
||||
subscribeModule.callCount.should.equal(1);
|
||||
subscribeService.callCount.should.equal(1);
|
||||
subscribeDb.callCount.should.equal(1);
|
||||
subscribeDb.args[0][0].should.equal(bus);
|
||||
subscribeDb.args[0][1].should.equal('a');
|
||||
subscribeDb.args[0][2].should.equal('b');
|
||||
subscribeDb.args[0][3].should.equal('c');
|
||||
subscribeModule.args[0][0].should.equal(bus);
|
||||
subscribeModule.args[0][1].should.equal('a');
|
||||
subscribeModule.args[0][2].should.equal('b');
|
||||
subscribeModule.args[0][3].should.equal('c');
|
||||
subscribeService.args[0][0].should.equal(bus);
|
||||
subscribeService.args[0][1].should.equal('a');
|
||||
subscribeService.args[0][2].should.equal('b');
|
||||
subscribeService.args[0][3].should.equal('c');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#unsubscribe', function() {
|
||||
it('will call db and modules unsubscribe function with the correct arguments', function() {
|
||||
it('will call db and services unsubscribe function with the correct arguments', function() {
|
||||
var unsubscribeDb = sinon.spy();
|
||||
var unsubscribeModule = sinon.spy();
|
||||
var db = {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'dbtest',
|
||||
scope: this,
|
||||
unsubscribe: unsubscribeDb
|
||||
}
|
||||
]
|
||||
)
|
||||
};
|
||||
var unsubscribeService = sinon.spy();
|
||||
var node = {
|
||||
db: db,
|
||||
modules: {
|
||||
module1: {
|
||||
services: {
|
||||
db: {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'dbtest',
|
||||
scope: this,
|
||||
unsubscribe: unsubscribeDb
|
||||
}
|
||||
])
|
||||
},
|
||||
service1: {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'test',
|
||||
scope: this,
|
||||
unsubscribe: unsubscribeModule,
|
||||
unsubscribe: unsubscribeService,
|
||||
}
|
||||
])
|
||||
}
|
||||
|
@ -81,43 +77,41 @@ describe('Bus', function() {
|
|||
var bus = new Bus({node: node});
|
||||
bus.unsubscribe('dbtest', 'a', 'b', 'c');
|
||||
bus.unsubscribe('test', 'a', 'b', 'c');
|
||||
unsubscribeModule.callCount.should.equal(1);
|
||||
unsubscribeService.callCount.should.equal(1);
|
||||
unsubscribeDb.callCount.should.equal(1);
|
||||
unsubscribeDb.args[0][0].should.equal(bus);
|
||||
unsubscribeDb.args[0][1].should.equal('a');
|
||||
unsubscribeDb.args[0][2].should.equal('b');
|
||||
unsubscribeDb.args[0][3].should.equal('c');
|
||||
unsubscribeModule.args[0][0].should.equal(bus);
|
||||
unsubscribeModule.args[0][1].should.equal('a');
|
||||
unsubscribeModule.args[0][2].should.equal('b');
|
||||
unsubscribeModule.args[0][3].should.equal('c');
|
||||
unsubscribeService.args[0][0].should.equal(bus);
|
||||
unsubscribeService.args[0][1].should.equal('a');
|
||||
unsubscribeService.args[0][2].should.equal('b');
|
||||
unsubscribeService.args[0][3].should.equal('c');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#close', function() {
|
||||
it('will unsubscribe from all events', function() {
|
||||
var unsubscribeDb = sinon.spy();
|
||||
var unsubscribeModule = sinon.spy();
|
||||
var db = {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'dbtest',
|
||||
scope: this,
|
||||
unsubscribe: unsubscribeDb
|
||||
}
|
||||
]
|
||||
)
|
||||
};
|
||||
var unsubscribeService = sinon.spy();
|
||||
var node = {
|
||||
db: db,
|
||||
modules: {
|
||||
module1: {
|
||||
services: {
|
||||
db: {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'test',
|
||||
scope: this,
|
||||
unsubscribe: unsubscribeModule
|
||||
}
|
||||
{
|
||||
name: 'dbtest',
|
||||
scope: this,
|
||||
unsubscribe: unsubscribeDb
|
||||
}
|
||||
])
|
||||
},
|
||||
service1: {
|
||||
getPublishEvents: sinon.stub().returns([
|
||||
{
|
||||
name: 'test',
|
||||
scope: this,
|
||||
unsubscribe: unsubscribeService
|
||||
}
|
||||
])
|
||||
}
|
||||
}
|
||||
|
@ -126,11 +120,11 @@ describe('Bus', function() {
|
|||
bus.close();
|
||||
|
||||
unsubscribeDb.callCount.should.equal(1);
|
||||
unsubscribeModule.callCount.should.equal(1);
|
||||
unsubscribeService.callCount.should.equal(1);
|
||||
unsubscribeDb.args[0].length.should.equal(1);
|
||||
unsubscribeDb.args[0][0].should.equal(bus);
|
||||
unsubscribeModule.args[0].length.should.equal(1);
|
||||
unsubscribeModule.args[0][0].should.equal(bus);
|
||||
unsubscribeService.args[0].length.should.equal(1);
|
||||
unsubscribeService.args[0][0].should.equal(bus);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ var bitcore = require('bitcore');
|
|||
var Networks = bitcore.Networks;
|
||||
var proxyquire = require('proxyquire');
|
||||
var util = require('util');
|
||||
var BaseModule = require('../lib/module');
|
||||
var BaseService = require('../lib/service');
|
||||
|
||||
describe('Bitcore Node', function() {
|
||||
|
||||
|
@ -47,18 +47,18 @@ describe('Bitcore Node', function() {
|
|||
});
|
||||
|
||||
describe('@constructor', function() {
|
||||
var TestModule;
|
||||
var TestService;
|
||||
before(function() {
|
||||
TestModule = function TestModule() {};
|
||||
util.inherits(TestModule, BaseModule);
|
||||
TestService = function TestService() {};
|
||||
util.inherits(TestService, BaseService);
|
||||
});
|
||||
it('will set properties', function() {
|
||||
var config = {
|
||||
datadir: 'testdir',
|
||||
modules: [
|
||||
services: [
|
||||
{
|
||||
name: 'test1',
|
||||
module: TestModule
|
||||
module: TestService
|
||||
}
|
||||
],
|
||||
};
|
||||
|
@ -66,19 +66,19 @@ describe('Bitcore Node', function() {
|
|||
TestNode.prototype.start = sinon.spy();
|
||||
var node = new TestNode(config);
|
||||
TestNode.prototype.start.callCount.should.equal(1);
|
||||
node._unloadedModules.length.should.equal(1);
|
||||
node._unloadedModules[0].name.should.equal('test1');
|
||||
node._unloadedModules[0].module.should.equal(TestModule);
|
||||
node._unloadedServices.length.should.equal(1);
|
||||
node._unloadedServices[0].name.should.equal('test1');
|
||||
node._unloadedServices[0].module.should.equal(TestService);
|
||||
node.network.should.equal(Networks.defaultNetwork);
|
||||
});
|
||||
it('will set network to testnet', function() {
|
||||
var config = {
|
||||
network: 'testnet',
|
||||
datadir: 'testdir',
|
||||
modules: [
|
||||
services: [
|
||||
{
|
||||
name: 'test1',
|
||||
module: TestModule
|
||||
module: TestService
|
||||
}
|
||||
],
|
||||
};
|
||||
|
@ -91,10 +91,10 @@ describe('Bitcore Node', function() {
|
|||
var config = {
|
||||
network: 'regtest',
|
||||
datadir: 'testdir',
|
||||
modules: [
|
||||
services: [
|
||||
{
|
||||
name: 'test1',
|
||||
module: TestModule
|
||||
module: TestService
|
||||
}
|
||||
],
|
||||
};
|
||||
|
@ -108,10 +108,10 @@ describe('Bitcore Node', function() {
|
|||
it('should emit error if an error occurred starting services', function(done) {
|
||||
var config = {
|
||||
datadir: 'testdir',
|
||||
modules: [
|
||||
services: [
|
||||
{
|
||||
name: 'test1',
|
||||
module: TestModule
|
||||
module: TestService
|
||||
}
|
||||
],
|
||||
};
|
||||
|
@ -139,16 +139,16 @@ describe('Bitcore Node', function() {
|
|||
});
|
||||
|
||||
describe('#getAllAPIMethods', function() {
|
||||
it('should return db methods and modules methods', function() {
|
||||
it('should return db methods and service methods', function() {
|
||||
var node = new Node(baseConfig);
|
||||
node.modules = {
|
||||
node.services = {
|
||||
db: {
|
||||
getAPIMethods: sinon.stub().returns(['db1', 'db2']),
|
||||
},
|
||||
module1: {
|
||||
service1: {
|
||||
getAPIMethods: sinon.stub().returns(['mda1', 'mda2'])
|
||||
},
|
||||
module2: {
|
||||
service2: {
|
||||
getAPIMethods: sinon.stub().returns(['mdb1', 'mdb2'])
|
||||
}
|
||||
};
|
||||
|
@ -159,16 +159,16 @@ describe('Bitcore Node', function() {
|
|||
});
|
||||
|
||||
describe('#getAllPublishEvents', function() {
|
||||
it('should return modules publish events', function() {
|
||||
it('should return services publish events', function() {
|
||||
var node = new Node(baseConfig);
|
||||
node.modules = {
|
||||
node.services = {
|
||||
db: {
|
||||
getPublishEvents: sinon.stub().returns(['db1', 'db2']),
|
||||
},
|
||||
module1: {
|
||||
service1: {
|
||||
getPublishEvents: sinon.stub().returns(['mda1', 'mda2'])
|
||||
},
|
||||
module2: {
|
||||
service2: {
|
||||
getPublishEvents: sinon.stub().returns(['mdb1', 'mdb2'])
|
||||
}
|
||||
};
|
||||
|
@ -180,7 +180,7 @@ describe('Bitcore Node', function() {
|
|||
describe('#getServiceOrder', function() {
|
||||
it('should return the services in the correct order', function() {
|
||||
var node = new Node(baseConfig);
|
||||
node._unloadedModules = [
|
||||
node._unloadedServices = [
|
||||
{
|
||||
name: 'chain',
|
||||
dependencies: ['db']
|
||||
|
@ -206,46 +206,46 @@ describe('Bitcore Node', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('#_instantiateModule', function() {
|
||||
describe('#_instantiateService', function() {
|
||||
it('will instantiate an instance and load api methods', function() {
|
||||
var node = new Node(baseConfig);
|
||||
function TestModule() {}
|
||||
util.inherits(TestModule, BaseModule);
|
||||
TestModule.prototype.getData = function() {};
|
||||
TestModule.prototype.getAPIMethods = function() {
|
||||
function TestService() {}
|
||||
util.inherits(TestService, BaseService);
|
||||
TestService.prototype.getData = function() {};
|
||||
TestService.prototype.getAPIMethods = function() {
|
||||
return [
|
||||
['getData', this, this.getData, 1]
|
||||
];
|
||||
};
|
||||
var service = {
|
||||
name: 'testmodule',
|
||||
module: TestModule
|
||||
name: 'testservice',
|
||||
module: TestService
|
||||
};
|
||||
node._instantiateModule(service);
|
||||
should.exist(node.modules.testmodule);
|
||||
node._instantiateService(service);
|
||||
should.exist(node.services.testservice);
|
||||
should.exist(node.getData);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#start', function() {
|
||||
it('will call start for each module', function(done) {
|
||||
it('will call start for each service', function(done) {
|
||||
var node = new Node(baseConfig);
|
||||
|
||||
function TestModule() {}
|
||||
util.inherits(TestModule, BaseModule);
|
||||
TestModule.prototype.start = sinon.stub().callsArg(0);
|
||||
TestModule.prototype.getData = function() {};
|
||||
TestModule.prototype.getAPIMethods = function() {
|
||||
function TestService() {}
|
||||
util.inherits(TestService, BaseService);
|
||||
TestService.prototype.start = sinon.stub().callsArg(0);
|
||||
TestService.prototype.getData = function() {};
|
||||
TestService.prototype.getAPIMethods = function() {
|
||||
return [
|
||||
['getData', this, this.getData, 1]
|
||||
];
|
||||
};
|
||||
|
||||
function TestModule2() {}
|
||||
util.inherits(TestModule2, BaseModule);
|
||||
TestModule2.prototype.start = sinon.stub().callsArg(0);
|
||||
TestModule2.prototype.getData2 = function() {};
|
||||
TestModule2.prototype.getAPIMethods = function() {
|
||||
function TestService2() {}
|
||||
util.inherits(TestService2, BaseService);
|
||||
TestService2.prototype.start = sinon.stub().callsArg(0);
|
||||
TestService2.prototype.getData2 = function() {};
|
||||
TestService2.prototype.getAPIMethods = function() {
|
||||
return [
|
||||
['getData2', this, this.getData2, 1]
|
||||
];
|
||||
|
@ -254,16 +254,16 @@ describe('Bitcore Node', function() {
|
|||
node.getServiceOrder = sinon.stub().returns([
|
||||
{
|
||||
name: 'test1',
|
||||
module: TestModule
|
||||
module: TestService
|
||||
},
|
||||
{
|
||||
name: 'test2',
|
||||
module: TestModule2
|
||||
module: TestService2
|
||||
}
|
||||
]);
|
||||
node.start(function() {
|
||||
TestModule2.prototype.start.callCount.should.equal(1);
|
||||
TestModule.prototype.start.callCount.should.equal(1);
|
||||
TestService2.prototype.start.callCount.should.equal(1);
|
||||
TestService.prototype.start.callCount.should.equal(1);
|
||||
should.exist(node.getData2);
|
||||
should.exist(node.getData);
|
||||
done();
|
||||
|
@ -272,21 +272,21 @@ describe('Bitcore Node', function() {
|
|||
it('will error if there are conflicting API methods', function(done) {
|
||||
var node = new Node(baseConfig);
|
||||
|
||||
function TestModule() {}
|
||||
util.inherits(TestModule, BaseModule);
|
||||
TestModule.prototype.start = sinon.stub().callsArg(0);
|
||||
TestModule.prototype.getData = function() {};
|
||||
TestModule.prototype.getAPIMethods = function() {
|
||||
function TestService() {}
|
||||
util.inherits(TestService, BaseService);
|
||||
TestService.prototype.start = sinon.stub().callsArg(0);
|
||||
TestService.prototype.getData = function() {};
|
||||
TestService.prototype.getAPIMethods = function() {
|
||||
return [
|
||||
['getData', this, this.getData, 1]
|
||||
];
|
||||
};
|
||||
|
||||
function ConflictModule() {}
|
||||
util.inherits(ConflictModule, BaseModule);
|
||||
ConflictModule.prototype.start = sinon.stub().callsArg(0);
|
||||
ConflictModule.prototype.getData = function() {};
|
||||
ConflictModule.prototype.getAPIMethods = function() {
|
||||
function ConflictService() {}
|
||||
util.inherits(ConflictService, BaseService);
|
||||
ConflictService.prototype.start = sinon.stub().callsArg(0);
|
||||
ConflictService.prototype.getData = function() {};
|
||||
ConflictService.prototype.getAPIMethods = function() {
|
||||
return [
|
||||
['getData', this, this.getData, 1]
|
||||
];
|
||||
|
@ -295,11 +295,11 @@ describe('Bitcore Node', function() {
|
|||
node.getServiceOrder = sinon.stub().returns([
|
||||
{
|
||||
name: 'test',
|
||||
module: TestModule
|
||||
module: TestService
|
||||
},
|
||||
{
|
||||
name: 'conflict',
|
||||
module: ConflictModule
|
||||
module: ConflictService
|
||||
}
|
||||
]);
|
||||
|
||||
|
@ -313,30 +313,30 @@ describe('Bitcore Node', function() {
|
|||
});
|
||||
|
||||
describe('#stop', function() {
|
||||
it('will call stop for each module', function(done) {
|
||||
it('will call stop for each service', function(done) {
|
||||
var node = new Node(baseConfig);
|
||||
function TestModule() {}
|
||||
util.inherits(TestModule, BaseModule);
|
||||
TestModule.prototype.stop = sinon.stub().callsArg(0);
|
||||
TestModule.prototype.getData = function() {};
|
||||
TestModule.prototype.getAPIMethods = function() {
|
||||
function TestService() {}
|
||||
util.inherits(TestService, BaseService);
|
||||
TestService.prototype.stop = sinon.stub().callsArg(0);
|
||||
TestService.prototype.getData = function() {};
|
||||
TestService.prototype.getAPIMethods = function() {
|
||||
return [
|
||||
['getData', this, this.getData, 1]
|
||||
];
|
||||
};
|
||||
node.modules = {
|
||||
'test1': new TestModule({node: node})
|
||||
node.services = {
|
||||
'test1': new TestService({node: node})
|
||||
};
|
||||
node.test2 = {};
|
||||
node.test2.stop = sinon.stub().callsArg(0);
|
||||
node.getServiceOrder = sinon.stub().returns([
|
||||
{
|
||||
name: 'test1',
|
||||
module: TestModule
|
||||
module: TestService
|
||||
}
|
||||
]);
|
||||
node.stop(function() {
|
||||
TestModule.prototype.stop.callCount.should.equal(1);
|
||||
TestService.prototype.stop.callCount.should.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -15,7 +15,7 @@ describe('#add', function() {
|
|||
var testDir = path.resolve(basePath, 'temporary-test-data');
|
||||
var startConfig = {
|
||||
name: 'My Node',
|
||||
modules: []
|
||||
services: []
|
||||
};
|
||||
var startPackage = {};
|
||||
|
||||
|
@ -56,7 +56,7 @@ describe('#add', function() {
|
|||
it('will give an error if expected files do not exist', function(done) {
|
||||
add({
|
||||
path: path.resolve(testDir, 's0'),
|
||||
modules: ['a', 'b', 'c']
|
||||
services: ['a', 'b', 'c']
|
||||
}, function(err) {
|
||||
should.exist(err);
|
||||
err.message.match(/^Invalid state/);
|
||||
|
@ -82,15 +82,15 @@ describe('#add', function() {
|
|||
|
||||
addtest({
|
||||
path: path.resolve(testDir, 's0/s1/'),
|
||||
modules: ['a', 'b', 'c']
|
||||
services: ['a', 'b', 'c']
|
||||
}, function(err) {
|
||||
should.exist(err);
|
||||
err.message.should.equal('There was an error installing module: a');
|
||||
err.message.should.equal('There was an error installing service: a');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('will update bitcore-node.json modules', function(done) {
|
||||
it('will update bitcore-node.json services', function(done) {
|
||||
var spawn = sinon.stub().returns({
|
||||
stdout: {
|
||||
on: sinon.stub()
|
||||
|
@ -107,12 +107,12 @@ describe('#add', function() {
|
|||
});
|
||||
addtest({
|
||||
path: path.resolve(testDir, 's0/s1/'),
|
||||
modules: ['a', 'b', 'c']
|
||||
services: ['a', 'b', 'c']
|
||||
}, function(err) {
|
||||
should.not.exist(err);
|
||||
var configPath = path.resolve(testDir, 's0/s1/bitcore-node.json');
|
||||
var config = JSON.parse(fs.readFileSync(configPath));
|
||||
config.modules.should.deep.equal(['a','b','c']);
|
||||
config.services.should.deep.equal(['a','b','c']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -75,7 +75,7 @@ describe('#create', function() {
|
|||
|
||||
var config = JSON.parse(fs.readFileSync(configPath));
|
||||
config.name.should.equal('My Node 1');
|
||||
config.modules.should.deep.equal(['address']);
|
||||
config.services.should.deep.equal(['address']);
|
||||
config.datadir.should.equal('./data');
|
||||
config.network.should.equal('livenet');
|
||||
|
||||
|
|
|
@ -3,18 +3,18 @@
|
|||
var should = require('chai').should();
|
||||
var sinon = require('sinon');
|
||||
var proxyquire = require('proxyquire');
|
||||
var AddressModule = require('../../lib/modules/address');
|
||||
var AddressService = require('../../lib/services/address');
|
||||
|
||||
describe('#start', function() {
|
||||
|
||||
describe('will dynamically create a node from a configuration', function() {
|
||||
|
||||
it('require each bitcore-node module', function(done) {
|
||||
it('require each bitcore-node service', function(done) {
|
||||
var node;
|
||||
var TestNode = function(options) {
|
||||
options.modules[0].should.deep.equal({
|
||||
options.services[0].should.deep.equal({
|
||||
name: 'address',
|
||||
module: AddressModule,
|
||||
module: AddressService,
|
||||
dependencies: ['bitcoind', 'db']
|
||||
});
|
||||
};
|
||||
|
@ -30,7 +30,7 @@ describe('#start', function() {
|
|||
node = starttest({
|
||||
path: __dirname,
|
||||
config: {
|
||||
modules: [
|
||||
services: [
|
||||
'address'
|
||||
],
|
||||
datadir: './data'
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
var should = require('chai').should();
|
||||
var sinon = require('sinon');
|
||||
var bitcorenode = require('../../');
|
||||
var AddressModule = bitcorenode.modules.AddressModule;
|
||||
var AddressService = bitcorenode.services.Address;
|
||||
var blockData = require('../data/livenet-345003.json');
|
||||
var bitcore = require('bitcore');
|
||||
var Networks = bitcore.Networks;
|
||||
|
@ -16,18 +16,18 @@ var mockdb = {
|
|||
|
||||
var mocknode = {
|
||||
db: mockdb,
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
describe('Address Module', function() {
|
||||
describe('Address Service', function() {
|
||||
|
||||
describe('#getAPIMethods', function() {
|
||||
it('should return the correct methods', function() {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var methods = am.getAPIMethods();
|
||||
methods.length.should.equal(5);
|
||||
});
|
||||
|
@ -35,7 +35,7 @@ describe('Address Module', function() {
|
|||
|
||||
describe('#getPublishEvents', function() {
|
||||
it('will return an array of publish event objects', function() {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
am.subscribe = sinon.spy();
|
||||
am.unsubscribe = sinon.spy();
|
||||
var events = am.getPublishEvents();
|
||||
|
@ -71,7 +71,7 @@ describe('Address Module', function() {
|
|||
it('create a message for an address', function() {
|
||||
var txBuf = new Buffer('01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000', 'hex');
|
||||
var tx = bitcore.Transaction().fromBuffer(txBuf);
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
am.node.network = Networks.livenet;
|
||||
var address = '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX';
|
||||
var messages = {};
|
||||
|
@ -88,7 +88,7 @@ describe('Address Module', function() {
|
|||
describe('#transactionHandler', function() {
|
||||
it('will pass outputs to transactionOutputHandler and call transactionEventHandler', function() {
|
||||
var txBuf = new Buffer('01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000', 'hex');
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var address = '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX';
|
||||
var message = {};
|
||||
am.transactionOutputHandler = function(messages) {
|
||||
|
@ -153,7 +153,7 @@ describe('Address Module', function() {
|
|||
var value64 = data[2].value;
|
||||
|
||||
before(function() {
|
||||
am = new AddressModule({node: mocknode});
|
||||
am = new AddressService({node: mocknode});
|
||||
am.node.network = Networks.livenet;
|
||||
});
|
||||
|
||||
|
@ -208,7 +208,7 @@ describe('Address Module', function() {
|
|||
});
|
||||
});
|
||||
it('should continue if output script is null', function(done) {
|
||||
var am = new AddressModule({node: mocknode, network: 'livenet'});
|
||||
var am = new AddressService({node: mocknode, network: 'livenet'});
|
||||
|
||||
var block = {
|
||||
__height: 345003,
|
||||
|
@ -240,13 +240,13 @@ describe('Address Module', function() {
|
|||
var db = {};
|
||||
var testnode = {
|
||||
db: db,
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
var am = new AddressModule({node: testnode, network: 'livenet'});
|
||||
var am = new AddressService({node: testnode, network: 'livenet'});
|
||||
am.transactionEventHandler = sinon.spy();
|
||||
am.balanceEventHandler = sinon.spy();
|
||||
|
||||
|
@ -274,7 +274,7 @@ describe('Address Module', function() {
|
|||
|
||||
describe('#transactionEventHandler', function() {
|
||||
it('will emit a transaction if there is a subscriber', function(done) {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var emitter = new EventEmitter();
|
||||
am.subscriptions['address/transaction'] = {
|
||||
'1DzjESe6SLmAKVPLFMj6Sx1sWki3qt5i8N': [emitter]
|
||||
|
@ -304,7 +304,7 @@ describe('Address Module', function() {
|
|||
|
||||
describe('#balanceEventHandler', function() {
|
||||
it('will emit a balance if there is a subscriber', function(done) {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var emitter = new EventEmitter();
|
||||
am.subscriptions['address/balance'] = {
|
||||
'1DzjESe6SLmAKVPLFMj6Sx1sWki3qt5i8N': [emitter]
|
||||
|
@ -324,7 +324,7 @@ describe('Address Module', function() {
|
|||
|
||||
describe('#subscribe', function() {
|
||||
it('will add emitters to the subscribers array (transaction)', function() {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var emitter = new EventEmitter();
|
||||
|
||||
var address = '1DzjESe6SLmAKVPLFMj6Sx1sWki3qt5i8N';
|
||||
|
@ -341,7 +341,7 @@ describe('Address Module', function() {
|
|||
am.subscriptions['address/transaction'][address].should.deep.equal([emitter, emitter2]);
|
||||
});
|
||||
it('will add an emitter to the subscribers array (balance)', function() {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var emitter = new EventEmitter();
|
||||
var name = 'address/balance';
|
||||
var address = '1DzjESe6SLmAKVPLFMj6Sx1sWki3qt5i8N';
|
||||
|
@ -360,7 +360,7 @@ describe('Address Module', function() {
|
|||
|
||||
describe('#unsubscribe', function() {
|
||||
it('will remove emitter from subscribers array (transaction)', function() {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var emitter = new EventEmitter();
|
||||
var emitter2 = new EventEmitter();
|
||||
var address = '1DzjESe6SLmAKVPLFMj6Sx1sWki3qt5i8N';
|
||||
|
@ -370,7 +370,7 @@ describe('Address Module', function() {
|
|||
am.subscriptions['address/transaction'][address].should.deep.equal([emitter2]);
|
||||
});
|
||||
it('will remove emitter from subscribers array (balance)', function() {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var emitter = new EventEmitter();
|
||||
var emitter2 = new EventEmitter();
|
||||
var address = '1DzjESe6SLmAKVPLFMj6Sx1sWki3qt5i8N';
|
||||
|
@ -380,7 +380,7 @@ describe('Address Module', function() {
|
|||
am.subscriptions['address/balance'][address].should.deep.equal([emitter2]);
|
||||
});
|
||||
it('should unsubscribe from all addresses if no addresses are specified', function() {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var emitter = new EventEmitter();
|
||||
var emitter2 = new EventEmitter();
|
||||
am.subscriptions['address/balance'] = {
|
||||
|
@ -397,7 +397,7 @@ describe('Address Module', function() {
|
|||
|
||||
describe('#getBalance', function() {
|
||||
it('should sum up the unspent outputs', function(done) {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
var outputs = [
|
||||
{satoshis: 1000}, {satoshis: 2000}, {satoshis: 3000}
|
||||
];
|
||||
|
@ -410,7 +410,7 @@ describe('Address Module', function() {
|
|||
});
|
||||
|
||||
it('will handle error from unspent outputs', function(done) {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
am.getUnspentOutputs = sinon.stub().callsArgWith(2, new Error('error'));
|
||||
am.getBalance('someaddress', false, function(err) {
|
||||
should.exist(err);
|
||||
|
@ -430,7 +430,7 @@ describe('Address Module', function() {
|
|||
}
|
||||
};
|
||||
var testnode = {
|
||||
modules: {
|
||||
services: {
|
||||
db: db,
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
|
@ -439,12 +439,12 @@ describe('Address Module', function() {
|
|||
};
|
||||
|
||||
before(function() {
|
||||
am = new AddressModule({node: testnode});
|
||||
am = new AddressService({node: testnode});
|
||||
});
|
||||
|
||||
it('should get outputs for an address', function(done) {
|
||||
var readStream1 = new EventEmitter();
|
||||
am.node.modules.db.store = {
|
||||
am.node.services.db.store = {
|
||||
createReadStream: sinon.stub().returns(readStream1)
|
||||
};
|
||||
var mempoolOutputs = [
|
||||
|
@ -456,7 +456,7 @@ describe('Address Module', function() {
|
|||
blockHeight: 352532
|
||||
}
|
||||
];
|
||||
am.node.modules.bitcoind = {
|
||||
am.node.services.bitcoind = {
|
||||
getMempoolOutputs: sinon.stub().returns(mempoolOutputs)
|
||||
};
|
||||
|
||||
|
@ -499,7 +499,7 @@ describe('Address Module', function() {
|
|||
|
||||
it('should give an error if the readstream has an error', function(done) {
|
||||
var readStream2 = new EventEmitter();
|
||||
am.node.modules.db.store = {
|
||||
am.node.services.db.store = {
|
||||
createReadStream: sinon.stub().returns(readStream2)
|
||||
};
|
||||
|
||||
|
@ -526,14 +526,14 @@ describe('Address Module', function() {
|
|||
|
||||
var db = {};
|
||||
var testnode = {
|
||||
modules: {
|
||||
services: {
|
||||
db: db,
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
var am = new AddressModule({node: testnode});
|
||||
var am = new AddressService({node: testnode});
|
||||
am.getUnspentOutputsForAddress = function(address, queryMempool, callback) {
|
||||
var result = addresses[address];
|
||||
if(result instanceof Error) {
|
||||
|
@ -559,13 +559,13 @@ describe('Address Module', function() {
|
|||
var db = {};
|
||||
var testnode = {
|
||||
db: db,
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
var am = new AddressModule({node: testnode});
|
||||
var am = new AddressService({node: testnode});
|
||||
am.getUnspentOutputsForAddress = function(address, queryMempool, callback) {
|
||||
var result = addresses[address];
|
||||
if(result instanceof Error) {
|
||||
|
@ -592,13 +592,13 @@ describe('Address Module', function() {
|
|||
var db = {};
|
||||
var testnode = {
|
||||
db: db,
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
var am = new AddressModule({node: testnode});
|
||||
var am = new AddressService({node: testnode});
|
||||
am.getUnspentOutputsForAddress = function(address, queryMempool, callback) {
|
||||
var result = addresses[address];
|
||||
if(result instanceof Error) {
|
||||
|
@ -634,7 +634,7 @@ describe('Address Module', function() {
|
|||
];
|
||||
var i = 0;
|
||||
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
am.getOutputs = sinon.stub().callsArgWith(2, null, outputs);
|
||||
am.isUnspent = function(output, queryMempool, callback) {
|
||||
callback(!outputs[i].spent);
|
||||
|
@ -650,7 +650,7 @@ describe('Address Module', function() {
|
|||
});
|
||||
});
|
||||
it('should handle an error from getOutputs', function(done) {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
am.getOutputs = sinon.stub().callsArgWith(2, new Error('error'));
|
||||
am.getUnspentOutputsForAddress('1KiW1A4dx1oRgLHtDtBjcunUGkYtFgZ1W', false, function(err, outputs) {
|
||||
should.exist(err);
|
||||
|
@ -659,7 +659,7 @@ describe('Address Module', function() {
|
|||
});
|
||||
});
|
||||
it('should handle when there are no outputs', function(done) {
|
||||
var am = new AddressModule({node: mocknode});
|
||||
var am = new AddressService({node: mocknode});
|
||||
am.getOutputs = sinon.stub().callsArgWith(2, null, []);
|
||||
am.getUnspentOutputsForAddress('1KiW1A4dx1oRgLHtDtBjcunUGkYtFgZ1W', false, function(err, outputs) {
|
||||
should.exist(err);
|
||||
|
@ -674,7 +674,7 @@ describe('Address Module', function() {
|
|||
var am;
|
||||
|
||||
before(function() {
|
||||
am = new AddressModule({node: mocknode});
|
||||
am = new AddressService({node: mocknode});
|
||||
});
|
||||
|
||||
it('should give true when isSpent() gives false', function(done) {
|
||||
|
@ -707,15 +707,15 @@ describe('Address Module', function() {
|
|||
var db = {};
|
||||
var testnode = {
|
||||
db: db,
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
before(function() {
|
||||
am = new AddressModule({node: testnode});
|
||||
am.node.modules.bitcoind = {
|
||||
am = new AddressService({node: testnode});
|
||||
am.node.services.bitcoind = {
|
||||
isSpent: sinon.stub().returns(true),
|
||||
on: sinon.stub()
|
||||
};
|
||||
|
@ -737,14 +737,14 @@ describe('Address Module', function() {
|
|||
}
|
||||
};
|
||||
var testnode = {
|
||||
modules: {
|
||||
services: {
|
||||
db: db,
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
var am = new AddressModule({node: testnode});
|
||||
var am = new AddressService({node: testnode});
|
||||
am.getSpendInfoForOutput('txid', 3, function(err, info) {
|
||||
should.not.exist(err);
|
||||
info.txid.should.equal('spendtxid');
|
||||
|
@ -855,14 +855,14 @@ describe('Address Module', function() {
|
|||
}
|
||||
};
|
||||
var testnode = {
|
||||
modules: {
|
||||
services: {
|
||||
db: db,
|
||||
bitcoind: {
|
||||
on: sinon.stub()
|
||||
}
|
||||
}
|
||||
};
|
||||
var am = new AddressModule({node: testnode});
|
||||
var am = new AddressService({node: testnode});
|
||||
|
||||
am.getOutputs = sinon.stub().callsArgWith(2, null, incoming);
|
||||
am.getSpendInfoForOutput = function(txid, outputIndex, callback) {
|
|
@ -4,18 +4,18 @@ var should = require('chai').should();
|
|||
var proxyquire = require('proxyquire');
|
||||
var fs = require('fs');
|
||||
var sinon = require('sinon');
|
||||
var BitcoinModule = proxyquire('../../lib/modules/bitcoind', {
|
||||
var BitcoinService = proxyquire('../../lib/services/bitcoind', {
|
||||
fs: {
|
||||
readFileSync: sinon.stub().returns(fs.readFileSync(__dirname + '/../data/bitcoin.conf'))
|
||||
}
|
||||
});
|
||||
var BadBitcoin = proxyquire('../../lib/modules/bitcoind', {
|
||||
var BadBitcoin = proxyquire('../../lib/services/bitcoind', {
|
||||
fs: {
|
||||
readFileSync: sinon.stub().returns(fs.readFileSync(__dirname + '/../data/badbitcoin.conf'))
|
||||
}
|
||||
});
|
||||
|
||||
describe('Bitcoin Module', function() {
|
||||
describe('Bitcoin Service', function() {
|
||||
var baseConfig = {
|
||||
node: {
|
||||
datadir: 'testdir',
|
||||
|
@ -26,7 +26,7 @@ describe('Bitcoin Module', function() {
|
|||
};
|
||||
describe('#_loadConfiguration', function() {
|
||||
it('will parse a bitcoin.conf file', function() {
|
||||
var bitcoind = new BitcoinModule(baseConfig);
|
||||
var bitcoind = new BitcoinService(baseConfig);
|
||||
bitcoind._loadConfiguration({datadir: process.env.HOME + '/.bitcoin'});
|
||||
should.exist(bitcoind.configuration);
|
||||
bitcoind.configuration.should.deep.equal({
|
|
@ -5,7 +5,7 @@ var sinon = require('sinon');
|
|||
var EventEmitter = require('events').EventEmitter;
|
||||
var proxyquire = require('proxyquire');
|
||||
var index = require('../../');
|
||||
var DB = index.modules.DBModule;
|
||||
var DB = index.services.DB;
|
||||
var blockData = require('../data/livenet-345003.json');
|
||||
var bitcore = require('bitcore');
|
||||
var Networks = bitcore.Networks;
|
||||
|
@ -19,7 +19,7 @@ var memdown = require('memdown');
|
|||
var bitcore = require('bitcore');
|
||||
var Transaction = bitcore.Transaction;
|
||||
|
||||
describe('DB Module', function() {
|
||||
describe('DB Service', function() {
|
||||
|
||||
function hexlebuf(hexString){
|
||||
return BufferUtil.reverse(new Buffer(hexString, 'hex'));
|
||||
|
@ -106,7 +106,7 @@ describe('DB Module', function() {
|
|||
var genesisBuffer;
|
||||
|
||||
before(function() {
|
||||
TestDB = proxyquire('../../lib/modules/db', {
|
||||
TestDB = proxyquire('../../lib/services/db', {
|
||||
fs: {
|
||||
existsSync: sinon.stub().returns(true)
|
||||
},
|
||||
|
@ -118,12 +118,11 @@ describe('DB Module', function() {
|
|||
it('should emit ready', function(done) {
|
||||
var db = new TestDB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
on: sinon.spy(),
|
||||
genesisBuffer: genesisBuffer
|
||||
};
|
||||
db._addModule = sinon.spy();
|
||||
db.getMetadata = sinon.stub().callsArg(0);
|
||||
db.connectBlock = sinon.stub().callsArg(1);
|
||||
db.saveMetadata = sinon.stub();
|
||||
|
@ -142,7 +141,7 @@ describe('DB Module', function() {
|
|||
var node = {
|
||||
network: Networks.testnet,
|
||||
datadir: 'testdir',
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
genesisBuffer: genesisBuffer,
|
||||
on: sinon.stub()
|
||||
|
@ -165,7 +164,7 @@ describe('DB Module', function() {
|
|||
var node = {
|
||||
network: Networks.testnet,
|
||||
datadir: 'testdir',
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
genesisBuffer: genesisBuffer,
|
||||
on: sinon.stub()
|
||||
|
@ -193,7 +192,7 @@ describe('DB Module', function() {
|
|||
var node = {
|
||||
network: Networks.testnet,
|
||||
datadir: 'testdir',
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
genesisBuffer: genesisBuffer,
|
||||
on: sinon.stub()
|
||||
|
@ -213,7 +212,7 @@ describe('DB Module', function() {
|
|||
var node = {
|
||||
network: Networks.testnet,
|
||||
datadir: 'testdir',
|
||||
modules: {
|
||||
services: {
|
||||
bitcoind: {
|
||||
genesisBuffer: genesisBuffer,
|
||||
on: sinon.stub()
|
||||
|
@ -236,29 +235,29 @@ describe('DB Module', function() {
|
|||
|
||||
it('will call sync when there is a new tip', function(done) {
|
||||
var db = new TestDB(baseConfig);
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = new EventEmitter();
|
||||
db.node.modules.bitcoind.syncPercentage = sinon.spy();
|
||||
db.node.modules.bitcoind.genesisBuffer = genesisBuffer;
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = new EventEmitter();
|
||||
db.node.services.bitcoind.syncPercentage = sinon.spy();
|
||||
db.node.services.bitcoind.genesisBuffer = genesisBuffer;
|
||||
db.getMetadata = sinon.stub().callsArg(0);
|
||||
db.connectBlock = sinon.stub().callsArg(1);
|
||||
db.saveMetadata = sinon.stub();
|
||||
db.sync = sinon.stub();
|
||||
db.start(function() {
|
||||
db.sync = function() {
|
||||
db.node.modules.bitcoind.syncPercentage.callCount.should.equal(1);
|
||||
db.node.services.bitcoind.syncPercentage.callCount.should.equal(1);
|
||||
done();
|
||||
};
|
||||
db.node.modules.bitcoind.emit('tip', 10);
|
||||
db.node.services.bitcoind.emit('tip', 10);
|
||||
});
|
||||
});
|
||||
|
||||
it('will not call sync when there is a new tip and shutting down', function(done) {
|
||||
var db = new TestDB(baseConfig);
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = new EventEmitter();
|
||||
db.node.modules.bitcoind.syncPercentage = sinon.spy();
|
||||
db.node.modules.bitcoind.genesisBuffer = genesisBuffer;
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = new EventEmitter();
|
||||
db.node.services.bitcoind.syncPercentage = sinon.spy();
|
||||
db.node.services.bitcoind.genesisBuffer = genesisBuffer;
|
||||
db.getMetadata = sinon.stub().callsArg(0);
|
||||
db.connectBlock = sinon.stub().callsArg(1);
|
||||
db.saveMetadata = sinon.stub();
|
||||
|
@ -266,11 +265,11 @@ describe('DB Module', function() {
|
|||
db.sync = sinon.stub();
|
||||
db.start(function() {
|
||||
db.sync.callCount.should.equal(1);
|
||||
db.node.modules.bitcoind.once('tip', function() {
|
||||
db.node.services.bitcoind.once('tip', function() {
|
||||
db.sync.callCount.should.equal(1);
|
||||
done();
|
||||
});
|
||||
db.node.modules.bitcoind.emit('tip', 10);
|
||||
db.node.services.bitcoind.emit('tip', 10);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -291,8 +290,8 @@ describe('DB Module', function() {
|
|||
it('will return a NotFound error', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getTransaction: sinon.stub().callsArgWith(2, null, null)
|
||||
};
|
||||
var txid = '7426c707d0e9705bdd8158e60983e37d0f5d63529086d6672b07d9238d5aa623';
|
||||
|
@ -304,8 +303,8 @@ describe('DB Module', function() {
|
|||
it('will return an error from bitcoind', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getTransaction: sinon.stub().callsArgWith(2, new Error('test error'))
|
||||
};
|
||||
var txid = '7426c707d0e9705bdd8158e60983e37d0f5d63529086d6672b07d9238d5aa623';
|
||||
|
@ -317,8 +316,8 @@ describe('DB Module', function() {
|
|||
it('will return an error from bitcoind', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getTransaction: sinon.stub().callsArgWith(2, null, new Buffer(transactionData[0].hex, 'hex'))
|
||||
};
|
||||
var txid = '7426c707d0e9705bdd8158e60983e37d0f5d63529086d6672b07d9238d5aa623';
|
||||
|
@ -337,8 +336,8 @@ describe('DB Module', function() {
|
|||
var blockBuffer = new Buffer(blockData, 'hex');
|
||||
var expectedBlock = Block.fromBuffer(blockBuffer);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getBlock: sinon.stub().callsArgWith(1, null, blockBuffer)
|
||||
};
|
||||
|
||||
|
@ -351,9 +350,9 @@ describe('DB Module', function() {
|
|||
});
|
||||
it('should give an error when bitcoind.js gives an error', function(done) {
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {};
|
||||
db.node.modules.bitcoind.getBlock = sinon.stub().callsArgWith(1, new Error('error'));
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {};
|
||||
db.node.services.bitcoind.getBlock = sinon.stub().callsArgWith(1, new Error('error'));
|
||||
db.getBlock('00000000000000000593b60d8b4f40fd1ec080bdb0817d475dae47b5f5b1f735', function(err, block) {
|
||||
should.exist(err);
|
||||
err.message.should.equal('error');
|
||||
|
@ -366,8 +365,8 @@ describe('DB Module', function() {
|
|||
it('should return prevHash from bitcoind', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getBlockIndex: sinon.stub().returns({
|
||||
prevHash: 'prevhash'
|
||||
})
|
||||
|
@ -383,8 +382,8 @@ describe('DB Module', function() {
|
|||
it('should give an error if bitcoind could not find it', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getBlockIndex: sinon.stub().returns(null)
|
||||
};
|
||||
|
||||
|
@ -406,8 +405,8 @@ describe('DB Module', function() {
|
|||
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getTransactionWithBlockInfo: sinon.stub().callsArgWith(2, null, info)
|
||||
};
|
||||
|
||||
|
@ -421,8 +420,8 @@ describe('DB Module', function() {
|
|||
it('should give an error if one occurred', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getTransactionWithBlockInfo: sinon.stub().callsArgWith(2, new Error('error'))
|
||||
};
|
||||
|
||||
|
@ -437,8 +436,8 @@ describe('DB Module', function() {
|
|||
it('should give the txid on success', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
sendTransaction: sinon.stub().returns('txid')
|
||||
};
|
||||
|
||||
|
@ -452,8 +451,8 @@ describe('DB Module', function() {
|
|||
it('should give an error if bitcoind threw an error', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
sendTransaction: sinon.stub().throws(new Error('error'))
|
||||
};
|
||||
|
||||
|
@ -469,15 +468,15 @@ describe('DB Module', function() {
|
|||
it('should pass along the fee from bitcoind', function(done) {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
estimateFee: sinon.stub().returns(1000)
|
||||
};
|
||||
|
||||
db.estimateFee(5, function(err, fee) {
|
||||
should.not.exist(err);
|
||||
fee.should.equal(1000);
|
||||
db.node.modules.bitcoind.estimateFee.args[0][0].should.equal(5);
|
||||
db.node.services.bitcoind.estimateFee.args[0][0].should.equal(5);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -512,20 +511,20 @@ describe('DB Module', function() {
|
|||
|
||||
describe('#runAllBlockHandlers', function() {
|
||||
var db = new DB(baseConfig);
|
||||
var Module1 = function() {};
|
||||
Module1.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op1', 'op2', 'op3']);
|
||||
var Module2 = function() {};
|
||||
Module2.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op4', 'op5']);
|
||||
var Service1 = function() {};
|
||||
Service1.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op1', 'op2', 'op3']);
|
||||
var Service2 = function() {};
|
||||
Service2.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op4', 'op5']);
|
||||
db.node = {};
|
||||
db.node.modules = {
|
||||
module1: new Module1(),
|
||||
module2: new Module2()
|
||||
db.node.services = {
|
||||
service1: new Service1(),
|
||||
service2: new Service2()
|
||||
};
|
||||
db.store = {
|
||||
batch: sinon.stub().callsArg(1)
|
||||
};
|
||||
|
||||
it('should call blockHandler in all modules and perform operations', function(done) {
|
||||
it('should call blockHandler in all services and perform operations', function(done) {
|
||||
db.runAllBlockHandlers('block', true, function(err) {
|
||||
should.not.exist(err);
|
||||
db.store.batch.args[0][0].should.deep.equal(['op1', 'op2', 'op3', 'op4', 'op5']);
|
||||
|
@ -533,10 +532,10 @@ describe('DB Module', function() {
|
|||
});
|
||||
});
|
||||
|
||||
it('should give an error if one of the modules gives an error', function(done) {
|
||||
var Module3 = function() {};
|
||||
Module3.prototype.blockHandler = sinon.stub().callsArgWith(2, new Error('error'));
|
||||
db.node.modules.module3 = new Module3();
|
||||
it('should give an error if one of the services gives an error', function(done) {
|
||||
var Service3 = function() {};
|
||||
Service3.prototype.blockHandler = sinon.stub().callsArgWith(2, new Error('error'));
|
||||
db.node.services.service3 = new Service3();
|
||||
|
||||
db.runAllBlockHandlers('block', true, function(err) {
|
||||
should.exist(err);
|
||||
|
@ -549,7 +548,7 @@ describe('DB Module', function() {
|
|||
it('should return the correct db methods', function() {
|
||||
var db = new DB(baseConfig);
|
||||
db.node = {};
|
||||
db.node.modules = {};
|
||||
db.node.services = {};
|
||||
var methods = db.getAPIMethods();
|
||||
methods.length.should.equal(5);
|
||||
});
|
||||
|
@ -631,8 +630,8 @@ describe('DB Module', function() {
|
|||
}
|
||||
},
|
||||
};
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getBlockIndex: function(hash) {
|
||||
var block = forkedBlocks[hash];
|
||||
return {
|
||||
|
@ -678,7 +677,7 @@ describe('DB Module', function() {
|
|||
}
|
||||
});
|
||||
};
|
||||
db.node.modules = {};
|
||||
db.node.services = {};
|
||||
db.disconnectBlock = function(block, callback) {
|
||||
setImmediate(callback);
|
||||
};
|
||||
|
@ -710,8 +709,8 @@ describe('DB Module', function() {
|
|||
var db = new DB(syncConfig);
|
||||
var blockBuffer = new Buffer(blockData, 'hex');
|
||||
var block = Block.fromBuffer(blockBuffer);
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getBlock: sinon.stub().callsArgWith(1, null, blockBuffer),
|
||||
isSynced: sinon.stub().returns(true),
|
||||
height: 1
|
||||
|
@ -737,8 +736,8 @@ describe('DB Module', function() {
|
|||
});
|
||||
it('will exit and emit error with error from bitcoind.getBlock', function(done) {
|
||||
var db = new DB(syncConfig);
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getBlock: sinon.stub().callsArgWith(1, new Error('test error')),
|
||||
height: 1
|
||||
};
|
||||
|
@ -755,8 +754,8 @@ describe('DB Module', function() {
|
|||
var db = new DB(syncConfig);
|
||||
var blockBuffer = new Buffer(blockData, 'hex');
|
||||
var block = Block.fromBuffer(blockBuffer);
|
||||
db.node.modules = {};
|
||||
db.node.modules.bitcoind = {
|
||||
db.node.services = {};
|
||||
db.node.services.bitcoind = {
|
||||
getBlock: sinon.stub().callsArgWith(1, null, blockBuffer),
|
||||
isSynced: sinon.stub().returns(true),
|
||||
height: 1
|
Loading…
Reference in New Issue