Skip to content

Commit

Permalink
Cleanup configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Jul 21, 2015
1 parent 4f502ec commit 0bbc388
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 28 deletions.
42 changes: 42 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var fixtureData = {
};

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

Expand Down
24 changes: 4 additions & 20 deletions example/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -52,4 +36,4 @@ node.chain.on('addblock', function(block) {
}

times[node.chain.tip.__height % count] = Date.now();
});
});
51 changes: 46 additions & 5 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
};
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0bbc388

Please sign in to comment.