From a09c735a9b62d4d69a7600149709fda269e7bc07 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 16 Oct 2014 14:56:41 -0700 Subject: [PATCH] datadir fixes. wait for wallet to load. --- example/index.js | 4 +-- lib/bitcoind.js | 67 +++++++++++++++++++++++++++++++++-------------- platform/os.sh | 2 +- src/bitcoindjs.cc | 16 ++++++++--- 4 files changed, 62 insertions(+), 27 deletions(-) diff --git a/example/index.js b/example/index.js index 4f4ea9d1..4fb5c301 100755 --- a/example/index.js +++ b/example/index.js @@ -59,8 +59,8 @@ bitcoind.on('open', function(status) { if (argv['test-tx']) { var tx = bitcoind.tx.fromHex(testTx); - console.log(tx); - console.log(tx.txid === tx.getHash('hex')); + print(tx); + print(tx.txid === tx.getHash('hex')); } function compareObj(obj) { diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 37027650..152d219f 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -20,10 +20,6 @@ var bitcoin = Bitcoin; function Bitcoin(options) { var self = this; - if (Bitcoin.global) { - throw new Error('bitcoindjs cannot be instantiated more than once.'); - } - if (!(this instanceof Bitcoin)) { return new Bitcoin(options); } @@ -42,27 +38,35 @@ function Bitcoin(options) { delete this.options.directory; } + if (!this.options.datadir) { + this.options.datadir = process.env.HOME + '/.bitcoin'; + } + this.options.datadir = this.options.datadir.replace(/^~/, process.env.HOME); - this.config = this.datadir + '/bitcoin.conf'; + this.config = this.options.datadir + '/bitcoin.conf'; + + if (this.instances[this.options.datadir]) { + throw new Error('' + + 'bitcoind.js cannot be instantiated' + + ' more than once on the same datadir.'); + } if (!fs.existsSync(this.options.datadir)) { mkdirp.sync(this.options.datadir); } if (!fs.existsSync(this.config)) { - var password = Math.random().toString(36).slice(2) + var password = '' + + Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2); fs.writeFileSync(this.config, '' + 'rpcuser=bitcoinrpc\n' + 'rpcpassword=' + password + '\n' - + '\n' ); } - Bitcoin.global = this; - Object.keys(exports).forEach(function(key) { self[key] = exports[key]; }); @@ -90,6 +94,13 @@ process.on = function(name, listener) { return Bitcoin._processOn.apply(this, arguments); }; +Bitcoin.instances = {}; +Bitcoin.prototype.instances = Bitcoin.instances; + +Bitcoin.__defineGetter__('global', function() { + return Bitcoin.instances[process.env.HOME + '/.bitcoin']; +}); + Bitcoin.prototype.start = function(options, callback) { var self = this; @@ -106,8 +117,10 @@ Bitcoin.prototype.start = function(options, callback) { callback = utils.NOOP; } - if (this._startCalled) return; - this._startCalled = true; + if (this.instances[this.options.datadir]) { + return; + } + this.instances[this.options.datadir] = true; var none = {}; var isSignal = {}; @@ -175,16 +188,30 @@ Bitcoin.prototype.start = function(options, callback) { self.stop(); }); - if (callback) { - callback(err); - callback = null; - } + setTimeout(function callee() { + // Wait until wallet is loaded: + if (!Object.keys(self.wallet.listAccounts()).length) { + return setTimeout(callee, 100); + } - if (err) { - self.emit('error', err); - } else { - self.emit('open', status); - } + if (callback) { + callback(err ? err : null); + } + + if (err) { + self.emit('error', err); + } else { + if (callback) { + self.emit('open', status); + } else { + self.emit('status', status); + } + } + + if (callback) { + callback = null; + } + }, 100); }); // bitcoind's boost threads aren't in the thread pool diff --git a/platform/os.sh b/platform/os.sh index 14f5021b..bf9d3bb2 100755 --- a/platform/os.sh +++ b/platform/os.sh @@ -26,7 +26,7 @@ fi if test ! -e platform/${os}/libbitcoind.so; then cat platform/${os}/{xaa,xab} > platform/${os}/libbitcoind.so - chmod +x platform/${os}/libbitcoind.so + chmod 0755 platform/${os}/libbitcoind.so fi echo -n "$(pwd)/platform/${os}/libbitcoind.so" diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 0fd81cae..a33c1446 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -563,12 +563,20 @@ start_node_thread(void) { int argc = 0; char **argv = NULL; if (g_data_dir) { - argc = 3; + const int argl = 9 + strlen(g_data_dir) + 1; + char *arg = (char *)malloc(argl); + int w = snprintf(arg, argl, "-datadir=%s", g_data_dir); + if (w <= 0 || w >= argl) { + NanThrowError("Bad -datadir value."); + return; + } + arg[w] = '\0'; + + argc = 2; argv = (char **)malloc((argc + 1) * sizeof(char **)); argv[0] = (char *)"bitcoind"; - argv[1] = (char *)"-datadir"; - argv[2] = (char *)g_data_dir; - argv[3] = NULL; + argv[1] = arg; + argv[2] = NULL; } else { argc = 1; argv = (char **)malloc((argc + 1) * sizeof(char **));