Merge pull request #43 from braydonf/config
Cleanup configuration options. Closes #40
This commit is contained in:
commit
cda1e2a438
42
.jshintrc
Normal file
42
.jshintrc
Normal file
@ -0,0 +1,42 @@
|
||||
{
|
||||
"bitwise": false,
|
||||
"browser": true,
|
||||
"camelcase": false,
|
||||
"curly": true,
|
||||
"devel": false,
|
||||
"eqeqeq": true,
|
||||
"esnext": true,
|
||||
"freeze": true,
|
||||
"immed": true,
|
||||
"indent": 2,
|
||||
"latedef": true,
|
||||
"newcap": false,
|
||||
"noarg": true,
|
||||
"node": true,
|
||||
"noempty": true,
|
||||
"nonew": true,
|
||||
"quotmark": "single",
|
||||
"regexp": true,
|
||||
"smarttabs": false,
|
||||
"strict": true,
|
||||
"trailing": true,
|
||||
"undef": true,
|
||||
"unused": true,
|
||||
"maxparams": 4,
|
||||
"maxstatements": 15,
|
||||
"maxcomplexity": 10,
|
||||
"maxdepth": 3,
|
||||
"maxlen": 120,
|
||||
"multistr": true,
|
||||
"predef": [
|
||||
"after",
|
||||
"afterEach",
|
||||
"before",
|
||||
"beforeEach",
|
||||
"describe",
|
||||
"exports",
|
||||
"it",
|
||||
"module",
|
||||
"require"
|
||||
]
|
||||
}
|
||||
10
README.md
10
README.md
@ -20,12 +20,20 @@ npm install
|
||||
var BitcoinNode = require('bitcoind.js');
|
||||
|
||||
var configuration = {
|
||||
directory: '~/.bitcoin',
|
||||
datadir: '~/.bitcoin',
|
||||
testnet: true
|
||||
};
|
||||
|
||||
var node = new BitcoinNode(configuration);
|
||||
|
||||
node.on('ready', function() {
|
||||
console.log('Bitcoin Node Ready');
|
||||
});
|
||||
|
||||
node.on('error', function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
node.chain.on('addblock', function(block) {
|
||||
console.log('New Best Tip:', block.hash);
|
||||
});
|
||||
|
||||
@ -27,7 +27,7 @@ var fixtureData = {
|
||||
};
|
||||
|
||||
var bitcoind = require('../').daemon({
|
||||
directory: '~/.bitcoin',
|
||||
datadir: process.env.BITCOINDJS_DIR || '~/.bitcoin',
|
||||
testnet: true
|
||||
});
|
||||
|
||||
|
||||
@ -6,32 +6,16 @@ var chainlib = require('chainlib');
|
||||
var log = chainlib.log;
|
||||
//log.debug = function() {};
|
||||
|
||||
var privkey = 'tprv8ZgxMBicQKsPdj1QowoT9z1tY5Et38qaMjCHZVoPdPFb6narfmYkqTygEVHfUmY78k3HcaEpkyNCAQDANaXtwNe1HLFvcA7nqYj1B7wTSTo';
|
||||
|
||||
var configuration = {
|
||||
db: {
|
||||
xprivkey: privkey,
|
||||
path: './bitcoind.db'
|
||||
},
|
||||
p2p: {
|
||||
addrs: [
|
||||
{
|
||||
ip: {
|
||||
v4: '127.0.0.1'
|
||||
},
|
||||
port: 8333
|
||||
}
|
||||
],
|
||||
dnsSeed: false
|
||||
},
|
||||
testnet: false
|
||||
datadir: process.env.BITCOINDJS_DIR || '~/.bitcoin',
|
||||
testnet: true
|
||||
};
|
||||
|
||||
var node = new BitcoinNode(configuration);
|
||||
|
||||
var startHeight;
|
||||
var count = 100;
|
||||
var times = Array(count);
|
||||
var times = new Array(count);
|
||||
|
||||
node.on('ready', function() {
|
||||
times[node.chain.tip.__height % count] = Date.now();
|
||||
@ -52,4 +36,4 @@ node.chain.on('addblock', function(block) {
|
||||
}
|
||||
|
||||
times[node.chain.tip.__height % count] = Date.now();
|
||||
});
|
||||
});
|
||||
|
||||
61
lib/node.js
61
lib/node.js
@ -6,12 +6,14 @@ var Block = require('./block');
|
||||
var DB = require('./db');
|
||||
var chainlib = require('chainlib');
|
||||
var P2P = chainlib.P2P;
|
||||
var fs = require('fs');
|
||||
var BaseNode = chainlib.Node;
|
||||
var util = require('util');
|
||||
var log = chainlib.log;
|
||||
var bitcore = require('bitcore');
|
||||
var Networks = bitcore.Networks;
|
||||
var _ = bitcore.deps._;
|
||||
var $ = bitcore.util.preconditions;
|
||||
var genesis = require('./genesis.json');
|
||||
var daemon = require('./daemon');
|
||||
|
||||
@ -24,6 +26,7 @@ util.inherits(Node, BaseNode);
|
||||
|
||||
Node.prototype._loadConfiguration = function(config) {
|
||||
var self = this;
|
||||
this._loadBitcoinConf(config);
|
||||
this._loadBitcoind(config);
|
||||
Node.super_.prototype._loadConfiguration.call(self, config);
|
||||
};
|
||||
@ -47,13 +50,31 @@ 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');
|
||||
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.bitcoinConfiguration[option[0]] = value;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Node.prototype._loadBitcoind = function(config) {
|
||||
var bitcoindConfig = {};
|
||||
if (config.testnet) {
|
||||
bitcoindConfig.directory = '~/.bitcoin/testnet3';
|
||||
} else {
|
||||
bitcoindConfig.directory = '~/.bitcoin';
|
||||
}
|
||||
bitcoindConfig.datadir = config.datadir;
|
||||
bitcoindConfig.testnet = config.testnet;
|
||||
|
||||
// start the bitcoind daemon
|
||||
this.bitcoind = daemon(bitcoindConfig);
|
||||
@ -104,6 +125,7 @@ Node.prototype._loadNetwork = function(config) {
|
||||
} else {
|
||||
this.network = Networks.get('livenet');
|
||||
}
|
||||
$.checkState(this.network, 'Unrecognized network');
|
||||
};
|
||||
|
||||
Node.prototype._loadDB = function(config) {
|
||||
@ -116,6 +138,18 @@ Node.prototype._loadDB = function(config) {
|
||||
config.db = {};
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
this.db = new DB(config.db);
|
||||
@ -127,6 +161,23 @@ Node.prototype._loadP2P = function(config) {
|
||||
}
|
||||
config.p2p.noListen = true;
|
||||
config.p2p.network = this.network;
|
||||
|
||||
// We only want to directly connect via p2p to the trusted bitcoind daemon
|
||||
var port = 8333;
|
||||
if (this.bitcoinConfiguration && this.bitcoinConfiguration.port) {
|
||||
port = this.bitcoinConfiguration.port;
|
||||
} else if (this.network === Networks.testnet) {
|
||||
port = 18333;
|
||||
}
|
||||
config.p2p.addrs = [
|
||||
{
|
||||
ip: {
|
||||
v4: '127.0.0.1'
|
||||
},
|
||||
port: port
|
||||
}
|
||||
];
|
||||
config.p2p.dnsSeed = false;
|
||||
config.p2p.Transaction = this.db.Transaction;
|
||||
config.p2p.Block = this.Block;
|
||||
config.p2p.disableSync = true; // Disable p2p syncing and instead use bitcoind sync
|
||||
|
||||
@ -29,7 +29,7 @@
|
||||
"scripts": {
|
||||
"preinstall": "./bin/build-libbitcoind",
|
||||
"install": "./bin/build-bindings",
|
||||
"start": "export LD_LIBRARY_PATH=`./platform/os.sh osdir` && node example",
|
||||
"start": "node example",
|
||||
"debug_install": "./bin/build-libbitcoind debug && ./bin/build-bindings debug",
|
||||
"test": "NODE_ENV=test mocha --recursive",
|
||||
"coverage": "istanbul cover _mocha -- --recursive"
|
||||
|
||||
17
test/data/bitcoin.conf
Normal file
17
test/data/bitcoin.conf
Normal file
@ -0,0 +1,17 @@
|
||||
#testnet=1
|
||||
#irc=0
|
||||
#upnp=0
|
||||
server=1
|
||||
|
||||
whitelist=127.0.0.1
|
||||
txindex=1
|
||||
|
||||
# listen on different ports
|
||||
port=20000
|
||||
|
||||
rpcallowip=127.0.0.1
|
||||
|
||||
rpcuser=bitcoin
|
||||
rpcpassword=local321
|
||||
|
||||
|
||||
@ -11,6 +11,8 @@ var Block = require('../lib/block');
|
||||
var proxyquire = require('proxyquire');
|
||||
var chainlib = require('chainlib');
|
||||
var OriginalNode = chainlib.Node;
|
||||
var fs = require('fs');
|
||||
var bitcoinConfBuffer = fs.readFileSync('./test/data/bitcoin.conf');
|
||||
|
||||
var BaseNode = function() {};
|
||||
util.inherits(BaseNode, EventEmitter);
|
||||
@ -19,16 +21,23 @@ BaseNode.prototype._loadConfiguration = sinon.spy();
|
||||
BaseNode.prototype._initialize = sinon.spy();
|
||||
chainlib.Node = BaseNode;
|
||||
|
||||
var Node = proxyquire('../lib/node', {chainlib: chainlib});
|
||||
var Node = proxyquire('../lib/node', {
|
||||
chainlib: chainlib,
|
||||
fs: {
|
||||
readFileSync: sinon.stub().returns(bitcoinConfBuffer)
|
||||
}
|
||||
});
|
||||
chainlib.Node = OriginalNode;
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
@ -56,6 +65,22 @@ describe('Bitcoind Node', function() {
|
||||
}).should.throw('Strategy "unknown" is unknown');
|
||||
});
|
||||
});
|
||||
describe('#_loadBitcoinConf', function() {
|
||||
it('will parse a bitcoin.conf file', function() {
|
||||
var node = new Node({});
|
||||
node._loadBitcoinConf({datadir: '~/.bitcoin'});
|
||||
should.exist(node.bitcoinConfiguration);
|
||||
node.bitcoinConfiguration.should.deep.equal({
|
||||
server: 1,
|
||||
whitelist: '127.0.0.1',
|
||||
txindex: 1,
|
||||
port: 20000,
|
||||
rpcallowip: '127.0.0.1',
|
||||
rpcuser: 'bitcoin',
|
||||
rpcpassword: 'local321'
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('#_loadBitcoind', function() {
|
||||
it('should initialize', function() {
|
||||
var node = new Node({});
|
||||
@ -182,15 +207,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() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user