diff --git a/lib/scaffold/start.js b/lib/scaffold/start.js index f2ee95a8..e052ca11 100644 --- a/lib/scaffold/start.js +++ b/lib/scaffold/start.js @@ -10,6 +10,48 @@ var shuttingDown = false; log.debug = function() {}; +/** + * Checks for configuration options from version 2. This includes an "address" and + * "db" service, or having "datadir" at the root of the config. + */ +function checkConfigVersion2(fullConfig) { + var datadirUndefined = _.isUndefined(fullConfig.datadir); + var addressDefined = (fullConfig.services.indexOf('address') >= 0); + var dbDefined = (fullConfig.services.indexOf('db') >= 0); + + if (!datadirUndefined || addressDefined || dbDefined) { + + console.warn('\nConfiguration file is not compatible with this version. \n' + + 'A reindex for bitcoind is necessary for this upgrade with bitcoin.conf option "reindex=1". \n' + + 'There are changes necessary in both bitcoin.conf and bitcore-node.json.' + + 'To upgrade please see the details below and documentation at: \n' + + 'https://github.com/bitpay/bitcore-node/blob/bitcoind/docs/upgrade.md \n'); + + if (!datadirUndefined) { + console.warn('Please remove "datadir" and add it to the config at ' + fullConfig.path + ' with:'); + var missingConfig = { + servicesConfig: { + bitcoind: { + spawn: { + datadir: fullConfig.datadir, + exec: path.resolve(__dirname, '../../bin/bitcoind') + } + } + } + }; + console.warn(JSON.stringify(missingConfig, null, 2) + '\n'); + } + + if (addressDefined || dbDefined) { + console.warn('Please remove "address" and/or "db" from "services" in: ' + fullConfig.path + '\n'); + } + + return true; + } + + return false; +} + /** * This function will instantiate and start a Node, requiring the necessary service * modules, and registering event handlers. @@ -37,12 +79,8 @@ function start(options) { fullConfig.path = path.resolve(options.path, './bitcore-node.json'); - if (fullConfig.datadir) { - throw new TypeError( - 'Configuration file (' + fullConfig.path + ') is not compatible with this version.' + - ' Please see https://github.com/bitpay/bitcore-node/blob/bitcoind/docs/upgrade.md' + - ' for upgrade details.' - ); + if (checkConfigVersion2(fullConfig)) { + process.exit(1); } fullConfig.services = start.setupServices(require, servicesPath, options.config); @@ -215,3 +253,4 @@ module.exports.registerExitHandlers = registerExitHandlers; module.exports.exitHandler = exitHandler; module.exports.setupServices = setupServices; module.exports.cleanShutdown = cleanShutdown; +module.exports.checkConfigVersion2 = checkConfigVersion2; diff --git a/test/scaffold/start.unit.js b/test/scaffold/start.unit.js index 38d14feb..efc06299 100644 --- a/test/scaffold/start.unit.js +++ b/test/scaffold/start.unit.js @@ -8,6 +8,35 @@ var proxyquire = require('proxyquire'); var start = require('../../lib/scaffold/start'); describe('#start', function() { + describe('#checkConfigVersion2', function() { + var sandbox = sinon.sandbox.create(); + beforeEach(function() { + sandbox.stub(console, 'warn'); + }); + afterEach(function() { + sandbox.restore(); + }); + it('will give true with "datadir" at root', function() { + var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2; + var v2 = checkConfigVersion2({datadir: '/home/user/.bitcore/data', services: []}); + v2.should.equal(true); + }); + it('will give true with "address" service enabled', function() { + var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2; + var v2 = checkConfigVersion2({services: ['address']}); + v2.should.equal(true); + }); + it('will give true with "db" service enabled', function() { + var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2; + var v2 = checkConfigVersion2({services: ['db']}); + v2.should.equal(true); + }); + it('will give false without "datadir" at root and "address", "db" services disabled', function() { + var checkConfigVersion2 = proxyquire('../../lib/scaffold/start', {}).checkConfigVersion2; + var v2 = checkConfigVersion2({services: []}); + v2.should.equal(false); + }); + }); describe('#setupServices', function() { var cwd = process.cwd(); var setupServices = proxyquire('../../lib/scaffold/start', {}).setupServices;