embark-area-51/lib/cmds/blockchain/geth_commands.js

241 lines
7.0 KiB
JavaScript
Raw Normal View History

const async = require('async');
// TODO: make all of this async
2017-03-30 11:12:39 +00:00
class GethCommands {
constructor(options) {
2017-03-30 13:16:46 +00:00
this.config = options && options.hasOwnProperty('config') ? options.config : {};
this.env = options && options.hasOwnProperty('env') ? options.env : 'development';
2018-05-09 13:17:48 +00:00
this.isDev = options && options.hasOwnProperty('isDev') ? options.isDev : (this.env === 'development');
2017-03-30 11:12:39 +00:00
this.name = "Go-Ethereum (https://github.com/ethereum/go-ethereum)";
this.geth_bin = this.config.geth_bin || "geth";
}
2017-03-30 11:12:39 +00:00
commonOptions() {
let config = this.config;
2018-05-28 15:54:31 +00:00
let cmd = [];
2018-05-28 15:54:31 +00:00
cmd.push(this.determineNetworkType(config));
2017-03-30 11:12:39 +00:00
if (config.datadir) {
2018-05-28 15:55:16 +00:00
cmd.push(`--datadir=${config.datadir}`);
2017-03-30 11:12:39 +00:00
}
if (config.syncMode || config.syncmode) {
2018-06-29 20:42:01 +00:00
cmd.push("--syncmode=" + config.syncmode || config.syncMode);
2017-03-30 11:12:39 +00:00
}
2017-03-30 11:12:39 +00:00
if (config.account && config.account.password) {
2018-05-28 15:55:16 +00:00
cmd.push(`--password=${config.account.password}`);
2017-03-30 11:12:39 +00:00
}
2018-05-22 13:11:38 +00:00
if (Number.isInteger(config.verbosity) && config.verbosity >=0 && config.verbosity <= 5) {
2018-05-28 15:54:31 +00:00
cmd.push("--verbosity=" + config.verbosity);
2018-05-22 13:11:38 +00:00
}
2017-03-30 11:12:39 +00:00
return cmd;
}
2018-05-28 15:55:16 +00:00
determineVersionCommand() {
return this.geth_bin + " version";
}
2017-03-30 11:12:39 +00:00
determineNetworkType(config) {
2018-05-28 15:54:31 +00:00
let cmd;
2017-03-30 11:12:39 +00:00
if (config.networkType === 'testnet') {
2018-06-14 19:22:01 +00:00
cmd = "--testnet";
} else if (config.networkType === 'rinkeby') {
cmd = "--rinkeby";
2017-03-30 11:12:39 +00:00
} else if (config.networkType === 'custom') {
2018-05-28 15:54:31 +00:00
cmd = "--networkid=" + config.networkId;
2017-03-30 11:12:39 +00:00
}
return cmd;
}
2017-03-30 11:12:39 +00:00
initGenesisCommmand() {
let config = this.config;
2018-05-28 15:54:31 +00:00
let cmd = this.geth_bin + " " + this.commonOptions().join(' ');
2017-03-30 11:12:39 +00:00
if (config.genesisBlock) {
2018-05-28 15:55:16 +00:00
cmd += " init \"" + config.genesisBlock + "\" ";
2017-03-30 11:12:39 +00:00
}
2017-03-30 11:12:39 +00:00
return cmd;
}
2016-10-31 00:21:28 +00:00
2017-03-30 11:12:39 +00:00
newAccountCommand() {
2018-05-28 15:55:16 +00:00
return this.geth_bin + " " + this.commonOptions().join(' ') + " account new ";
2017-03-30 11:12:39 +00:00
}
2017-03-30 11:12:39 +00:00
listAccountsCommand() {
2018-05-28 15:55:16 +00:00
return this.geth_bin + " " + this.commonOptions().join(' ') + " account list ";
2017-03-30 11:12:39 +00:00
}
2017-03-30 11:12:39 +00:00
determineRpcOptions(config) {
2018-05-28 15:54:31 +00:00
let cmd = [];
2017-03-30 11:12:39 +00:00
2018-05-28 15:54:31 +00:00
cmd.push("--port=" + config.port);
cmd.push("--rpc");
cmd.push("--rpcport=" + config.rpcPort);
cmd.push("--rpcaddr=" + config.rpcHost);
2017-03-30 11:12:39 +00:00
if (config.rpcCorsDomain) {
if (config.rpcCorsDomain === '*') {
console.log('==================================');
2018-05-08 21:49:46 +00:00
console.log(__('rpcCorsDomain set to *'));
console.log(__('make sure you know what you are doing'));
2017-03-30 11:12:39 +00:00
console.log('==================================');
}
2018-05-28 14:16:07 +00:00
cmd.push("--rpccorsdomain=" + config.rpcCorsDomain);
2017-03-30 11:12:39 +00:00
} else {
console.log('==================================');
2018-05-08 21:49:46 +00:00
console.log(__('warning: cors is not set'));
console.log('==================================');
}
2017-03-30 11:12:39 +00:00
return cmd;
}
determineWsOptions(config) {
2018-05-28 15:54:31 +00:00
let cmd = [];
if (config.wsRPC) {
2018-05-28 15:54:31 +00:00
cmd.push("--ws");
cmd.push("--wsport=" + config.wsPort);
cmd.push("--wsaddr=" + config.wsHost);
if (config.wsOrigins) {
if (config.wsOrigins === '*') {
console.log('==================================');
2018-05-08 21:49:46 +00:00
console.log(__('wsOrigins set to *'));
console.log(__('make sure you know what you are doing'));
console.log('==================================');
}
2018-05-28 14:16:07 +00:00
cmd.push("--wsorigins=" + config.wsOrigins);
} else {
console.log('==================================');
2018-05-08 21:49:46 +00:00
console.log(__('warning: wsOrigins is not set'));
console.log('==================================');
}
}
return cmd;
}
2017-03-30 11:12:39 +00:00
mainCommand(address, done) {
let self = this;
let config = this.config;
2018-06-12 16:21:46 +00:00
let rpc_api = (this.config.rpcApi || ['eth', 'web3', 'net', 'debug']);
2018-07-19 07:58:47 +00:00
let defaultWsApi = ['eth', 'web3', 'net', 'shh', 'debug'];
if (this.isDev) {
defaultWsApi.push('personal');
}
let ws_api = (this.config.wsApi || defaultWsApi);
2017-03-30 11:12:39 +00:00
2018-05-28 15:54:31 +00:00
let args = [];
2017-03-30 11:12:39 +00:00
async.series([
function commonOptions(callback) {
let cmd = self.commonOptions();
2018-05-28 15:54:31 +00:00
args = args.concat(cmd);
2017-03-30 11:12:39 +00:00
callback(null, cmd);
},
function rpcOptions(callback) {
let cmd = self.determineRpcOptions(self.config);
2018-05-28 15:54:31 +00:00
args = args.concat(cmd);
2017-03-30 11:12:39 +00:00
callback(null, cmd);
},
function wsOptions(callback) {
let cmd = self.determineWsOptions(self.config);
2018-05-28 15:54:31 +00:00
args = args.concat(cmd);
callback(null, cmd);
},
2017-03-30 11:12:39 +00:00
function dontGetPeers(callback) {
if (config.nodiscover) {
2018-05-28 15:54:31 +00:00
args.push("--nodiscover");
2017-03-30 11:12:39 +00:00
return callback(null, "--nodiscover");
}
callback(null, "");
},
function vmDebug(callback) {
if (config.vmdebug) {
2018-05-28 15:54:31 +00:00
args.push("--vmdebug");
2017-03-30 11:12:39 +00:00
return callback(null, "--vmdebug");
}
callback(null, "");
},
function maxPeers(callback) {
2018-05-28 15:54:31 +00:00
let cmd = "--maxpeers=" + config.maxpeers;
args.push(cmd);
2017-03-30 11:12:39 +00:00
callback(null, cmd);
},
function mining(callback) {
if (config.mineWhenNeeded || config.mine) {
2018-05-28 15:54:31 +00:00
args.push("--mine");
return callback(null, "--mine");
2017-03-30 11:12:39 +00:00
}
callback("");
},
function bootnodes(callback) {
if (config.bootnodes && config.bootnodes !== "" && config.bootnodes !== []) {
2018-05-28 15:54:31 +00:00
args.push("--bootnodes=" + config.bootnodes);
return callback(null, "--bootnodes=" + config.bootnodes);
2017-03-30 11:12:39 +00:00
}
callback("");
},
function whisper(callback) {
if (config.whisper) {
rpc_api.push('shh');
if (ws_api.indexOf('shh') === -1) {
ws_api.push('shh');
}
2018-05-28 15:54:31 +00:00
args.push("--shh");
2017-03-30 11:12:39 +00:00
return callback(null, "--shh ");
}
callback("");
},
function rpcApi(callback) {
2018-05-28 14:16:07 +00:00
args.push('--rpcapi=' + rpc_api.join(','));
callback(null, '--rpcapi=' + rpc_api.join(','));
2017-03-30 11:12:39 +00:00
},
function wsApi(callback) {
2018-05-28 14:16:07 +00:00
args.push('--wsapi=' + ws_api.join(','));
callback(null, '--wsapi=' + ws_api.join(','));
},
2017-03-30 11:12:39 +00:00
function accountToUnlock(callback) {
2017-03-30 13:16:46 +00:00
let accountAddress = "";
if(config.account && config.account.address) {
2017-03-30 13:16:46 +00:00
accountAddress = config.account.address;
} else {
accountAddress = address;
}
2018-05-09 13:17:48 +00:00
if (accountAddress && !self.isDev) {
2018-05-28 15:54:31 +00:00
args.push("--unlock=" + accountAddress);
2017-03-30 11:12:39 +00:00
return callback(null, "--unlock=" + accountAddress);
}
callback(null, "");
},
function gasLimit(callback) {
if (config.targetGasLimit) {
2018-05-28 15:54:31 +00:00
args.push("--targetgaslimit=" + config.targetGasLimit);
return callback(null, "--targetgaslimit=" + config.targetGasLimit);
}
callback(null, "");
},
2018-05-08 20:25:48 +00:00
function isDev(callback) {
2018-05-09 13:17:48 +00:00
if (self.isDev) {
2018-05-28 15:54:31 +00:00
args.push('--dev');
2018-05-08 20:25:48 +00:00
return callback(null, '--dev');
}
callback(null, '');
2017-03-02 13:15:35 +00:00
}
2018-05-28 15:54:31 +00:00
], function (err) {
2017-03-30 11:12:39 +00:00
if (err) {
throw new Error(err.message);
2017-03-02 13:15:35 +00:00
}
2018-05-28 15:54:31 +00:00
return done(self.geth_bin, args);
2017-03-30 11:12:39 +00:00
});
}
}
module.exports = GethCommands;