2017-03-29 17:50:05 +00:00
|
|
|
let shelljs = require('shelljs');
|
2018-06-11 20:40:14 +00:00
|
|
|
let proxy = require('../core/proxy');
|
|
|
|
const Ipc = require('../core/ipc');
|
2018-06-11 20:43:08 +00:00
|
|
|
const constants = require('../constants.json');
|
2018-07-15 15:12:18 +00:00
|
|
|
const {defaultHost, dockerHostSwap} = require('../utils/host');
|
2018-06-11 20:40:14 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Simulator {
|
|
|
|
constructor(options) {
|
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
2018-04-27 13:16:29 +00:00
|
|
|
this.logger = options.logger;
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2017-02-18 13:01:03 +00:00
|
|
|
|
2018-06-11 20:43:08 +00:00
|
|
|
run(options) {
|
2018-06-11 13:02:52 +00:00
|
|
|
let cmds = [];
|
2017-02-18 13:01:03 +00:00
|
|
|
|
2018-07-11 17:55:08 +00:00
|
|
|
const ganache = shelljs.which('ganache-cli-embark');
|
2018-07-11 18:08:15 +00:00
|
|
|
// const testrpc = shelljs.which('testrpc');
|
2018-07-11 18:08:48 +00:00
|
|
|
// const ganache = shelljs.which('ganache-cli');
|
2018-07-11 18:01:27 +00:00
|
|
|
// if (!testrpc && !ganache) {
|
|
|
|
// this.logger.warn(__('%s is not installed on your machine', 'Ganache CLI (TestRPC)'));
|
|
|
|
// this.logger.info(__('You can install it by running: %s', 'npm -g install ganache-cli'));
|
|
|
|
// process.exit();
|
|
|
|
// }
|
2018-04-27 13:16:29 +00:00
|
|
|
|
2018-06-07 19:13:35 +00:00
|
|
|
let useProxy = this.blockchainConfig.proxy || false;
|
2018-07-15 15:12:18 +00:00
|
|
|
let host = (dockerHostSwap(options.host || this.blockchainConfig.rpcHost) || defaultHost);
|
2018-06-07 17:03:04 +00:00
|
|
|
let port = (options.port || this.blockchainConfig.rpcPort || 8545);
|
|
|
|
|
2018-06-11 20:43:08 +00:00
|
|
|
cmds.push("-p " + (port + (useProxy ? constants.blockchain.servicePortOnProxy : 0)));
|
2018-06-25 16:48:35 +00:00
|
|
|
if (!ganache) {
|
|
|
|
cmds.push("-h " + host);
|
|
|
|
}
|
2017-12-27 22:48:33 +00:00
|
|
|
cmds.push("-a " + (options.numAccounts || 10));
|
|
|
|
cmds.push("-e " + (options.defaultBalance || 100));
|
|
|
|
cmds.push("-l " + (options.gasLimit || 8000000));
|
2017-02-18 13:01:03 +00:00
|
|
|
|
2018-01-15 21:55:28 +00:00
|
|
|
// adding mnemonic only if it is defined in the blockchainConfig or options
|
|
|
|
let simulatorMnemonic = this.blockchainConfig.simulatorMnemonic || options.simulatorMnemonic;
|
|
|
|
if (simulatorMnemonic) {
|
|
|
|
cmds.push("--mnemonic \"" + (simulatorMnemonic) +"\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
// adding blocktime only if it is defined in the blockchainConfig or options
|
|
|
|
let simulatorBlocktime = this.blockchainConfig.simulatorBlocktime || options.simulatorBlocktime;
|
|
|
|
if (simulatorBlocktime) {
|
|
|
|
cmds.push("-b \"" + (simulatorBlocktime) +"\"");
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:01:27 +00:00
|
|
|
// const programName = ganache ? 'ganache-cli' : 'testrpc';
|
|
|
|
// const program = ganache ? ganache : testrpc;
|
|
|
|
const programName = 'ganache-cli';
|
|
|
|
const program = ganache;
|
2018-07-11 15:32:47 +00:00
|
|
|
console.log(`running: ${programName} ${cmds.join(' ')}`);
|
2018-06-07 19:13:35 +00:00
|
|
|
shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true});
|
|
|
|
|
|
|
|
if(useProxy){
|
2018-06-11 20:40:14 +00:00
|
|
|
let ipcObject = new Ipc({ipcRole: 'client'});
|
|
|
|
proxy.serve(ipcObject, host, port, false);
|
2018-06-07 19:13:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 13:01:03 +00:00
|
|
|
|
|
|
|
module.exports = Simulator;
|