embark/lib/blockchain.js

187 lines
4.6 KiB
JavaScript
Raw Normal View History

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