2016-08-21 14:42:42 +00:00
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
var wrench = require('wrench');
|
2016-09-25 06:23:33 +00:00
|
|
|
var colors = require('colors');
|
|
|
|
var GethCommands = require('./geth_commands.js');
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
var Blockchain = function(blockchainConfig, Client) {
|
2016-08-26 11:51:58 +00:00
|
|
|
this.blockchainConfig = blockchainConfig;
|
2016-09-25 06:23:33 +00:00
|
|
|
|
|
|
|
this.config = {
|
|
|
|
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,
|
|
|
|
networkId: this.blockchainConfig.networkId || 12301,
|
|
|
|
port: this.blockchainConfig.port || 30303,
|
|
|
|
nodiscover: this.blockchainConfig.nodiscover || false,
|
|
|
|
mine: this.blockchainConfig.mine || false,
|
|
|
|
whisper: this.blockchainConfig.whisper || true,
|
|
|
|
account: this.blockchainConfig.account || {}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.client = new Client({config: this.config});
|
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);
|
|
|
|
return exec(cmd);
|
|
|
|
};
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
Blockchain.prototype.run = function() {
|
|
|
|
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();
|
|
|
|
var mainCommand = this.client.mainCommand(address);
|
|
|
|
this.runCommand(mainCommand);
|
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.
|
|
|
|
this.datadir = '.embark/development/datadir';
|
|
|
|
mkdirp.sync(this.datadir);
|
|
|
|
|
2016-09-25 06:23:33 +00:00
|
|
|
// copy mining script
|
|
|
|
wrench.copyDirSyncRecursive(__dirname + "/../js", ".embark/development/js", {forceDelete: 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;
|
|
|
|
};
|
|
|
|
|
2016-08-26 11:51:58 +00:00
|
|
|
var BlockchainClient = function(blockchainConfig, client) {
|
2016-08-21 14:42:42 +00:00
|
|
|
if (client === 'geth') {
|
2016-09-25 06:23:33 +00:00
|
|
|
return new Blockchain(blockchainConfig, GethCommands);
|
2016-08-21 14:42:42 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('unknown client');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = BlockchainClient;
|
|
|
|
|