embark/lib/cmds/blockchain/blockchain.js

197 lines
7.4 KiB
JavaScript
Raw Normal View History

2018-05-22 18:13:56 +00:00
const async = require('async');
const shelljs = require('shelljs');
2018-05-22 18:13:56 +00:00
const fs = require('../../core/fs.js');
2017-02-19 17:51:32 +00:00
2018-05-22 18:13:56 +00:00
const GethCommands = require('./geth_commands.js');
2017-02-19 17:51:32 +00:00
2018-01-24 00:36:02 +00:00
/*eslint complexity: ["error", 35]*/
2017-03-31 11:34:43 +00:00
var Blockchain = function(options) {
this.blockchainConfig = options.blockchainConfig;
this.env = options.env || 'development';
this.client = options.client;
this.isDev = options.isDev;
2018-05-22 18:13:56 +00:00
this.onReadyCallback = options.onReadyCallback;
2017-03-31 11:34:43 +00:00
if ((this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') && this.env !== 'development') {
2018-05-08 21:49:46 +00:00
console.log("===> " + __("warning: running default config on a non-development environment"));
}
2017-03-31 11:34:43 +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 || 1337,
2017-03-31 11:34:43 +00:00
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']),
wsRPC: (this.blockchainConfig.wsRPC === undefined) || this.blockchainConfig.wsRPC,
wsHost: this.blockchainConfig.wsHost || 'localhost',
wsPort: this.blockchainConfig.wsPort || 8546,
wsOrigins: this.blockchainConfig.wsOrigins || false,
wsApi: (this.blockchainConfig.wsApi || ['eth', 'web3', 'net', 'shh']),
vmdebug: this.blockchainConfig.vmdebug || false,
targetGasLimit: this.blockchainConfig.targetGasLimit || false,
light: this.blockchainConfig.light || false,
2018-05-22 13:11:38 +00:00
fast: this.blockchainConfig.fast || false,
verbosity: this.blockchainConfig.verbosity
2017-03-31 11:34:43 +00:00
};
if (this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') {
this.config.account = {};
this.config.account.password = fs.embarkPath("templates/boilerplate/config/development/password");
this.config.genesisBlock = fs.embarkPath("templates/boilerplate/config/development/genesis.json");
this.config.datadir = fs.embarkPath(".embark/development/datadir");
}
2018-05-09 13:17:48 +00:00
this.client = new options.client({config: this.config, env: this.env, isDev: this.isDev});
};
2018-05-22 18:13:56 +00:00
Blockchain.prototype.runCommand = function(cmd, options, callback) {
2018-05-08 21:49:46 +00:00
console.log(__("running: %s", cmd.underline).green);
2018-05-22 18:13:56 +00:00
if (this.blockchainConfig.silent) {
options.silent = true;
}
return shelljs.exec(cmd, options, callback);
2017-03-31 11:34:43 +00:00
};
2017-03-31 11:34:43 +00:00
Blockchain.prototype.run = function() {
var self = this;
console.log("===============================================================================".magenta);
console.log("===============================================================================".magenta);
2018-05-08 21:49:46 +00:00
console.log(__("Embark Blockchain Using: %s", this.client.name.underline).magenta);
2017-03-31 11:34:43 +00:00
console.log("===============================================================================".magenta);
console.log("===============================================================================".magenta);
2018-05-22 18:13:56 +00:00
2018-05-08 20:25:48 +00:00
let address = '';
2018-05-22 18:13:56 +00:00
async.waterfall([
function checkInstallation(next) {
self.isClientInstalled((err) => {
if (err) {
console.log(__("could not find {{geth_bin}} command; is {{client_name}} installed or in the PATH?", {geth_bin: this.config.geth_bin, client_name: this.client.name}).green);
return next(err);
}
next();
});
},
function init(next) {
if (!self.isDev) {
return self.initChainAndGetAddress((err, addr) => {
address = addr;
next(err);
});
}
next();
},
function getMainCommand(next) {
self.client.mainCommand(address, function(cmd) {
next(null, cmd);
});
}
], function (err, cmd) {
if (err) {
console.error(err);
return;
}
const child = self.runCommand(cmd, {}, (err, stdout, _stderr) => {
if (err && self.env === 'development' && stdout.indexOf('Failed to unlock') > 0) {
console.warn('\n' + __('Development blockchain has changed to use the --dev option.').yellow);
console.warn(__('You can reset your workspace to fix the problem with').yellow + ' embark reset'.cyan);
console.warn(__('Otherwise, you can change your data directory in blockchain.json (datadir)').yellow);
}
});
if (self.onReadyCallback) {
// Geth logs appear in stderr somehow
child.stderr.on('data', (data) => {
if (!self.readyCalled && data.indexOf('Mapped network port') > -1) {
self.readyCalled = true;
return self.onReadyCallback();
}
console.log('Geth:' + data);
});
}
2017-03-31 11:34:43 +00:00
});
};
2018-05-22 18:13:56 +00:00
Blockchain.prototype.isClientInstalled = function(callback) {
let versionCmd = this.client.determineVersion();
2018-05-22 18:13:56 +00:00
this.runCommand(versionCmd, {}, (err, stdout, stderr) => {
if (err || stderr || !stdout || stdout.indexOf("not found") >= 0) {
return callback('Geth not found');
}
callback();
});
};
2018-05-22 18:13:56 +00:00
Blockchain.prototype.initChainAndGetAddress = function(callback) {
const self = this;
2018-05-22 19:36:31 +00:00
let address = null;
2017-03-31 11:34:43 +00:00
// ensure datadir exists, bypassing the interactive liabilities prompt.
2018-05-22 18:13:56 +00:00
self.datadir = '.embark/' + self.env + '/datadir';
async.waterfall([
function makeDir(next) {
2018-05-22 19:36:31 +00:00
fs.mkdirp(self.datadir, (err, _result) => {
next(err);
});
2018-05-22 18:13:56 +00:00
},
function copy(next) {
// copy mining script
fs.copy(fs.embarkPath("js"), ".embark/" + self.env + "/js", {overwrite: true}, next);
},
function listAccounts(next) {
self.runCommand(self.client.listAccountsCommand(), {}, (err, stdout, stderr) => {
if (err || stderr || stdout === undefined || stdout.match(/{(\w+)}/) === null || stdout.indexOf("Fatal") >= 0) {
console.log(__("no accounts found").green);
return next();
}
2018-05-22 19:36:31 +00:00
console.log(__("already initialized").green);
address = stdout.match(/{(\w+)}/)[1];
next();
2018-05-22 18:13:56 +00:00
});
},
function genesisBlock(next) {
if (!self.config.genesisBlock) {
return next();
}
2018-05-08 21:49:46 +00:00
console.log(__("initializing genesis block").green);
2018-05-22 18:13:56 +00:00
self.runCommand(self.client.initGenesisCommmand(), {}, (err, _stdout, _stderr) => {
next(err);
});
},
function newAccount(next) {
2018-05-22 19:36:31 +00:00
self.runCommand(self.client.newAccountCommand(), {}, (err, stdout, _stderr) => {
2018-05-22 18:13:56 +00:00
if (err) {
return next(err);
}
address = stdout.match(/{(\w+)}/)[1];
2018-05-22 19:36:31 +00:00
next();
2018-05-22 18:13:56 +00:00
});
2017-03-30 11:12:39 +00:00
}
2018-05-22 18:13:56 +00:00
], (err) => {
callback(err, address);
});
2017-03-31 11:34:43 +00:00
};
2018-05-22 18:13:56 +00:00
var BlockchainClient = function(blockchainConfig, client, env, isDev, onReadyCallback) {
2018-05-18 17:27:01 +00:00
// TODO add other clients at some point
2017-03-31 11:34:43 +00:00
if (client === 'geth') {
2018-05-22 18:13:56 +00:00
return new Blockchain({blockchainConfig, client: GethCommands, env, isDev, onReadyCallback});
2017-03-31 11:34:43 +00:00
} else {
throw new Error('unknown client');
}
2017-03-31 11:34:43 +00:00
};
module.exports = BlockchainClient;