Skip to content

Commit 3a0ba64

Browse files
committed
Merge pull request #437 from braydonf/relative-datadir
bitcoind: relative spawn.datadir handling
2 parents 7efe271 + 4d780a9 commit 3a0ba64

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

lib/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Node.prototype._startService = function(serviceInfo, callback) {
228228
Node.prototype._logTitle = function() {
229229
if (this.configPath) {
230230
log.info('Using config:', this.configPath);
231+
log.info('Using network:', this.getNetworkName());
231232
}
232233
};
233234

lib/scaffold/start.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ function checkConfigVersion2(fullConfig) {
6161
* @param {Object} options.config - The parsed bitcore-node.json configuration file
6262
* @param {Array} options.config.services - An array of services names.
6363
* @param {Object} options.config.servicesConfig - Parameters to pass to each service
64-
* @param {String} options.config.datadir - A relative (to options.path) or absolute path to the datadir
6564
* @param {String} options.config.network - 'livenet', 'testnet' or 'regtest
6665
* @param {Number} options.config.port - The port to use for the web service
6766
*/

lib/services/bitcoind.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var fs = require('fs');
4+
var path = require('path');
45
var spawn = require('child_process').spawn;
56
var util = require('util');
67
var mkdirp = require('mkdirp');
@@ -16,6 +17,7 @@ var Transaction = bitcore.Transaction;
1617
var index = require('../');
1718
var errors = index.errors;
1819
var log = index.log;
20+
var utils = require('../utils');
1921
var Service = require('../service');
2022

2123
/**
@@ -311,15 +313,28 @@ Bitcoin.prototype._parseBitcoinConf = function(configPath) {
311313
return options;
312314
};
313315

316+
Bitcoin.prototype._expandRelativeDatadir = function() {
317+
if (!utils.isAbsolutePath(this.options.spawn.datadir)) {
318+
$.checkState(this.node.configPath);
319+
$.checkState(utils.isAbsolutePath(this.node.configPath));
320+
var baseConfigPath = path.dirname(this.node.configPath);
321+
this.options.spawn.datadir = path.resolve(baseConfigPath, this.options.spawn.datadir);
322+
}
323+
};
324+
314325
Bitcoin.prototype._loadSpawnConfiguration = function(node) {
315326
/* jshint maxstatements: 25 */
316327

317328
$.checkArgument(this.options.spawn, 'Please specify "spawn" in bitcoind config options');
318329
$.checkArgument(this.options.spawn.datadir, 'Please specify "spawn.datadir" in bitcoind config options');
319330
$.checkArgument(this.options.spawn.exec, 'Please specify "spawn.exec" in bitcoind config options');
320331

332+
this._expandRelativeDatadir();
333+
321334
var spawnOptions = this.options.spawn;
322-
var configPath = spawnOptions.datadir + '/bitcoin.conf';
335+
var configPath = path.resolve(spawnOptions.datadir, './bitcoin.conf');
336+
337+
log.info('Using bitcoin config file:', configPath);
323338

324339
this.spawn = {};
325340
this.spawn.datadir = this.options.spawn.datadir;

test/services/bitcoind.unit.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ describe('Bitcoin Service', function() {
340340
});
341341

342342
describe('#_loadSpawnConfiguration', function() {
343+
var sandbox = sinon.sandbox.create();
344+
beforeEach(function() {
345+
sandbox.stub(log, 'info');
346+
});
347+
afterEach(function() {
348+
sandbox.restore();
349+
});
343350
it('will parse a bitcoin.conf file', function() {
344351
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
345352
fs: {
@@ -352,7 +359,9 @@ describe('Bitcoin Service', function() {
352359
}
353360
});
354361
var bitcoind = new TestBitcoin(baseConfig);
355-
bitcoind._loadSpawnConfiguration({datadir: process.env.HOME + '/.bitcoin'});
362+
bitcoind.options.spawn.datadir = '/tmp/.bitcoin';
363+
var node = {};
364+
bitcoind._loadSpawnConfiguration(node);
356365
should.exist(bitcoind.spawn.config);
357366
bitcoind.spawn.config.should.deep.equal({
358367
addressindex: 1,
@@ -374,6 +383,33 @@ describe('Bitcoin Service', function() {
374383
zmqpubrawtx: 'tcp://127.0.0.1:28332'
375384
});
376385
});
386+
it('will expand relative datadir to absolute path', function() {
387+
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
388+
fs: {
389+
readFileSync: readFileSync,
390+
existsSync: sinon.stub().returns(true),
391+
writeFileSync: sinon.stub()
392+
},
393+
mkdirp: {
394+
sync: sinon.stub()
395+
}
396+
});
397+
var config = {
398+
node: {
399+
network: bitcore.Networks.testnet,
400+
configPath: '/tmp/.bitcore/bitcore-node.json'
401+
},
402+
spawn: {
403+
datadir: './data',
404+
exec: 'testpath'
405+
}
406+
};
407+
var bitcoind = new TestBitcoin(config);
408+
bitcoind.options.spawn.datadir = './data';
409+
var node = {};
410+
bitcoind._loadSpawnConfiguration(node);
411+
bitcoind.options.spawn.datadir.should.equal('/tmp/.bitcore/data');
412+
});
377413
it('should throw an exception if txindex isn\'t enabled in the configuration', function() {
378414
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
379415
fs: {
@@ -420,7 +456,9 @@ describe('Bitcoin Service', function() {
420456
}
421457
};
422458
var bitcoind = new TestBitcoin(config);
423-
bitcoind._loadSpawnConfiguration({datadir: process.env.HOME + '/.bitcoin'});
459+
bitcoind.options.spawn.datadir = '/tmp/.bitcoin';
460+
var node = {};
461+
bitcoind._loadSpawnConfiguration(node);
424462
});
425463
});
426464

0 commit comments

Comments
 (0)