datadir fixes. wait for wallet to load.

This commit is contained in:
Christopher Jeffrey 2014-10-16 14:56:41 -07:00
parent 90fd43e0f8
commit a09c735a9b
4 changed files with 62 additions and 27 deletions

View File

@ -59,8 +59,8 @@ bitcoind.on('open', function(status) {
if (argv['test-tx']) { if (argv['test-tx']) {
var tx = bitcoind.tx.fromHex(testTx); var tx = bitcoind.tx.fromHex(testTx);
console.log(tx); print(tx);
console.log(tx.txid === tx.getHash('hex')); print(tx.txid === tx.getHash('hex'));
} }
function compareObj(obj) { function compareObj(obj) {

View File

@ -20,10 +20,6 @@ var bitcoin = Bitcoin;
function Bitcoin(options) { function Bitcoin(options) {
var self = this; var self = this;
if (Bitcoin.global) {
throw new Error('bitcoindjs cannot be instantiated more than once.');
}
if (!(this instanceof Bitcoin)) { if (!(this instanceof Bitcoin)) {
return new Bitcoin(options); return new Bitcoin(options);
} }
@ -42,27 +38,35 @@ function Bitcoin(options) {
delete this.options.directory; 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.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)) { if (!fs.existsSync(this.options.datadir)) {
mkdirp.sync(this.options.datadir); mkdirp.sync(this.options.datadir);
} }
if (!fs.existsSync(this.config)) { 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)
+ Math.random().toString(36).slice(2); + Math.random().toString(36).slice(2);
fs.writeFileSync(this.config, '' fs.writeFileSync(this.config, ''
+ 'rpcuser=bitcoinrpc\n' + 'rpcuser=bitcoinrpc\n'
+ 'rpcpassword=' + password + '\n' + 'rpcpassword=' + password + '\n'
+ '\n'
); );
} }
Bitcoin.global = this;
Object.keys(exports).forEach(function(key) { Object.keys(exports).forEach(function(key) {
self[key] = exports[key]; self[key] = exports[key];
}); });
@ -90,6 +94,13 @@ process.on = function(name, listener) {
return Bitcoin._processOn.apply(this, arguments); 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) { Bitcoin.prototype.start = function(options, callback) {
var self = this; var self = this;
@ -106,8 +117,10 @@ Bitcoin.prototype.start = function(options, callback) {
callback = utils.NOOP; callback = utils.NOOP;
} }
if (this._startCalled) return; if (this.instances[this.options.datadir]) {
this._startCalled = true; return;
}
this.instances[this.options.datadir] = true;
var none = {}; var none = {};
var isSignal = {}; var isSignal = {};
@ -175,16 +188,30 @@ Bitcoin.prototype.start = function(options, callback) {
self.stop(); self.stop();
}); });
if (callback) { setTimeout(function callee() {
callback(err); // Wait until wallet is loaded:
callback = null; if (!Object.keys(self.wallet.listAccounts()).length) {
} return setTimeout(callee, 100);
}
if (err) { if (callback) {
self.emit('error', err); callback(err ? err : null);
} else { }
self.emit('open', status);
} 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 // bitcoind's boost threads aren't in the thread pool

View File

@ -26,7 +26,7 @@ fi
if test ! -e platform/${os}/libbitcoind.so; then if test ! -e platform/${os}/libbitcoind.so; then
cat platform/${os}/{xaa,xab} > platform/${os}/libbitcoind.so cat platform/${os}/{xaa,xab} > platform/${os}/libbitcoind.so
chmod +x platform/${os}/libbitcoind.so chmod 0755 platform/${os}/libbitcoind.so
fi fi
echo -n "$(pwd)/platform/${os}/libbitcoind.so" echo -n "$(pwd)/platform/${os}/libbitcoind.so"

View File

@ -563,12 +563,20 @@ start_node_thread(void) {
int argc = 0; int argc = 0;
char **argv = NULL; char **argv = NULL;
if (g_data_dir) { 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 = (char **)malloc((argc + 1) * sizeof(char **));
argv[0] = (char *)"bitcoind"; argv[0] = (char *)"bitcoind";
argv[1] = (char *)"-datadir"; argv[1] = arg;
argv[2] = (char *)g_data_dir; argv[2] = NULL;
argv[3] = NULL;
} else { } else {
argc = 1; argc = 1;
argv = (char **)malloc((argc + 1) * sizeof(char **)); argv = (char **)malloc((argc + 1) * sizeof(char **));