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

95 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-03-29 17:18:00 +00:00
let colors = require('colors');
let shelljs = require('shelljs');
2017-03-29 17:18:00 +00:00
let fs = require('../../core/fs.js');
2017-02-19 17:51:32 +00:00
2017-03-29 17:18:00 +00:00
let GethCommands = require('./geth_commands.js');
2017-02-19 17:51:32 +00:00
2017-03-30 11:12:39 +00:00
let BlockchainClient = function(blockchainConfig, client, env) {
if (client === 'geth') {
2017-03-30 13:16:46 +00:00
return new Blockchain({blockchainConfig: blockchainConfig, client: new GethCommands(), env: env});
2017-03-30 11:12:39 +00:00
} else {
throw new Error('unknown client');
}
};
2017-03-30 11:12:39 +00:00
/*eslint complexity: ["error", 22]*/
class Blockchain {
constructor(options) {
this.blockchainConfig = options.blockchainConfig;
this.env = options.env || 'development';
this.client = options.client;
2017-03-30 11:12:39 +00:00
this.config = {
geth_bin: this.blockchainConfig.geth_bin || 'geth',
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,
rpcCorsDomain: this.blockchainConfig.rpcCorsDomain || false,
networkId: this.blockchainConfig.networkId || 12301,
port: this.blockchainConfig.port || 30303,
nodiscover: this.blockchainConfig.nodiscover || false,
mine: this.blockchainConfig.mine || false,
account: this.blockchainConfig.account || {},
whisper: (this.blockchainConfig.whisper === undefined) || this.blockchainConfig.whisper,
maxpeers: ((this.blockchainConfig.maxpeers === 0) ? 0 : (this.blockchainConfig.maxpeers || 25)),
bootnodes: this.blockchainConfig.bootnodes || "",
rpcApi: (this.blockchainConfig.rpcApi || ['eth', 'web3', 'net']),
vmdebug: this.blockchainConfig.vmdebug || false
};
}
2017-03-30 13:16:46 +00:00
runCommand(cmd) {
2017-03-30 11:12:39 +00:00
console.log(("running: " + cmd.underline).green);
return shelljs.exec(cmd);
}
run () {
2017-03-30 11:52:49 +00:00
let self = this;
console.log("===============================================================================".magenta);
console.log("===============================================================================".magenta);
console.log(("Embark Blockchain Using: " + this.client.name.underline).magenta);
console.log("===============================================================================".magenta);
console.log("===============================================================================".magenta);
let address = this.initChainAndGetAddress();
this.client.mainCommand(address, function(cmd) {
shelljs.exec(cmd, {async : true});
});
}
2017-03-30 11:12:39 +00:00
initChainAndGetAddress() {
let address = null, result;
// ensure datadir exists, bypassing the interactive liabilities prompt.
this.datadir = '.embark/' + this.env + '/datadir';
fs.mkdirpSync(this.datadir);
// copy mining script
fs.copySync(fs.embarkPath("js"), ".embark/" + this.env + "/js", {overwrite: true});
// check if an account already exists, create one if not, return address
result = this.runCommand(this.client.listAccountsCommand());
if (result.output === undefined || result.output === '' || result.output.indexOf("Fatal") >= 0) {
console.log("no accounts found".green);
if (this.config.genesisBlock) {
console.log("initializing genesis block".green);
result = this.runCommand(this.client.initGenesisCommmand());
}
result = this.runCommand(this.client.newAccountCommand());
address = result.output.match(/{(\w+)}/)[1];
} else {
console.log("already initialized".green);
address = result.output.match(/{(\w+)}/)[1];
}
2017-03-30 11:12:39 +00:00
return address;
}
2017-03-30 11:12:39 +00:00
}
module.exports = BlockchainClient;