diff --git a/lib/node.js b/lib/node.js index 6c94173b..07b19ca4 100644 --- a/lib/node.js +++ b/lib/node.js @@ -51,6 +51,7 @@ Node.prototype.setSyncStrategy = function(strategy) { }; Node.prototype._loadBitcoinConf = function(config) { + $.checkArgument(config.datadir, 'Please specify "datadir" in configuration options'); var datadir = config.datadir.replace(/^~/, process.env.HOME); this.bitcoinConfiguration = {}; var file = fs.readFileSync(datadir + '/bitcoin.conf'); @@ -66,7 +67,6 @@ Node.prototype._loadBitcoinConf = function(config) { Node.prototype._loadBitcoind = function(config) { var bitcoindConfig = {}; - $.checkArgument(config.datadir, 'Please specify "datadir" in configuration options'); bitcoindConfig.datadir = config.datadir; bitcoindConfig.testnet = config.testnet; @@ -134,11 +134,15 @@ Node.prototype._loadDB = function(config) { // Store the additional indexes in a new directory // based on the network configuration and the datadir + $.checkArgument(config.datadir, 'Please specify "datadir" in configuration options'); + $.checkState(this.network, 'Network property not defined'); var datadir = config.datadir.replace(/^~/, process.env.HOME); if (this.network === Networks.testnet) { config.db.path = datadir + '/testnet3/bitcoindjs.db'; } else if (this.network === Networks.livenet) { config.db.path = datadir + '/bitcoindjs.db'; + } else { + throw new Error('Unknown network: ' + this.network); } config.db.network = this.network; @@ -154,7 +158,7 @@ Node.prototype._loadP2P = function(config) { // We only want to directly connect via p2p to the trusted bitcoind daemon var port = 8333; - if (this.bitcoinConfiguration.port) { + if (this.bitcoinConfiguration && this.bitcoinConfiguration.port) { port = this.bitcoinConfiguration.port; } else if (this.network === Networks.testnet) { port = 18333; diff --git a/test/node.unit.js b/test/node.unit.js index 31345b17..4ad34c89 100644 --- a/test/node.unit.js +++ b/test/node.unit.js @@ -26,9 +26,11 @@ describe('Bitcoind Node', function() { describe('#_loadConfiguration', function() { it('should call the necessary methods', function() { var node = new Node({}); + node._loadBitcoinConf = sinon.spy(); node._loadBitcoind = sinon.spy(); node._loadConfiguration({}); node._loadBitcoind.called.should.equal(true); + node._loadBitcoinConf.called.should.equal(true); BaseNode.prototype._loadConfiguration.called.should.equal(true); }); }); @@ -182,15 +184,44 @@ describe('Bitcoind Node', function() { }); describe('#_loadDB', function() { it('should load the db', function() { - var DB = function() {}; + var DB = function(config) { + config.path.should.equal(process.env.HOME + '/.bitcoin/bitcoindjs.db'); + }; var config = { - DB: DB + DB: DB, + datadir: '~/.bitcoin' }; var node = new Node(config); + node.network = Networks.livenet; node._loadDB(config); node.db.should.be.instanceof(DB); }); + it('should load the db for testnet', function() { + var DB = function(config) { + config.path.should.equal(process.env.HOME + '/.bitcoin/testnet3/bitcoindjs.db'); + }; + var config = { + DB: DB, + datadir: '~/.bitcoin' + }; + + var node = new Node(config); + node.network = Networks.testnet; + node._loadDB(config); + node.db.should.be.instanceof(DB); + }); + it('error with unknown network', function() { + var config = { + datadir: '~/.bitcoin' + }; + + var node = new Node(config); + node.network = 'not a network'; + (function() { + node._loadDB(config); + }).should.throw('Unknown network'); + }); }); describe('#_loadP2P', function() { it('should load p2p', function() {