bitcoind: load network bitcoin.conf and set defaults

This commit is contained in:
Braydon Fuller 2016-04-20 11:41:02 -04:00
parent c3dab07b30
commit 019bc2a58c
2 changed files with 141 additions and 14 deletions

View File

@ -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) {

View File

@ -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();