bitcoind: load network bitcoin.conf and set defaults
This commit is contained in:
parent
c3dab07b30
commit
019bc2a58c
@ -185,6 +185,26 @@ Bitcoin.prototype._getDefaultConfig = function() {
|
|||||||
return config;
|
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) {
|
Bitcoin.prototype._loadSpawnConfiguration = function(node) {
|
||||||
/* jshint maxstatements: 25 */
|
/* jshint maxstatements: 25 */
|
||||||
|
|
||||||
@ -210,20 +230,12 @@ Bitcoin.prototype._loadSpawnConfiguration = function(node) {
|
|||||||
fs.writeFileSync(configPath, defaultConfig);
|
fs.writeFileSync(configPath, defaultConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
var file = fs.readFileSync(configPath);
|
_.extend(this.spawn.config, this._getDefaultConf());
|
||||||
var unparsed = file.toString().split('\n');
|
_.extend(this.spawn.config, this._parseBitcoinConf(configPath));
|
||||||
for(var i = 0; i < unparsed.length; i++) {
|
|
||||||
var line = unparsed[i];
|
var networkConfigPath = this._getNetworkConfigPath();
|
||||||
if (!line.match(/^\#/) && line.match(/\=/)) {
|
if (networkConfigPath && fs.existsSync(networkConfigPath)) {
|
||||||
var option = line.split('=');
|
_.extend(this.spawn.config, this._parseBitcoinConf(networkConfigPath));
|
||||||
var value;
|
|
||||||
if (!Number.isNaN(Number(option[1]))) {
|
|
||||||
value = Number(option[1]);
|
|
||||||
} else {
|
|
||||||
value = option[1];
|
|
||||||
}
|
|
||||||
this.spawn.config[option[0]] = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var spawnConfig = this.spawn.config;
|
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() {
|
Bitcoin.prototype._getNetworkOption = function() {
|
||||||
var networkOption;
|
var networkOption;
|
||||||
if (this.node.network === bitcore.Networks.testnet) {
|
if (this.node.network === bitcore.Networks.testnet) {
|
||||||
|
|||||||
@ -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() {
|
describe('#_getNetworkOption', function() {
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
bitcore.Networks.disableRegtest();
|
bitcore.Networks.disableRegtest();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user