From 814576953c45be7d671b0312442bad3a961a2d62 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Jun 2016 11:33:06 -0400 Subject: [PATCH 1/2] bitcoind: relative spawn.datadir handling Will expand the datadir into an absolute path based on the location of the configuration file. This is to avoid unexpected behavior in regards to the location of configuration files. --- lib/node.js | 1 + lib/scaffold/start.js | 1 - lib/services/bitcoind.js | 13 ++++++++++- test/services/bitcoind.unit.js | 42 ++++++++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/lib/node.js b/lib/node.js index c2350b50..1c97c361 100644 --- a/lib/node.js +++ b/lib/node.js @@ -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()); } }; diff --git a/lib/scaffold/start.js b/lib/scaffold/start.js index 378e96c3..e8654575 100644 --- a/lib/scaffold/start.js +++ b/lib/scaffold/start.js @@ -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 */ diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 4e79fcd6..7e0e04f9 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -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'); /** @@ -318,8 +320,17 @@ 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'); + 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); + } + 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; diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 8719f7f5..f486113a 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -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); }); }); From 4d780a9d2dafddbe6dfdfdfe12512d22c65c9457 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Jun 2016 11:41:41 -0400 Subject: [PATCH 2/2] bitcoind: separate function for relative datadir expanding --- lib/services/bitcoind.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 7e0e04f9..f1e21464 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -313,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 */ @@ -320,12 +329,7 @@ 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'); - 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); - } + this._expandRelativeDatadir(); var spawnOptions = this.options.spawn; var configPath = path.resolve(spawnOptions.datadir, './bitcoin.conf');