-
Notifications
You must be signed in to change notification settings - Fork 648
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from braydonf/cli
CLI and Module Architecture
- Loading branch information
Showing
16 changed files
with
1,077 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,175 +1,6 @@ | ||
'use strict'; | ||
|
||
var BitcoinNode = require('..').Node; | ||
var chainlib = require('chainlib'); | ||
var socketio = require('socket.io'); | ||
var log = chainlib.log; | ||
log.debug = function() {}; | ||
var start = require('../lib/scaffold/start'); | ||
var defaultConfig = require('../lib/scaffold/default-config'); | ||
|
||
var configuration = { | ||
datadir: process.env.BITCORENODE_DIR || '~/.bitcoin', | ||
network: process.env.BITCORENODE_NETWORK || 'livenet', | ||
port: 3000 | ||
}; | ||
|
||
var node = new BitcoinNode(configuration); | ||
|
||
var count = 0; | ||
var interval = false; | ||
|
||
function logSyncStatus() { | ||
log.info('Sync Status: Tip:', node.chain.tip.hash, 'Height:', node.chain.tip.__height, 'Rate:', count/10, 'blocks per second'); | ||
} | ||
|
||
node.on('synced', function() { | ||
// Stop logging of sync status | ||
clearInterval(interval); | ||
interval = false; | ||
logSyncStatus(); | ||
}); | ||
|
||
node.on('ready', function() { | ||
|
||
var io = socketio(configuration.port); | ||
|
||
io.on('connection', function(socket) { | ||
|
||
var bus = node.openBus(); | ||
|
||
var methods = node.getAllAPIMethods(); | ||
var methodsMap = {}; | ||
|
||
methods.forEach(function(data) { | ||
var name = data[0]; | ||
var instance = data[1]; | ||
var method = data[2]; | ||
var args = data[3]; | ||
methodsMap[name] = { | ||
fn: function() { | ||
return method.apply(instance, arguments); | ||
}, | ||
args: args | ||
}; | ||
}); | ||
|
||
socket.on('message', function(message, socketCallback) { | ||
if (methodsMap[message.method]) { | ||
var params = message.params; | ||
|
||
if(!params || !params.length) { | ||
params = []; | ||
} | ||
|
||
if(params.length !== methodsMap[message.method].args) { | ||
return socketCallback({ | ||
error: { | ||
message: 'Expected ' + methodsMap[message.method].args + ' parameters' | ||
} | ||
}); | ||
} | ||
|
||
var callback = function(err, result) { | ||
var response = {}; | ||
if(err) { | ||
response.error = { | ||
message: err.toString() | ||
}; | ||
} | ||
|
||
if(result) { | ||
response.result = result; | ||
} | ||
|
||
socketCallback(response); | ||
}; | ||
|
||
params = params.concat(callback); | ||
methodsMap[message.method].fn.apply(this, params); | ||
} else { | ||
socketCallback({ | ||
error: { | ||
message: 'Method Not Found' | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
socket.on('subscribe', function(name, params) { | ||
bus.subscribe(name, params); | ||
}); | ||
|
||
socket.on('unsubscribe', function(name, params) { | ||
bus.unsubscribe(name, params); | ||
}); | ||
|
||
var events = node.getAllPublishEvents(); | ||
|
||
events.forEach(function(event) { | ||
bus.on(event.name, function() { | ||
if(socket.connected) { | ||
var results = []; | ||
|
||
for(var i = 0; i < arguments.length; i++) { | ||
results.push(arguments[i]); | ||
} | ||
|
||
var params = [event.name].concat(results); | ||
socket.emit.apply(socket, params); | ||
} | ||
}); | ||
}); | ||
|
||
socket.on('disconnect', function() { | ||
bus.close(); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
node.on('error', function(err) { | ||
log.error(err); | ||
}); | ||
|
||
node.chain.on('addblock', function(block) { | ||
count++; | ||
// Initialize logging if not already instantiated | ||
if (!interval) { | ||
interval = setInterval(function() { | ||
logSyncStatus(); | ||
count = 0; | ||
}, 10000); | ||
} | ||
}); | ||
|
||
node.on('stopping', function() { | ||
clearInterval(interval); | ||
}); | ||
|
||
function exitHandler(options, err) { | ||
if (err) { | ||
log.error('uncaught exception:', err); | ||
if(err.stack) { | ||
console.log(err.stack); | ||
} | ||
process.exit(-1); | ||
} | ||
if (options.sigint) { | ||
node.stop(function(err) { | ||
if(err) { | ||
log.error('Failed to stop services: ' + err); | ||
return process.exit(1); | ||
} | ||
|
||
log.info('Halted'); | ||
process.exit(0); | ||
}); | ||
} | ||
} | ||
|
||
//catches uncaught exceptions | ||
|
||
|
||
process.on('uncaughtException', exitHandler.bind(null, {exit:true})); | ||
//catches ctrl+c event | ||
process.on('SIGINT', exitHandler.bind(null, {sigint:true})); | ||
start(defaultConfig()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
var program = require('commander'); | ||
var version = require(__dirname + '/../package.json').version; | ||
var bitcore = require('bitcore'); | ||
var $ = bitcore.util.preconditions; | ||
var path = require('path'); | ||
var create = require('../lib/scaffold/create'); | ||
var add = require('../lib/scaffold/add'); | ||
var start = require('../lib/scaffold/start'); | ||
var findConfig = require('../lib/scaffold/find-config'); | ||
var defaultConfig = require('../lib/scaffold/default-config'); | ||
|
||
program | ||
.version(version); | ||
|
||
program | ||
.command('create <directory> [name]') | ||
.description('Create a new node') | ||
.option('-d, --datadir <dir>', 'Specify the bitcoin database directory') | ||
.action(function(dirname, name, cmd){ | ||
if (cmd.datadir) { | ||
cmd.datadir = path.resolve(process.cwd(), cmd.datadir); | ||
} | ||
var opts = { | ||
cwd: process.cwd(), | ||
dirname: dirname, | ||
name: name, | ||
datadir: cmd.datadir || './data', | ||
isGlobal: false | ||
}; | ||
create(opts, function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('Successfully created node in directory: ', dirname); | ||
}); | ||
}); | ||
|
||
program | ||
.command('start') | ||
.description('Start the current node') | ||
.option('-c, --config <dir>', 'Specify the directory with Bitcore Node configuration') | ||
.action(function(cmd){ | ||
if (cmd.config) { | ||
cmd.config = path.resolve(process.cwd(), cmd.config); | ||
} | ||
var configInfo = findConfig(cmd.config || process.cwd()); | ||
if (!configInfo) { | ||
configInfo = defaultConfig(); | ||
} | ||
start(configInfo); | ||
}); | ||
|
||
program | ||
.command('add <modules...>') | ||
.alias('install') | ||
.description('Install a module for the current node') | ||
.action(function(modules){ | ||
var configInfo = findConfig(process.cwd()); | ||
if (!configInfo) { | ||
throw new Error('Could not find configuration, see `bitcore-node create --help`'); | ||
} | ||
var opts = { | ||
path: configInfo.path, | ||
modules: modules | ||
}; | ||
add(opts, function() { | ||
console.log('Successfully added modules: ', modules.join(', ')); | ||
}); | ||
}).on('--help', function() { | ||
console.log(' Examples:'); | ||
console.log(); | ||
console.log(' $ bitcore-node add wallet-service'); | ||
console.log(' $ bitcore-node add insight-api'); | ||
console.log(); | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
if (process.argv.length === 2) { | ||
program.help(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.