From e2ad9320ff2092ae8ab43c574dd3b4b9b74ebf49 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 15 Oct 2014 16:38:10 -0700 Subject: [PATCH] improve start_node and example. --- example/index.js | 27 +++++++++++++++++++++++---- lib/bitcoind.js | 12 ++++++++++-- src/bitcoindjs.cc | 29 +++++++++++++++++------------ 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/example/index.js b/example/index.js index 10e46e9f..40f4326f 100755 --- a/example/index.js +++ b/example/index.js @@ -27,6 +27,20 @@ bitcoind.on('error', function(err) { bitcoind.on('open', function(status) { print('status="%s"', status); + if (!argv.list + || !argv.blocks + || !argv['on-block'] + || !argv['on-tx'] + || !argv.broadcast + || !argv['get-tx']) { + argv['list'] = true; + argv['on-block'] = true; + setTimeout(function() { + argv['on-block'] = false; + print(bitcoind.wallet.listAccounts()); + }, 5000); + } + if (argv.list) { print(bitcoind.wallet.listAccounts()); } @@ -35,9 +49,11 @@ bitcoind.on('open', function(status) { getBlocks(bitcoind); } - // var tx = bitcoind.tx.fromHex(testTx); - // console.log(tx); - // console.log(tx.txid === tx.getHash('hex')); + if (argv['test-tx']) { + var tx = bitcoind.tx.fromHex(testTx); + console.log(tx); + console.log(tx.txid === tx.getHash('hex')); + } function compareObj(obj) { // Hash @@ -64,7 +80,10 @@ bitcoind.on('open', function(status) { } if (argv['on-block']) { - bitcoind.on('block', function(block) { + bitcoind.on('block', function callee(block) { + if (!argv['on-block']) { + return bitcoind.removeListener('block', callee); + } print('Found Block:'); print(block); compareObj(block); diff --git a/lib/bitcoind.js b/lib/bitcoind.js index d0a19bde..17b74d9f 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -60,9 +60,17 @@ process.on = function(name, listener) { return Bitcoin._processOn.apply(this, arguments); }; -Bitcoin.prototype.start = function(callback) { +Bitcoin.prototype.start = function(options, callback) { var self = this; + if (!callback) { + callback = options; + options = {}; + } + if (!callback) { + callback = utils.NOOP; + } + if (this._startCalled) return; this._startCalled = true; @@ -74,7 +82,7 @@ Bitcoin.prototype.start = function(callback) { var exitCaught = none; var errorCaught = none; - this.log_pipe = bitcoindjs.start(function(err, status) { + this.log_pipe = bitcoindjs.start(options, function(err, status) { self._started = true; [sigint, sighup, sigquit].forEach(function(signal) { diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index ae17e6a3..338712d5 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -427,19 +427,24 @@ NAN_METHOD(StartBitcoind) { Local callback; std::string datadir = std::string(""); - if (args.Length() == 2 && args[0]->IsObject() && args[1]->IsFunction()) { - Local options = Local::Cast(args[0]); - String::Utf8Value datadir_(options->Get(NanNew("datadir"))->ToString()); - datadir = std::string(*datadir_); - callback = Local::Cast(args[1]); - } else { - if (args.Length() < 1 || !args[0]->IsFunction()) { - return NanThrowError( - "Usage: bitcoind.start(callback)"); - } - callback = Local::Cast(args[0]); - } + if (args.Length() >= 2 && args[0]->IsObject() && args[1]->IsFunction()) { + Local options = Local::Cast(args[0]); + if (options->Get(NanNew("datadir"))->IsString()) { + String::Utf8Value datadir_(options->Get(NanNew("datadir"))->ToString()); + datadir = std::string(*datadir_); + } + callback = Local::Cast(args[1]); + } else if (args.length >= 2 + && (args[0]->IsUndefined() || args[0]->IsNull()) + && args[1]->IsFunction()) { + callback = Local::Cast(args[1]); + } else if (args.length >= 1 && args[0]->IsFunction()) { + callback = Local::Cast(args[0]); + } else { + return NanThrowError( + "Usage: bitcoind.start(callback)"); + } // // Run bitcoind's StartNode() on a separate thread.