From 019bc2a58cf03c5ee09393e28d30c89630bfd519 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 20 Apr 2016 11:41:02 -0400 Subject: [PATCH] bitcoind: load network bitcoin.conf and set defaults --- lib/services/bitcoind.js | 61 +++++++++++++++++----- test/services/bitcoind.unit.js | 94 ++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 14 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 9d59f48d..d94e49ea 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -185,6 +185,26 @@ Bitcoin.prototype._getDefaultConfig = function() { return config; }; +Bitcoin.prototype._parseBitcoinConf = function(configPath) { + var options = {}; + var file = fs.readFileSync(configPath); + var unparsed = file.toString().split('\n'); + for(var i = 0; i < unparsed.length; i++) { + var line = unparsed[i]; + if (!line.match(/^\#/) && line.match(/\=/)) { + var option = line.split('='); + var value; + if (!Number.isNaN(Number(option[1]))) { + value = Number(option[1]); + } else { + value = option[1]; + } + options[option[0]] = value; + } + } + return options; +}; + Bitcoin.prototype._loadSpawnConfiguration = function(node) { /* jshint maxstatements: 25 */ @@ -210,20 +230,12 @@ Bitcoin.prototype._loadSpawnConfiguration = function(node) { fs.writeFileSync(configPath, defaultConfig); } - var file = fs.readFileSync(configPath); - var unparsed = file.toString().split('\n'); - for(var i = 0; i < unparsed.length; i++) { - var line = unparsed[i]; - if (!line.match(/^\#/) && line.match(/\=/)) { - var option = line.split('='); - var value; - if (!Number.isNaN(Number(option[1]))) { - value = Number(option[1]); - } else { - value = option[1]; - } - this.spawn.config[option[0]] = value; - } + _.extend(this.spawn.config, this._getDefaultConf()); + _.extend(this.spawn.config, this._parseBitcoinConf(configPath)); + + var networkConfigPath = this._getNetworkConfigPath(); + if (networkConfigPath && fs.existsSync(networkConfigPath)) { + _.extend(this.spawn.config, this._parseBitcoinConf(networkConfigPath)); } var spawnConfig = this.spawn.config; @@ -334,6 +346,27 @@ Bitcoin.prototype._initChain = function(callback) { }); }; +Bitcoin.prototype._getDefaultConf = function() { + var networkOptions = { + rpcport: 8332 + }; + if (this.node.network === bitcore.Networks.testnet) { + networkOptions.rpcport = 18332; + } + return networkOptions; +}; + +Bitcoin.prototype._getNetworkConfigPath = function() { + var networkPath; + if (this.node.network === bitcore.Networks.testnet) { + networkPath = 'testnet3/bitcoin.conf'; + if (this.node.network.regtestEnabled) { + networkPath = 'regtest/bitcoin.conf'; + } + } + return networkPath; +}; + Bitcoin.prototype._getNetworkOption = function() { var networkOption; if (this.node.network === bitcore.Networks.testnet) { diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 62131046..7f9136cf 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -411,6 +411,100 @@ describe('Bitcoin Service', function() { }); }); + describe('#_getDefaultConf', function() { + afterEach(function() { + bitcore.Networks.disableRegtest(); + baseConfig.node.network = bitcore.Networks.testnet; + }); + it('will get default rpc port for livenet', function() { + var config = { + node: { + network: bitcore.Networks.livenet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + bitcoind._getDefaultConf().rpcport.should.equal(8332); + }); + it('will get default rpc port for testnet', function() { + var config = { + node: { + network: bitcore.Networks.testnet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + bitcoind._getDefaultConf().rpcport.should.equal(18332); + }); + it('will get default rpc port for regtest', function() { + bitcore.Networks.enableRegtest(); + var config = { + node: { + network: bitcore.Networks.testnet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + bitcoind._getDefaultConf().rpcport.should.equal(18332); + }); + }); + + describe('#_getNetworkConfigPath', function() { + afterEach(function() { + bitcore.Networks.disableRegtest(); + baseConfig.node.network = bitcore.Networks.testnet; + }); + it('will get default config path for livenet', function() { + var config = { + node: { + network: bitcore.Networks.livenet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + should.equal(bitcoind._getNetworkConfigPath(), undefined); + }); + it('will get default rpc port for testnet', function() { + var config = { + node: { + network: bitcore.Networks.testnet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + bitcoind._getNetworkConfigPath().should.equal('testnet3/bitcoin.conf'); + }); + it('will get default rpc port for regtest', function() { + bitcore.Networks.enableRegtest(); + var config = { + node: { + network: bitcore.Networks.testnet + }, + spawn: { + datadir: 'testdir', + exec: 'testpath' + } + }; + var bitcoind = new BitcoinService(config); + bitcoind._getNetworkConfigPath().should.equal('regtest/bitcoin.conf'); + }); + }); + describe('#_getNetworkOption', function() { afterEach(function() { bitcore.Networks.disableRegtest();