refactor blockchain config

This commit is contained in:
Iuri Matias 2015-06-28 09:51:58 -04:00
parent 12f84bf403
commit 3b5c45a29f
7 changed files with 233 additions and 63 deletions

View File

@ -1,65 +1,35 @@
var Deploy;
var readYaml = require('read-yaml');
var Config = require('./config/config.js');
startChain = function(env) {
try {
blockchainConfig = readYaml.sync("config/blockchain.yml");
} catch (_error) {
exception = _error;
console.log("==== error reading config/blockchain.yml");
console.log(exception);
}
config = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
rpcHost = blockchainConfig[env].rpc_host;
rpcPort = blockchainConfig[env].rpc_port;
rpcWhitelist = blockchainConfig[env].rpc_whitelist;
minerthreads = blockchainConfig[env].minerthreads;
datadir = blockchainConfig[env].datadir;
networkId = blockchainConfig[env].network_id || Math.floor((Math.random() * 100000) + 1000);
port = blockchainConfig[env].port || "30303";
console_toggle = blockchainConfig[env].console || false;
mine_when_needed = blockchainConfig[env].mine_when_needed || false;
account = blockchainConfig[env].account;
address = account.address;
address = config.account.address;
cmd = "geth ";
if (datadir !== "default") {
cmd += "--datadir=\"" + datadir + "\" ";
cmd += "--logfile=\"" + datadir + ".log\" ";
if (config.datadir !== "default") {
cmd += "--datadir=\"" + config.datadir + "\" ";
cmd += "--logfile=\"" + config.datadir + ".log\" ";
}
cmd += "--port " + port + " ";
cmd += "--port " + config.port + " ";
cmd += "--rpc ";
cmd += "--rpcport " + config.rpcPort + " ";
cmd += "--networkid " + config.networkId + " ";
cmd += "--rpccorsdomain \"" + config.rpcWhitelist + "\" ";
cmd += "--rpcport " + rpcPort + " ";
cmd += "--networkid " + networkId + " ";
cmd += "--rpccorsdomain \"" + rpcWhitelist + "\" ";
if (minerthreads !== void 0) {
cmd += "--minerthreads \"" + minerthreads + "\" ";
if (config.minerthreads !== void 0) {
cmd += "--minerthreads \"" + config.minerthreads + "\" ";
}
cmd += "--mine ";
if (account.password !== void 0) {
cmd += "--password " + account.password + " ";
if (config.account.password !== void 0) {
cmd += "--password " + config.account.password + " ";
}
if (account.init) {
if (config.account.init) {
console.log("=== initializating account");
console.log("running: " + cmd + " account list");
result = exec(cmd + "account list");
@ -79,11 +49,11 @@ startChain = function(env) {
cmd += "--unlock " + address + " ";
}
if (console_toggle) {
if (config.console_toggle) {
cmd += "console";
}
if (mine_when_needed) {
if (config.mine_when_needed) {
cmd += "js node_modules/embark-framework/js/mine.js";
}

60
lib/config/config.js Normal file
View File

@ -0,0 +1,60 @@
var readYaml = require('read-yaml');
// read config/blockchain.yml
//* config can be an argument
BlockchainConfig = function() {};
BlockchainConfig.prototype.loadConfigFile = function(filename) {
try {
this.blockchainConfig = readYaml.sync(filename);
} catch (e) {
throw new Error("error reading " + filename);
}
return this;
}
BlockchainConfig.prototype.loadConfig = function(config) {
this.blockchainConfig = config;
return this;
}
BlockchainConfig.prototype.config = function(env) {
if (this.blockchainConfig === null) {
throw new Error("no blockchain config found");
}
var config = this.blockchainConfig[env || "development"];
var networkId;
if (config.network_id === undefined) {
networkId = Math.floor((Math.random() * 100000) + 1000)
}
else {
networkId = config.network_id
}
config = {
rpcHost: config.rpc_host,
rpcPort: config.rpc_port,
gasLimit: config.gas_limit || 500000,
gasPrice: config.gas_price || 10000000000000,
rpcWhitelist: config.rpc_whitelist,
minerthreads: config.minerthreads,
datadir: config.datadir,
networkId: networkId,
port: config.port || "30303",
console_toggle: config.console || false,
mine_when_needed: config.mine_when_needed || false,
account: config.account
}
return config;
}
Config = {
Blockchain: BlockchainConfig
}
module.exports = Config

View File

@ -2,35 +2,28 @@ var web3 = require('web3');
var fs = require('fs');
var grunt = require('grunt');
var readYaml = require('read-yaml');
var Config = require('./config/config.js');
deployContracts = function(env, contractFiles, destFile) {
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
blockchainConfig = readYaml.sync("config/blockchain.yml");
blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
contractsConfig = readYaml.sync("config/contracts.yml")[env || "development"];
rpcHost = blockchainConfig[env || "development"].rpc_host;
rpcPort = blockchainConfig[env || "development"].rpc_port;
gasLimit = blockchainConfig[env || "development"].gas_limit || 500000;
gasPrice = blockchainConfig[env || "development"].gas_price || 10000000000000;
try {
web3.setProvider(new web3.providers.HttpProvider("http://" + rpcHost + ":" + rpcPort));
web3.setProvider(new web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort));
primaryAddress = web3.eth.coinbase;
web3.eth.defaultAccount = primaryAddress;
} catch (_error) {
e = _error;
console.log("==== can't connect to " + rpcHost + ":" + rpcPort + " check if an ethereum node is running");
console.log("==== can't connect to " + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + " check if an ethereum node is running");
exit;
}
console.log("address is : " + primaryAddress);
result = "web3.setProvider(new web3.providers.HttpProvider('http://" + rpcHost + ":" + rpcPort + "'));";
result = "web3.setProvider(new web3.providers.HttpProvider('http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + "'));";
result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
@ -95,8 +88,8 @@ deployContracts = function(env, contractFiles, destFile) {
for (k = 0, len2 = all_contracts.length; k < len2; k++) {
className = all_contracts[k];
contract = contractDB[className];
contractGasLimit = (contractsConfig != null ? (ref1 = contractsConfig[className]) != null ? ref1.gasLimit : void 0 : void 0) || gasLimit;
contractGasPrice = (contractsConfig != null ? (ref2 = contractsConfig[className]) != null ? ref2.gasPrice : void 0 : void 0) || gasPrice;
contractGasLimit = (contractsConfig != null ? (ref1 = contractsConfig[className]) != null ? ref1.gasLimit : void 0 : void 0) || blockchainConfig.gasLimit;
contractGasPrice = (contractsConfig != null ? (ref2 = contractsConfig[className]) != null ? ref2.gasPrice : void 0 : void 0) || blockchainConfig.gasPrice;
args = (contractsConfig != null ? (ref3 = contractsConfig[className]) != null ? ref3.args : void 0 : void 0) || [];
contractObject = web3.eth.contract(contract.info.abiDefinition);
realArgs = [];

View File

@ -17,6 +17,7 @@ embark.Tests = require('./test.js');
embark.Blockchain = require('./blockchain.js');
embark.Deploy = require('./deploy.js');
embark.Release = require('./ipfs.js');
embark.Config = require('./config/config.js');
module.exports = embark;

125
test/config.js Normal file
View File

@ -0,0 +1,125 @@
var Config = require('../lib/config/config.js');
var assert = require('assert');
describe('embark.config.blockchain', function() {
var blockchainConfig = new Config.Blockchain();
describe('#loadConfigFile', function() {
it('should read and load yml file', function() {
blockchainConfig.loadConfigFile('test/support/blockchain.yml');
assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('development'), true)
assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('staging'), true)
});
it('should throw exception reading invalid file', function() {
assert.throws(function() { blockchainConfig.loadConfigFile('test/support/invalid.yml') }, Error);
});
});
describe('#loadConfig', function() {
it('should load config', function() {
var hsh = {
development: {},
staging: {}
};
blockchainConfig.loadConfig(hsh);
assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('development'), true)
assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('staging'), true)
});
});
describe('#config', function() {
it('should load environment', function() {
var hsh = {
development: {
rpc_host: 'localhost',
rpc_port: 8101,
rpc_whitelist: "*",
network_id: 0,
minerthreads: 1,
datadir: '/tmp/embark',
mine_when_needed: true,
gas_limit: 123,
gas_price: 100,
console: false,
account: {
init: true,
password: 'config/password'
}
},
staging: {}
};
blockchainConfig.loadConfig(hsh);
assert.deepEqual(blockchainConfig.config('development'), {
rpcHost: 'localhost',
rpcPort: 8101,
gasLimit: 123,
gasPrice: 100,
rpcWhitelist: "*",
minerthreads: 1,
datadir: '/tmp/embark',
networkId: 0,
port: "30303",
console_toggle: false,
mine_when_needed: true,
account: {
init: true,
password: 'config/password'
}
})
});
it('should return defaults', function() {
var hsh = {
development: {
rpc_host: 'localhost',
rpc_port: 8101,
rpc_whitelist: "*",
network_id: 0,
minerthreads: 1,
datadir: '/tmp/embark',
mine_when_needed: true,
console: false,
account: {
init: true,
password: 'config/password'
}
},
staging: {}
};
blockchainConfig.loadConfig(hsh);
assert.deepEqual(blockchainConfig.config('development'), {
rpcHost: 'localhost',
rpcPort: 8101,
gasLimit: 500000,
gasPrice: 10000000000000,
rpcWhitelist: "*",
minerthreads: 1,
datadir: '/tmp/embark',
networkId: 0,
port: "30303",
console_toggle: false,
mine_when_needed: true,
account: {
init: true,
password: 'config/password'
}
})
});
it('should load environment', function() {
var blockchainConfig = new Config.Blockchain();
assert.throws(function() { blockchainConfig.config('development') }, Error);
});
});
});

View File

@ -0,0 +1,23 @@
development:
rpc_host: localhost
rpc_port: 8101
rpc_whitelist: "*"
minerthreads: 1
datadir: /tmp/embark
mine_when_needed: true
gas_limit: 500000
gas_price: 10000000000000
console: false
account:
init: true
password: config/password
staging:
rpc_host: localhost
rpc_port: 8101
rpc_whitelist: "*"
datadir: default
network_id: 0
console: true
account:
init: false
address:

View File

@ -1,2 +0,0 @@
EmbarkSpec = require('../lib/test.js').Tests;