Cleanup configuration options

This commit is contained in:
Braydon Fuller 2015-07-21 09:09:59 -04:00
parent 4f502ec580
commit 0bbc388ca6
6 changed files with 103 additions and 28 deletions

42
.jshintrc Normal file
View 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"
]
}

View File

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

View File

@ -27,7 +27,7 @@ var fixtureData = {
};
var bitcoind = require('../').daemon({
directory: '~/.bitcoin',
datadir: process.env.BITCOINDJS_DIR || '~/.bitcoin',
testnet: true
});

View File

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

View File

@ -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,25 @@ Node.prototype.setSyncStrategy = function(strategy) {
};
Node.prototype._loadBitcoinConf = function(config) {
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('=');
this.bitcoinConfiguration[option[0]] = option[1];
}
}
};
Node.prototype._loadBitcoind = function(config) {
var bitcoindConfig = {};
if (config.testnet) {
bitcoindConfig.directory = '~/.bitcoin/testnet3';
} else {
bitcoindConfig.directory = '~/.bitcoin';
}
$.checkArgument(config.datadir, 'Please specify "datadir" in configuration options');
bitcoindConfig.datadir = config.datadir;
bitcoindConfig.testnet = config.testnet;
// start the bitcoind daemon
this.bitcoind = daemon(bitcoindConfig);
@ -104,6 +119,7 @@ Node.prototype._loadNetwork = function(config) {
} else {
this.network = Networks.get('livenet');
}
$.checkState(this.network, 'Unrecognized network');
};
Node.prototype._loadDB = function(config) {
@ -116,6 +132,14 @@ Node.prototype._loadDB = function(config) {
config.db = {};
}
// Store the additional indexes in a new directory
// based on the network configuration and the datadir
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';
}
config.db.network = this.network;
this.db = new DB(config.db);
@ -127,6 +151,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.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

View File

@ -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"