2015-07-29 14:33:19 +02:00
|
|
|
var mkdirp = require('mkdirp');
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
Blockchain = function(blockchainConfig) {
|
|
|
|
this.config = blockchainConfig;
|
|
|
|
}
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
Blockchain.prototype.generate_basic_command = function() {
|
|
|
|
var config = this.config;
|
|
|
|
var address = config.account.address;
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
var cmd = "geth ";
|
2015-10-25 13:56:06 -05:00
|
|
|
var rpc_api = ['eth', 'web3'];
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-06-28 09:51:58 -04:00
|
|
|
if (config.datadir !== "default") {
|
|
|
|
cmd += "--datadir=\"" + config.datadir + "\" ";
|
|
|
|
cmd += "--logfile=\"" + config.datadir + ".log\" ";
|
2015-06-27 22:20:07 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 08:47:51 +01:00
|
|
|
if (config.geth_extra_opts) {
|
|
|
|
cmd += config.geth_extra_opts + " ";
|
|
|
|
}
|
|
|
|
|
2015-06-28 09:51:58 -04:00
|
|
|
cmd += "--port " + config.port + " ";
|
2015-06-27 22:20:07 -04:00
|
|
|
cmd += "--rpc ";
|
2015-06-28 09:51:58 -04:00
|
|
|
cmd += "--rpcport " + config.rpcPort + " ";
|
2015-08-02 09:02:09 +00:00
|
|
|
cmd += "--rpcaddr " + config.rpcHost + " ";
|
2015-06-28 09:51:58 -04:00
|
|
|
cmd += "--networkid " + config.networkId + " ";
|
2015-12-10 13:59:35 -05:00
|
|
|
cmd += "--rpccorsdomain " + config.rpcWhitelist + " ";
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-11-20 11:57:51 -05:00
|
|
|
if(config.testnet){
|
2015-11-20 11:18:32 -05:00
|
|
|
cmd += "--testnet "
|
|
|
|
}
|
|
|
|
|
2015-06-28 09:51:58 -04:00
|
|
|
if (config.minerthreads !== void 0) {
|
|
|
|
cmd += "--minerthreads \"" + config.minerthreads + "\" ";
|
2015-06-27 22:20:07 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 15:21:46 -04:00
|
|
|
if(config.mine)
|
|
|
|
cmd += "--mine ";
|
2015-10-19 19:54:27 -04:00
|
|
|
|
|
|
|
if (config.genesisBlock !== void 0)
|
2015-07-29 15:49:02 +02:00
|
|
|
cmd += "--genesis=\"" + config.genesisBlock + "\" ";
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-08-09 16:41:42 -04:00
|
|
|
if (config.whisper) {
|
|
|
|
cmd += "--shh ";
|
|
|
|
rpc_api.push('shh')
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd += '--rpcapi "' + rpc_api.join(',') + '" ';
|
|
|
|
|
2015-10-25 13:56:06 -05:00
|
|
|
//TODO: this should be configurable
|
2015-07-29 21:40:49 -04:00
|
|
|
cmd += "--maxpeers " + config.maxPeers + " ";
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-06-28 09:51:58 -04:00
|
|
|
if (config.account.password !== void 0) {
|
|
|
|
cmd += "--password " + config.account.password + " ";
|
2015-06-27 22:20:07 -04:00
|
|
|
}
|
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
Blockchain.prototype.list_command = function() {
|
|
|
|
return this.generate_basic_command() + "account list ";
|
|
|
|
}
|
|
|
|
|
|
|
|
Blockchain.prototype.init_command = function() {
|
|
|
|
return this.generate_basic_command() + "account new ";
|
|
|
|
}
|
|
|
|
|
2015-08-18 12:05:20 +02:00
|
|
|
Blockchain.prototype.geth_command = function(geth_args) {
|
|
|
|
return this.generate_basic_command() + geth_args;
|
|
|
|
}
|
|
|
|
|
2015-07-12 21:33:36 -04:00
|
|
|
Blockchain.prototype.run_command = function(address, use_tmp) {
|
2015-07-03 08:53:42 -04:00
|
|
|
var cmd = this.generate_basic_command();
|
|
|
|
var config = this.config;
|
2015-06-27 22:20:07 -04:00
|
|
|
|
|
|
|
if (address !== void 0) {
|
|
|
|
cmd += "--unlock " + address + " ";
|
|
|
|
}
|
|
|
|
|
2015-10-23 19:30:30 -05:00
|
|
|
if (config.bootNodes !== undefined && config.bootNodes.boot == true) {
|
2015-10-20 22:06:17 -05:00
|
|
|
cmd += "--bootnodes \"";
|
2015-10-01 08:23:41 -05:00
|
|
|
for (var i = 0; i < config.bootNodes.enodes.length; i++){
|
2015-10-20 22:06:17 -05:00
|
|
|
cmd += config.bootNodes.enodes[i];
|
2015-10-23 19:30:30 -05:00
|
|
|
if (i != config.bootNodes.enodes.length - 1) cmd += " ";
|
2015-10-01 08:23:41 -05:00
|
|
|
}
|
2015-10-20 22:06:17 -05:00
|
|
|
cmd += "\"";
|
2015-06-27 22:20:07 -04:00
|
|
|
}
|
|
|
|
|
2015-06-28 09:51:58 -04:00
|
|
|
if (config.mine_when_needed) {
|
2015-07-12 21:33:36 -04:00
|
|
|
if (use_tmp) {
|
2015-07-22 08:30:04 -04:00
|
|
|
cmd += "js /tmp/js/mine.js";
|
2015-07-12 21:33:36 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
cmd += "js node_modules/embark-framework/js/mine.js";
|
|
|
|
}
|
2015-06-27 22:20:07 -04:00
|
|
|
}
|
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
Blockchain.prototype.get_address = function() {
|
|
|
|
var config = this.config;
|
2015-09-25 14:46:28 -04:00
|
|
|
|
2015-09-25 15:16:44 -04:00
|
|
|
if(config.account.address)
|
|
|
|
return config.account.address;
|
2015-09-25 14:46:28 -04:00
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
var address = null;
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
if (config.account.init) {
|
2015-07-29 14:33:19 +02:00
|
|
|
// ensure datadir exists, bypassing the interactive liabilities prompt.
|
|
|
|
var newDir = mkdirp.sync(config.datadir);
|
|
|
|
if (newDir) {
|
|
|
|
console.log("=== datadir created");
|
|
|
|
} else {
|
|
|
|
console.log("=== datadir already exists");
|
|
|
|
}
|
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
console.log("running: " + this.list_command());
|
|
|
|
result = exec(this.list_command());
|
|
|
|
|
|
|
|
if (result.output.indexOf("Fatal") < 0) {
|
|
|
|
console.log("=== already initialized");
|
|
|
|
address = result.output.match(/{(\w+)}/)[1];
|
|
|
|
} else {
|
|
|
|
console.log("running: " + this.init_command());
|
|
|
|
result = exec(this.init_command());
|
2015-07-09 07:43:53 -04:00
|
|
|
address = result.output.match(/{(\w+)}/)[1];
|
2015-07-03 08:53:42 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-27 22:20:07 -04:00
|
|
|
|
2015-07-03 08:53:42 -04:00
|
|
|
return address;
|
2015-06-27 22:20:07 -04:00
|
|
|
}
|
|
|
|
|
2015-07-12 21:33:36 -04:00
|
|
|
Blockchain.prototype.startChain = function(use_tmp) {
|
2015-07-03 08:53:42 -04:00
|
|
|
var address = this.get_address();
|
2015-07-12 21:33:36 -04:00
|
|
|
console.log("running: " + this.run_command(address, use_tmp));
|
|
|
|
exec(this.run_command(address, use_tmp));
|
2015-06-27 22:20:07 -04:00
|
|
|
}
|
|
|
|
|
2015-08-18 12:05:20 +02:00
|
|
|
Blockchain.prototype.execGeth = function(args) {
|
|
|
|
var cmd = this.geth_command(args);
|
|
|
|
console.log("executing: " + cmd);
|
|
|
|
exec(cmd);
|
|
|
|
}
|
|
|
|
|
2015-08-31 00:48:38 +08:00
|
|
|
Blockchain.prototype.getStartChainCommand = function(use_tmp) {
|
|
|
|
var address = this.get_address();
|
|
|
|
return this.run_command(address, use_tmp);
|
|
|
|
}
|
|
|
|
|
2015-06-27 22:20:07 -04:00
|
|
|
module.exports = Blockchain
|