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']) {
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) {

View File

@ -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

View File

@ -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"

View File

@ -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 **));