Create data directory on a fresh start.
This commit is contained in:
parent
ac09e767fb
commit
6dc3577e00
@ -1,9 +1,3 @@
|
||||
/**
|
||||
* bitcoind.js
|
||||
* Copyright (c) 2014, BitPay (MIT License)
|
||||
* A bitcoind node.js binding.
|
||||
*/
|
||||
|
||||
var net = require('net');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var bitcoindjs = require('bindings')('bitcoindjs.node');
|
||||
@ -11,16 +5,13 @@ var util = require('util');
|
||||
var fs = require('fs');
|
||||
var mkdirp = require('mkdirp');
|
||||
var tiny = require('tiny').json;
|
||||
|
||||
// Compatibility with old node versions:
|
||||
var setImmediate = global.setImmediate || process.nextTick.bind(process);
|
||||
|
||||
/**
|
||||
* Daemon
|
||||
*/
|
||||
var bitcore = require('bitcore');
|
||||
var $ = bitcore.util.preconditions;
|
||||
|
||||
var daemon = Daemon;
|
||||
|
||||
|
||||
|
||||
function Daemon(options) {
|
||||
var self = this;
|
||||
|
||||
@ -29,44 +20,25 @@ function Daemon(options) {
|
||||
}
|
||||
|
||||
if (Object.keys(this.instances).length) {
|
||||
throw new
|
||||
Error('bitcoind.js cannot be instantiated more than once.');
|
||||
throw new Error('Daemon cannot be instantiated more than once.');
|
||||
}
|
||||
|
||||
EventEmitter.call(this);
|
||||
|
||||
$.checkArgument(options.datadir, 'Please specify a datadir');
|
||||
|
||||
this.options = options || {};
|
||||
|
||||
|
||||
if (!this.options.datadir) {
|
||||
this.options.datadir = '~/.bitcoind.js';
|
||||
}
|
||||
|
||||
this.options.datadir = this.options.datadir.replace(/^~/, process.env.HOME);
|
||||
|
||||
this.datadir = this.options.datadir;
|
||||
|
||||
this.config = this.datadir + '/bitcoin.conf';
|
||||
this.network = Daemon['livenet'];
|
||||
|
||||
this.network = Daemon.livenet;
|
||||
|
||||
if (this.options.network === 'testnet') {
|
||||
this.network = Daemon['testnet'];
|
||||
this.network = Daemon.testnet;
|
||||
} else if(this.options.network === 'regtest') {
|
||||
this.network = Daemon['regtest'];
|
||||
}
|
||||
|
||||
if (!fs.existsSync(this.datadir)) {
|
||||
mkdirp.sync(this.datadir);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(this.config)) {
|
||||
var password = ''
|
||||
+ Math.random().toString(36).slice(2)
|
||||
+ Math.random().toString(36).slice(2)
|
||||
+ Math.random().toString(36).slice(2);
|
||||
fs.writeFileSync(this.config, ''
|
||||
+ 'rpcuser=bitcoinrpc\n'
|
||||
+ 'rpcpassword=' + password + '\n'
|
||||
);
|
||||
this.network = Daemon.regtest;
|
||||
}
|
||||
|
||||
// Add hardcoded peers
|
||||
@ -87,7 +59,8 @@ function Daemon(options) {
|
||||
}
|
||||
fs.writeFileSync(
|
||||
this.datadir + '/testnet3/bitcoin.conf',
|
||||
fs.readFileSync(this.config));
|
||||
fs.readFileSync(this.config)
|
||||
);
|
||||
}
|
||||
|
||||
if (this.network.name === 'regtest') {
|
||||
@ -96,7 +69,8 @@ function Daemon(options) {
|
||||
}
|
||||
fs.writeFileSync(
|
||||
this.datadir + '/regtest/bitcoin.conf',
|
||||
fs.readFileSync(this.config));
|
||||
fs.readFileSync(this.config)
|
||||
);
|
||||
}
|
||||
|
||||
Object.keys(exports).forEach(function(key) {
|
||||
@ -423,7 +397,6 @@ Daemon.prototype.getAddresses = function() {
|
||||
};
|
||||
|
||||
Daemon.prototype.getProgress = function(callback) {
|
||||
if (daemon.stopping) return [];
|
||||
return bitcoindjs.getProgress(callback);
|
||||
};
|
||||
|
||||
|
||||
15
lib/node.js
15
lib/node.js
@ -9,6 +9,7 @@ var P2P = chainlib.P2P;
|
||||
var fs = require('fs');
|
||||
var BaseNode = chainlib.Node;
|
||||
var util = require('util');
|
||||
var mkdirp = require('mkdirp');
|
||||
var log = chainlib.log;
|
||||
var bitcore = require('bitcore');
|
||||
var Networks = bitcore.Networks;
|
||||
@ -36,6 +37,8 @@ Node.SYNC_STRATEGIES = {
|
||||
BITCOIND: 'bitcoind'
|
||||
};
|
||||
|
||||
Node.DEFAULT_DAEMON_CONFIG = 'whitelist=127.0.0.1\n' + 'txindex=1\n';
|
||||
|
||||
Node.prototype.setSyncStrategy = function(strategy) {
|
||||
this.syncStrategy = strategy;
|
||||
|
||||
@ -53,8 +56,18 @@ 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);
|
||||
var configPath = datadir + '/bitcoin.conf';
|
||||
this.bitcoinConfiguration = {};
|
||||
var file = fs.readFileSync(datadir + '/bitcoin.conf');
|
||||
|
||||
if (!fs.existsSync(datadir)) {
|
||||
mkdirp.sync(datadir);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(configPath)) {
|
||||
fs.writeFileSync(configPath, Node.DEFAULT_DAEMON_CONFIG);
|
||||
}
|
||||
|
||||
var file = fs.readFileSync(configPath);
|
||||
var unparsed = file.toString().split('\n');
|
||||
for(var i = 0; i < unparsed.length; i++) {
|
||||
var line = unparsed[i];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user