embark-area-51/lib/blockchain.js

183 lines
4.7 KiB
JavaScript

var mkdirp = require('mkdirp');
var wrench = require('wrench');
var Blockchain = function() {
};
Blockchain.prototype.run = function(options) {
this.env = env;
//var address = this.get_address();
var address = this.initChainAndGetAddress(options);
console.log("running: " + this.run_command(address, false));
exec(this.run_command(address, false));
//this.runChain(address, options);
};
Blockchain.prototype.initChainAndGetAddress = function(options) {
var address = null, result;
// ensure datadir exists, bypassing the interactive liabilities prompt.
this.datadir = '.embark/development/datadir';
mkdirp.sync(this.datadir);
wrench.copyDirSyncRecursive(__dirname + "/../js", ".embark/development/js", {forceDelete: true});
console.log("running: " + this.list_command());
result = exec(this.list_command());
if (result.output === undefined || result.output === '' || result.output.indexOf("Fatal") >= 0) {
//if (config.genesisBlock !== void 0) {
console.log("initializing genesis block");
console.log("running: " + this.generate_genesis_init_command());
result = exec(this.generate_genesis_init_command());
//}
console.log("running: " + this.init_command());
result = exec(this.init_command());
address = result.output.match(/{(\w+)}/)[1];
} else {
console.log("=== already initialized");
address = result.output.match(/{(\w+)}/)[1];
}
return address;
};
Blockchain.prototype.generate_genesis_init_command = function() {
//var config = this.config;
var cmd = "geth ";
//if (config.datadir !== "default") {
cmd += "--datadir=\"" + this.datadir + "\" ";
//}
//cmd += "init \"" + config.genesisBlock + "\" ";
cmd += "init \"" + "./config/development/genesis.json" + "\" ";
return cmd;
};
Blockchain.prototype.init_command = function() {
return this.generate_init_command() + "account new ";
};
Blockchain.prototype.generate_init_command = function() {
//var config = this.config;
//var address = config.account.address;
var cmd = "geth ";
//if (config.datadir !== "default") {
cmd += "--datadir=\"" + this.datadir + "\" ";
//}
//if (config.account.password !== void 0) {
cmd += "--password " + './config/development/password' + " ";
//}
return cmd;
};
Blockchain.prototype.generate_basic_command = function() {
var config = this.config;
//var address = config.account.address;
var cmd = "geth ";
var rpc_api = ['eth', 'web3'];
//if (config.datadir !== "default") {
cmd += "--datadir=\"" + this.datadir + "\" ";
//}
//if (config.testnet) {
// cmd += "--testnet ";
//}
//if (config.account.password !== void 0) {
//cmd += "--password " + config.account.password + " ";
cmd += "--password " + "./config/development/password" + " ";
//}
cmd += "--port " + "30303" + " ";
cmd += "--rpc ";
cmd += "--rpcport " + 8545 + " ";
cmd += "--rpcaddr " + "localhost" + " ";
cmd += "--networkid " + "12301" + " ";
cmd += "--rpccorsdomain=\"" + "*" + "\" ";
//cmd += "--port " + config.port + " ";
//cmd += "--rpc ";
//cmd += "--rpcport " + config.rpcPort + " ";
//cmd += "--rpcaddr " + config.rpcHost + " ";
//cmd += "--networkid " + config.networkId + " ";
//cmd += "--rpccorsdomain=\"" + config.rpcWhitelist + "\" ";
//if (config.minerthreads !== void 0) {
// cmd += "--minerthreads \"" + config.minerthreads + "\" ";
//}
//if(config.mine_when_needed || config.mine)
cmd += "--mine ";
//if (config.whisper) {
cmd += "--shh ";
rpc_api.push('shh');
//}
cmd += '--rpcapi "' + rpc_api.join(',') + '" ';
//TODO: this should be configurable
//cmd += "--maxpeers " + config.maxPeers + " ";
return cmd;
};
Blockchain.prototype.list_command = function() {
return this.generate_init_command() + "account list ";
};
Blockchain.prototype.run_command = function(address, use_tmp) {
var cmd = this.generate_basic_command();
//var config = this.config;
//if (address !== void 0) {
cmd += "--unlock=" + address + " ";
//}
cmd += "js .embark/development/js/mine.js";
//if (config.mine_when_needed) {
// if (use_tmp) {
// cmd += "js /tmp/js/mine.js";
// }
// else {
// cmd += "js node_modules/embark-framework/js/mine.js";
// }
//}
return cmd;
};
Blockchain.prototype.startChain = function(use_tmp) {
var address = this.get_address();
console.log("running: " + this.run_command(address, use_tmp));
exec(this.run_command(address, use_tmp));
};
Blockchain.prototype.getStartChainCommand = function(use_tmp) {
var address = this.get_address();
return this.run_command(address, use_tmp);
};
var BlockchainClient = function(client) {
if (client === 'geth') {
return new Blockchain();
} else {
throw new Error('unknown client');
}
};
module.exports = BlockchainClient;