2016-09-25 06:23:33 +00:00
|
|
|
var colors = require('colors');
|
2017-02-18 21:06:39 +00:00
|
|
|
var shelljs = require('shelljs');
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2017-02-19 17:51:32 +00:00
|
|
|
var fs = require('../../core/fs.js');
|
|
|
|
|
|
|
|
var GethCommands = require('./geth_commands.js');
|
|
|
|
|
2017-03-02 02:28:27 +00:00
|
|
|
/*eslint complexity: ["error", 22]*/
|
2017-02-22 01:54:07 +00:00
|
|
|
var Blockchain = function(options) {
|
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
|
|
|
this.env = options.env || 'development';
|
|
|
|
this.client = options.client;
|
2016-09-25 06:23:33 +00:00
|
|
|
|
|
|
|
this.config = {
|
2017-02-18 13:55:33 +00:00
|
|
|
geth_bin: this.blockchainConfig.geth_bin || 'geth',
|
2016-09-25 06:23:33 +00:00
|
|
|
networkType: this.blockchainConfig.networkType || 'custom',
|
|
|
|
genesisBlock: this.blockchainConfig.genesisBlock || false,
|
|
|
|
datadir: this.blockchainConfig.datadir || false,
|
|
|
|
mineWhenNeeded: this.blockchainConfig.mineWhenNeeded || false,
|
|
|
|
rpcHost: this.blockchainConfig.rpcHost || 'localhost',
|
|
|
|
rpcPort: this.blockchainConfig.rpcPort || 8545,
|
2016-09-26 00:51:00 +00:00
|
|
|
rpcCorsDomain: this.blockchainConfig.rpcCorsDomain || false,
|
2016-09-25 06:23:33 +00:00
|
|
|
networkId: this.blockchainConfig.networkId || 12301,
|
|
|
|
port: this.blockchainConfig.port || 30303,
|
|
|
|
nodiscover: this.blockchainConfig.nodiscover || false,
|
|
|
|
mine: this.blockchainConfig.mine || false,
|
2016-10-30 23:53:23 +00:00
|
|
|
account: this.blockchainConfig.account || {},
|
2016-10-31 00:35:11 +00:00
|
|
|
whisper: (this.blockchainConfig.whisper === undefined) || this.blockchainConfig.whisper,
|
2017-02-18 13:24:23 +00:00
|
|
|
maxpeers: ((this.blockchainConfig.maxpeers === 0) ? 0 : (this.blockchainConfig.maxpeers || 25)),
|
2017-02-18 13:41:18 +00:00
|
|
|
bootnodes: this.blockchainConfig.bootnodes || "",
|
|
|
|
rpcApi: (this.blockchainConfig.rpcApi || ['eth', 'web3', 'net']),
|
|
|
|
vmdebug: this.blockchainConfig.vmdebug || false
|
2016-09-25 06:23:33 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 01:54:07 +00:00
|
|
|
this.client = new options.client({config: this.config, env: this.env});
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
Blockchain.prototype.runCommand = function(cmd) {
|
|
|
|
console.log(("running: " + cmd.underline).green);
|
2017-03-21 18:28:36 +00:00
|
|
|
return shelljs.exec(cmd);
|
2016-09-25 06:23:33 +00:00
|
|
|
};
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
Blockchain.prototype.run = function() {
|
2017-03-02 13:15:35 +00:00
|
|
|
var self = this;
|
2016-09-25 06:23:33 +00:00
|
|
|
console.log("===============================================================================".magenta);
|
|
|
|
console.log("===============================================================================".magenta);
|
|
|
|
console.log(("Embark Blockchain Using: " + this.client.name.underline).magenta);
|
|
|
|
console.log("===============================================================================".magenta);
|
|
|
|
console.log("===============================================================================".magenta);
|
|
|
|
var address = this.initChainAndGetAddress();
|
2017-03-02 13:15:35 +00:00
|
|
|
this.client.mainCommand(address, function(cmd) {
|
2017-03-21 18:28:36 +00:00
|
|
|
self.runCommand(cmd, {async:true});
|
2017-03-02 13:15:35 +00:00
|
|
|
});
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
Blockchain.prototype.initChainAndGetAddress = function() {
|
2016-08-21 14:42:42 +00:00
|
|
|
var address = null, result;
|
|
|
|
|
|
|
|
// ensure datadir exists, bypassing the interactive liabilities prompt.
|
2017-02-20 21:11:27 +00:00
|
|
|
this.datadir = '.embark/' + this.env + '/datadir';
|
2017-02-19 03:40:42 +00:00
|
|
|
fs.mkdirpSync(this.datadir);
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
// copy mining script
|
2017-02-20 21:11:27 +00:00
|
|
|
fs.copySync(fs.embarkPath("js"), ".embark/" + this.env + "/js", {overwrite: true});
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
// check if an account already exists, create one if not, return address
|
|
|
|
result = this.runCommand(this.client.listAccountsCommand());
|
2016-08-21 14:42:42 +00:00
|
|
|
if (result.output === undefined || result.output === '' || result.output.indexOf("Fatal") >= 0) {
|
2016-09-25 06:23:33 +00:00
|
|
|
console.log("no accounts found".green);
|
|
|
|
if (this.config.genesisBlock) {
|
|
|
|
console.log("initializing genesis block".green);
|
|
|
|
result = this.runCommand(this.client.initGenesisCommmand());
|
|
|
|
}
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
result = this.runCommand(this.client.newAccountCommand());
|
2016-08-21 14:42:42 +00:00
|
|
|
address = result.output.match(/{(\w+)}/)[1];
|
|
|
|
} else {
|
2016-09-25 06:23:33 +00:00
|
|
|
console.log("already initialized".green);
|
2016-08-21 14:42:42 +00:00
|
|
|
address = result.output.match(/{(\w+)}/)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
|
|
|
};
|
|
|
|
|
2017-02-20 21:11:27 +00:00
|
|
|
var BlockchainClient = function(blockchainConfig, client, env) {
|
2016-08-21 14:42:42 +00:00
|
|
|
if (client === 'geth') {
|
2017-02-22 01:54:07 +00:00
|
|
|
return new Blockchain({blockchainConfig: blockchainConfig, client: GethCommands, env: env});
|
2016-08-21 14:42:42 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('unknown client');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = BlockchainClient;
|
|
|
|
|