2018-07-12 14:54:45 -05:00
|
|
|
const path = require('path');
|
2018-08-12 17:10:49 -05:00
|
|
|
const pkgUp = require('pkg-up');
|
2017-03-30 02:50:05 +09:00
|
|
|
let shelljs = require('shelljs');
|
2018-08-07 18:57:49 -04:00
|
|
|
let proxy = require('./proxy');
|
|
|
|
const Ipc = require('../../core/ipc');
|
|
|
|
const constants = require('../../constants.json');
|
|
|
|
const {defaultHost, dockerHostSwap} = require('../../utils/host');
|
2018-09-07 16:28:03 -05:00
|
|
|
const fs = require('../../core/fs.js');
|
2018-06-11 16:40:14 -04:00
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
class Simulator {
|
|
|
|
constructor(options) {
|
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
2018-04-27 09:16:29 -04:00
|
|
|
this.logger = options.logger;
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
2017-02-18 08:01:03 -05:00
|
|
|
|
2018-06-11 16:43:08 -04:00
|
|
|
run(options) {
|
2018-06-11 09:02:52 -04:00
|
|
|
let cmds = [];
|
2017-02-18 08:01:03 -05:00
|
|
|
|
2018-09-07 16:28:03 -05:00
|
|
|
const ganache_main = require.resolve('ganache-cli', {paths: fs.embarkPath('node_modules')});
|
2018-08-12 17:10:49 -05:00
|
|
|
const ganache_json = pkgUp.sync(path.dirname(ganache_main));
|
|
|
|
const ganache_root = path.dirname(ganache_json);
|
|
|
|
const ganache_bin = require(ganache_json).bin;
|
|
|
|
let ganache;
|
|
|
|
if (typeof ganache_bin === 'string') {
|
|
|
|
ganache = path.join(ganache_root, ganache_bin);
|
|
|
|
} else {
|
|
|
|
ganache = path.join(ganache_root, ganache_bin['ganache-cli']);
|
|
|
|
}
|
2018-04-27 09:16:29 -04:00
|
|
|
|
2018-06-07 15:13:35 -04:00
|
|
|
let useProxy = this.blockchainConfig.proxy || false;
|
2018-07-15 10:12:18 -05:00
|
|
|
let host = (dockerHostSwap(options.host || this.blockchainConfig.rpcHost) || defaultHost);
|
2018-06-07 13:03:04 -04:00
|
|
|
let port = (options.port || this.blockchainConfig.rpcPort || 8545);
|
2018-09-19 21:12:20 -05:00
|
|
|
port = parseInt(port, 10) + (useProxy ? constants.blockchain.servicePortOnProxy : 0);
|
2018-06-07 13:03:04 -04:00
|
|
|
|
2018-09-19 19:48:00 -05:00
|
|
|
cmds.push("-p " + port);
|
2018-07-12 16:04:19 -05:00
|
|
|
cmds.push("-h " + host);
|
2017-12-27 17:48:33 -05:00
|
|
|
cmds.push("-a " + (options.numAccounts || 10));
|
|
|
|
cmds.push("-e " + (options.defaultBalance || 100));
|
|
|
|
cmds.push("-l " + (options.gasLimit || 8000000));
|
2017-02-18 08:01:03 -05: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) +"\"");
|
|
|
|
}
|
|
|
|
|
2018-08-06 20:28:16 +00:00
|
|
|
// as ganache-cli documentation explains, the simulatorAccounts configuration overrides a mnemonic
|
2018-08-03 23:08:02 +00:00
|
|
|
let simulatorAccounts = this.blockchainConfig.simulatorAccounts || options.simulatorAccounts;
|
|
|
|
if (simulatorAccounts && simulatorAccounts.length > 0) {
|
2018-08-06 20:28:16 +00:00
|
|
|
let web3 = new (require('web3'))();
|
2018-08-07 18:57:49 -04:00
|
|
|
let AccountParser = require('../../utils/accountParser.js');
|
2018-08-06 20:28:16 +00:00
|
|
|
let parsedAccounts = AccountParser.parseAccountsConfig(simulatorAccounts, web3, this.logger);
|
|
|
|
parsedAccounts.forEach((account) => {
|
|
|
|
let cmd = '--account="' + account.privateKey + ','+account.hexBalance + '"';
|
2018-08-03 23:08:02 +00:00
|
|
|
cmds.push(cmd);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-15 21:55:28 +00:00
|
|
|
// 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-08-15 23:23:48 +00:00
|
|
|
// Setting up network id for simulator from blockchainConfig or options.
|
|
|
|
// Otherwise ganache-cli would make random network id.
|
|
|
|
let networkId = this.blockchainConfig.networkId || options.networkId;
|
2018-08-16 18:09:12 +00:00
|
|
|
if (networkId) { // Don't handle networkId=="0" because it is not a valid networkId for ganache-cli.
|
2018-08-15 23:23:48 +00:00
|
|
|
cmds.push("--networkId " + networkId);
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:01:27 -05:00
|
|
|
const programName = 'ganache-cli';
|
|
|
|
const program = ganache;
|
2018-07-11 10:32:47 -05:00
|
|
|
console.log(`running: ${programName} ${cmds.join(' ')}`);
|
2018-06-07 15:13:35 -04:00
|
|
|
shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true});
|
|
|
|
|
|
|
|
if(useProxy){
|
2018-06-11 16:40:14 -04:00
|
|
|
let ipcObject = new Ipc({ipcRole: 'client'});
|
|
|
|
proxy.serve(ipcObject, host, port, false);
|
2018-06-07 15:13:35 -04:00
|
|
|
}
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 08:01:03 -05:00
|
|
|
|
|
|
|
module.exports = Simulator;
|