initialize chain

This commit is contained in:
Iuri Matias 2015-08-03 08:03:55 -04:00
parent c0420d74a8
commit ea54f7524d
3 changed files with 54 additions and 0 deletions

34
lib/chain_manager.js Normal file
View File

@ -0,0 +1,34 @@
var fs = require('fs');
var web3 = require('web3');
ChainManager = function() { }
ChainManager.prototype.loadConfigFile = function(filename) {
try {
var obj = JSON.parse(fs.readFileSync(filename));
this.chainManagerConfig = obj;
} catch (e) {
throw new Error("error reading " + filename);
}
return this;
};
ChainManager.prototype.loadConfig = function(config) {
this.chainManagerConfig = config;
return this;
};
ChainManager.prototype.init = function(env, blockchainConfig) {
var config = blockchainConfig.config(env);
web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort));
var chainId = web3.eth.getBlock(0).hash;
if (this.chainManagerConfig[chainId] === undefined) {
this.chainManagerConfig[chainId] = {contracts: []};
}
}
module.exports = ChainManager;

19
test/chain_manager.js Normal file
View File

@ -0,0 +1,19 @@
var ChainManager = require('../lib/chain_manager.js');
var Config = require('../lib/config/config.js');
var Blockchain = require('../lib/blockchain.js');
var assert = require('assert');
describe('embark.chain_manager', function() {
var chainManager = (new ChainManager()).loadConfigFile('./test/support/chain_manager.json');
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml');
describe('#init', function() {
chainManager.init('development', blockchainConfig);
it('should initialize chain', function() {
assert.equal(chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] != undefined, true);
assert.equal(chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'].contracts.length, 0);
});
});
});

View File

@ -0,0 +1 @@
{}