Merge pull request #437 from braydonf/relative-datadir

bitcoind: relative spawn.datadir handling
This commit is contained in:
Chris Kleeschulte 2016-06-02 14:10:58 -04:00
commit 3a0ba64a43
4 changed files with 57 additions and 4 deletions

View File

@ -228,6 +228,7 @@ Node.prototype._startService = function(serviceInfo, callback) {
Node.prototype._logTitle = function() {
if (this.configPath) {
log.info('Using config:', this.configPath);
log.info('Using network:', this.getNetworkName());
}
};

View File

@ -61,7 +61,6 @@ function checkConfigVersion2(fullConfig) {
* @param {Object} options.config - The parsed bitcore-node.json configuration file
* @param {Array} options.config.services - An array of services names.
* @param {Object} options.config.servicesConfig - Parameters to pass to each service
* @param {String} options.config.datadir - A relative (to options.path) or absolute path to the datadir
* @param {String} options.config.network - 'livenet', 'testnet' or 'regtest
* @param {Number} options.config.port - The port to use for the web service
*/

View File

@ -1,6 +1,7 @@
'use strict';
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var util = require('util');
var mkdirp = require('mkdirp');
@ -16,6 +17,7 @@ var Transaction = bitcore.Transaction;
var index = require('../');
var errors = index.errors;
var log = index.log;
var utils = require('../utils');
var Service = require('../service');
/**
@ -311,6 +313,15 @@ Bitcoin.prototype._parseBitcoinConf = function(configPath) {
return options;
};
Bitcoin.prototype._expandRelativeDatadir = function() {
if (!utils.isAbsolutePath(this.options.spawn.datadir)) {
$.checkState(this.node.configPath);
$.checkState(utils.isAbsolutePath(this.node.configPath));
var baseConfigPath = path.dirname(this.node.configPath);
this.options.spawn.datadir = path.resolve(baseConfigPath, this.options.spawn.datadir);
}
};
Bitcoin.prototype._loadSpawnConfiguration = function(node) {
/* jshint maxstatements: 25 */
@ -318,8 +329,12 @@ Bitcoin.prototype._loadSpawnConfiguration = function(node) {
$.checkArgument(this.options.spawn.datadir, 'Please specify "spawn.datadir" in bitcoind config options');
$.checkArgument(this.options.spawn.exec, 'Please specify "spawn.exec" in bitcoind config options');
this._expandRelativeDatadir();
var spawnOptions = this.options.spawn;
var configPath = spawnOptions.datadir + '/bitcoin.conf';
var configPath = path.resolve(spawnOptions.datadir, './bitcoin.conf');
log.info('Using bitcoin config file:', configPath);
this.spawn = {};
this.spawn.datadir = this.options.spawn.datadir;

View File

@ -340,6 +340,13 @@ describe('Bitcoin Service', function() {
});
describe('#_loadSpawnConfiguration', function() {
var sandbox = sinon.sandbox.create();
beforeEach(function() {
sandbox.stub(log, 'info');
});
afterEach(function() {
sandbox.restore();
});
it('will parse a bitcoin.conf file', function() {
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
fs: {
@ -352,7 +359,9 @@ describe('Bitcoin Service', function() {
}
});
var bitcoind = new TestBitcoin(baseConfig);
bitcoind._loadSpawnConfiguration({datadir: process.env.HOME + '/.bitcoin'});
bitcoind.options.spawn.datadir = '/tmp/.bitcoin';
var node = {};
bitcoind._loadSpawnConfiguration(node);
should.exist(bitcoind.spawn.config);
bitcoind.spawn.config.should.deep.equal({
addressindex: 1,
@ -374,6 +383,33 @@ describe('Bitcoin Service', function() {
zmqpubrawtx: 'tcp://127.0.0.1:28332'
});
});
it('will expand relative datadir to absolute path', function() {
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
fs: {
readFileSync: readFileSync,
existsSync: sinon.stub().returns(true),
writeFileSync: sinon.stub()
},
mkdirp: {
sync: sinon.stub()
}
});
var config = {
node: {
network: bitcore.Networks.testnet,
configPath: '/tmp/.bitcore/bitcore-node.json'
},
spawn: {
datadir: './data',
exec: 'testpath'
}
};
var bitcoind = new TestBitcoin(config);
bitcoind.options.spawn.datadir = './data';
var node = {};
bitcoind._loadSpawnConfiguration(node);
bitcoind.options.spawn.datadir.should.equal('/tmp/.bitcore/data');
});
it('should throw an exception if txindex isn\'t enabled in the configuration', function() {
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
fs: {
@ -420,7 +456,9 @@ describe('Bitcoin Service', function() {
}
};
var bitcoind = new TestBitcoin(config);
bitcoind._loadSpawnConfiguration({datadir: process.env.HOME + '/.bitcoin'});
bitcoind.options.spawn.datadir = '/tmp/.bitcoin';
var node = {};
bitcoind._loadSpawnConfiguration(node);
});
});