refactor blockchain module; support more options
This commit is contained in:
parent
2eef8b46ae
commit
4af36ce1e4
|
@ -3,16 +3,12 @@
|
||||||
"name": "development",
|
"name": "development",
|
||||||
"networkId": "",
|
"networkId": "",
|
||||||
"contracts": {
|
"contracts": {
|
||||||
"288f087742d513019f59539bbf4f07b352e14327e0e7b8ed4bc7c33e34fbc192": {
|
|
||||||
"address": "0xab47369d82950224a1698d4a856cfa175ba9cf84",
|
|
||||||
"name": "SimpleStorage"
|
|
||||||
},
|
|
||||||
"5a52b16abb7dfccfc675550d0daba1683a3cf7e80fb5f0e199301f7dd57237fb": {
|
"5a52b16abb7dfccfc675550d0daba1683a3cf7e80fb5f0e199301f7dd57237fb": {
|
||||||
"address": "0xbd2b5c34a2a85d3ae5e903e0015a07868c0d97c3",
|
"address": "0x94ba5dc1241a41a13a2c62e7a65c7c455dae5380",
|
||||||
"name": "SimpleStorage"
|
"name": "SimpleStorage"
|
||||||
},
|
},
|
||||||
"708fa6b699f419627ab3c4c2d9c82f8f1a6fab03c122d0a9ee55d2d0d0ad1e4b": {
|
"708fa6b699f419627ab3c4c2d9c82f8f1a6fab03c122d0a9ee55d2d0d0ad1e4b": {
|
||||||
"address": "0x7a72b8be758afe7d3e421e7d32c81c605fec333d",
|
"address": "0x57df57939ddf112aeaa4232d9e2ad20de40e9fb7",
|
||||||
"name": "token"
|
"name": "token"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"development": {
|
||||||
|
"networkType": "custom",
|
||||||
|
"genesisBlock": "config/development/genesis.json",
|
||||||
|
"datadir": ".embark/development/datadir",
|
||||||
|
"mineWhenNeeded": true,
|
||||||
|
"nodiscover": true,
|
||||||
|
"rpcHost": "localhost",
|
||||||
|
"rpcPort": 8545,
|
||||||
|
"account": {
|
||||||
|
"password": "config/development/password"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"testnet": {
|
||||||
|
"rpcHost": "localhost",
|
||||||
|
"rpcPort": 8545,
|
||||||
|
"networkType": "testnet"
|
||||||
|
},
|
||||||
|
"livenet": {
|
||||||
|
"networkType": "livenet"
|
||||||
|
},
|
||||||
|
"privatenet": {
|
||||||
|
"networkType": "custom",
|
||||||
|
"rpcHost": "localhost",
|
||||||
|
"rpcPort": 8545,
|
||||||
|
"datadir": "yourdatadir",
|
||||||
|
"networkId": "123",
|
||||||
|
"nodes": []
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,180 +1,77 @@
|
||||||
var mkdirp = require('mkdirp');
|
var mkdirp = require('mkdirp');
|
||||||
var wrench = require('wrench');
|
var wrench = require('wrench');
|
||||||
|
var colors = require('colors');
|
||||||
|
var GethCommands = require('./geth_commands.js');
|
||||||
|
|
||||||
var Blockchain = function(blockchainConfig) {
|
var Blockchain = function(blockchainConfig, Client) {
|
||||||
this.blockchainConfig = blockchainConfig;
|
this.blockchainConfig = blockchainConfig;
|
||||||
|
|
||||||
|
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});
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockchain.prototype.run = function(options) {
|
Blockchain.prototype.runCommand = function(cmd) {
|
||||||
this.env = env;
|
console.log(("running: " + cmd.underline).green);
|
||||||
|
return exec(cmd);
|
||||||
//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) {
|
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);
|
||||||
|
};
|
||||||
|
|
||||||
|
Blockchain.prototype.initChainAndGetAddress = function() {
|
||||||
var address = null, result;
|
var address = null, result;
|
||||||
|
|
||||||
// ensure datadir exists, bypassing the interactive liabilities prompt.
|
// ensure datadir exists, bypassing the interactive liabilities prompt.
|
||||||
this.datadir = '.embark/development/datadir';
|
this.datadir = '.embark/development/datadir';
|
||||||
mkdirp.sync(this.datadir);
|
mkdirp.sync(this.datadir);
|
||||||
|
|
||||||
|
// copy mining script
|
||||||
wrench.copyDirSyncRecursive(__dirname + "/../js", ".embark/development/js", {forceDelete: true});
|
wrench.copyDirSyncRecursive(__dirname + "/../js", ".embark/development/js", {forceDelete: true});
|
||||||
|
|
||||||
console.log("running: " + this.list_command());
|
// check if an account already exists, create one if not, return address
|
||||||
result = exec(this.list_command());
|
result = this.runCommand(this.client.listAccountsCommand());
|
||||||
|
|
||||||
if (result.output === undefined || result.output === '' || result.output.indexOf("Fatal") >= 0) {
|
if (result.output === undefined || result.output === '' || result.output.indexOf("Fatal") >= 0) {
|
||||||
//if (config.genesisBlock !== void 0) {
|
console.log("no accounts found".green);
|
||||||
console.log("initializing genesis block");
|
if (this.config.genesisBlock) {
|
||||||
console.log("running: " + this.generate_genesis_init_command());
|
console.log("initializing genesis block".green);
|
||||||
result = exec(this.generate_genesis_init_command());
|
result = this.runCommand(this.client.initGenesisCommmand());
|
||||||
//}
|
}
|
||||||
|
|
||||||
console.log("running: " + this.init_command());
|
result = this.runCommand(this.client.newAccountCommand());
|
||||||
result = exec(this.init_command());
|
|
||||||
address = result.output.match(/{(\w+)}/)[1];
|
address = result.output.match(/{(\w+)}/)[1];
|
||||||
} else {
|
} else {
|
||||||
console.log("=== already initialized");
|
console.log("already initialized".green);
|
||||||
address = result.output.match(/{(\w+)}/)[1];
|
address = result.output.match(/{(\w+)}/)[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return address;
|
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 " + this.blockchainConfig.rpcPort + " ";
|
|
||||||
cmd += "--rpcaddr " + this.blockchainConfig.rpcHost + " ";
|
|
||||||
cmd += "--networkid " + "12301" + " ";
|
|
||||||
//cmd += "--rpccorsdomain=\"" + "localhost" + "\" ";
|
|
||||||
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(blockchainConfig, client) {
|
var BlockchainClient = function(blockchainConfig, client) {
|
||||||
if (client === 'geth') {
|
if (client === 'geth') {
|
||||||
return new Blockchain(blockchainConfig);
|
return new Blockchain(blockchainConfig, GethCommands);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('unknown client');
|
throw new Error('unknown client');
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,8 @@ Config.prototype.reloadConfig = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
Config.prototype.loadBlockchainConfigFile = function() {
|
Config.prototype.loadBlockchainConfigFile = function() {
|
||||||
var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + this.env + "/blockchain.json"))[this.env];
|
//var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + this.env + "/blockchain.json"))[this.env];
|
||||||
|
var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + "blockchain.json"))[this.env];
|
||||||
this.blockchainConfig = defaultBlockchainConfig;
|
this.blockchainConfig = defaultBlockchainConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,127 @@
|
||||||
|
|
||||||
|
var GethCommands = function(options) {
|
||||||
|
this.config = options.config;
|
||||||
|
this.name = "Go-Ethereum (https://github.com/ethereum/go-ethereum)";
|
||||||
|
};
|
||||||
|
|
||||||
|
GethCommands.prototype.initGenesisCommmand = function() {
|
||||||
|
var config = this.config;
|
||||||
|
var cmd = "geth ";
|
||||||
|
|
||||||
|
if (config.networkType === 'testnet') {
|
||||||
|
cmd += "--testnet ";
|
||||||
|
} else if (config.networkType === 'olympic') {
|
||||||
|
cmd += "--olympic ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.datadir) {
|
||||||
|
cmd += "--datadir=\"" + config.datadir + "\" ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.genesisBlock) {
|
||||||
|
cmd += "init \"" + config.genesisBlock + "\" ";
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
};
|
||||||
|
|
||||||
|
GethCommands.prototype.newAccountCommand = function() {
|
||||||
|
var config = this.config;
|
||||||
|
var cmd = "geth ";
|
||||||
|
|
||||||
|
if (config.networkType === 'testnet') {
|
||||||
|
cmd += "--testnet ";
|
||||||
|
} else if (config.networkType === 'olympic') {
|
||||||
|
cmd += "--olympic ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.datadir) {
|
||||||
|
cmd += "--datadir=\"" + config.datadir + "\" ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.account && config.account.password) {
|
||||||
|
cmd += "--password " + config.account.password + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd + "account new ";
|
||||||
|
};
|
||||||
|
|
||||||
|
GethCommands.prototype.listAccountsCommand = function() {
|
||||||
|
var config = this.config;
|
||||||
|
var cmd = "geth ";
|
||||||
|
|
||||||
|
if (config.networkType === 'testnet') {
|
||||||
|
cmd += "--testnet ";
|
||||||
|
} else if (config.networkType === 'olympic') {
|
||||||
|
cmd += "--olympic ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.datadir) {
|
||||||
|
cmd += "--datadir=\"" + config.datadir + "\" ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.account && config.account.password) {
|
||||||
|
cmd += "--password " + config.account.password + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd + "account list ";
|
||||||
|
};
|
||||||
|
|
||||||
|
GethCommands.prototype.mainCommand = function(address) {
|
||||||
|
var config = this.config;
|
||||||
|
var cmd = "geth ";
|
||||||
|
var rpc_api = ['eth', 'web3'];
|
||||||
|
|
||||||
|
if (config.datadir) {
|
||||||
|
cmd += "--datadir=\"" + config.datadir + "\" ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.networkType === 'testnet') {
|
||||||
|
cmd += "--testnet ";
|
||||||
|
} else if (config.networkType === 'olympic') {
|
||||||
|
cmd += "--olympic ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.networkType !== 'custom') {
|
||||||
|
cmd += "--networkid " + config.networkId + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.account && config.account.password) {
|
||||||
|
cmd += "--password " + config.account.password + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd += "--port " + "30303" + " ";
|
||||||
|
cmd += "--rpc ";
|
||||||
|
cmd += "--rpcport " + config.rpcPort + " ";
|
||||||
|
cmd += "--rpcaddr " + config.rpcHost + " ";
|
||||||
|
//cmd += "--rpccorsdomain=\"" + "localhost" + "\" ";
|
||||||
|
cmd += "--rpccorsdomain=\"" + "*" + "\" ";
|
||||||
|
//cmd += "--rpccorsdomain=\"" + config.rpcWhitelist + "\" ";
|
||||||
|
|
||||||
|
//"nodiscover": true,
|
||||||
|
|
||||||
|
if (config.mineWhenNeeded || config.mine) {
|
||||||
|
cmd += "--mine ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.whisper) {
|
||||||
|
cmd += "--shh ";
|
||||||
|
rpc_api.push('shh');
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd += '--rpcapi "' + rpc_api.join(',') + '" ';
|
||||||
|
|
||||||
|
var accountAddress = config.account.address || address;
|
||||||
|
if (accountAddress) {
|
||||||
|
cmd += "--unlock=" + accountAddress + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.mineWhenNeeded) {
|
||||||
|
cmd += "js .embark/development/js/mine.js";
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = GethCommands;
|
||||||
|
|
Loading…
Reference in New Issue