save chain; add chain manager to deployment

This commit is contained in:
Iuri Matias 2015-08-03 21:54:39 -04:00
parent 56e59d814b
commit f8db3c2920
7 changed files with 49 additions and 16 deletions

View File

@ -15,12 +15,19 @@ var run = function(cmd) {
} }
var deploy = function(env, embarkConfig) { var deploy = function(env, embarkConfig) {
contractFiles = grunt.file.expand(embarkConfig.contracts); var contractFiles = grunt.file.expand(embarkConfig.contracts);
destFile = embarkConfig.output var destFile = embarkConfig.output;
Embark.init() var chainFile = embarkConfig.chains;
Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig)
Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig) Embark.init();
abi = Embark.deployContracts(env, contractFiles, destFile) Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig);
Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig);
if (chainFile === undefined) {
chainFile = './chains.json';
}
abi = Embark.deployContracts(env, contractFiles, destFile, chainFile);
grunt.file.write(destFile, abi); grunt.file.write(destFile, abi);
} }

View File

@ -4,11 +4,13 @@ var sha3_256 = require('js-sha3').sha3_256;
ChainManager = function() { ChainManager = function() {
this.currentChain = {}; this.currentChain = {};
this.file = "";
} }
ChainManager.prototype.loadConfigFile = function(filename) { ChainManager.prototype.loadConfigFile = function(filename) {
try { try {
var obj = JSON.parse(fs.readFileSync(filename)); var obj = JSON.parse(fs.readFileSync(filename));
this.file = filename;
this.chainManagerConfig = obj; this.chainManagerConfig = obj;
} catch (e) { } catch (e) {
throw new Error("error reading " + filename); throw new Error("error reading " + filename);
@ -21,9 +23,7 @@ ChainManager.prototype.loadConfig = function(config) {
return this; return this;
}; };
ChainManager.prototype.init = function(env, blockchainConfig) { ChainManager.prototype.init = function(env, config) {
var config = blockchainConfig.config(env);
web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort)); web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort));
var chainId = web3.eth.getBlock(0).hash; var chainId = web3.eth.getBlock(0).hash;
@ -46,5 +46,9 @@ ChainManager.prototype.getContract = function(code) {
return this.currentChain.contracts[sha3_256(code)]; return this.currentChain.contracts[sha3_256(code)];
} }
ChainManager.prototype.save = function() {
fs.writeFileSync(this.file, JSON.stringify(this.chainManagerConfig));
}
module.exports = ChainManager; module.exports = ChainManager;

View File

@ -10,9 +10,11 @@ sleep = function sleep(ms) {
while (new Date().getTime() < start + ms); while (new Date().getTime() < start + ms);
} }
Deploy = function(env, contractFiles, blockchainConfig, contractsConfig) { Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainManager) {
//this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); //this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
this.blockchainConfig = blockchainConfig; this.blockchainConfig = blockchainConfig;
this.chainManager = chainManager;
this.chainManager.init(env, this.blockchainConfig);
//this.contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml'); //this.contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml');
this.contractsManager = contractsConfig; this.contractsManager = contractsConfig;

View File

@ -22,6 +22,7 @@ Embark = {
this.blockchainConfig = (new Config.Blockchain()); this.blockchainConfig = (new Config.Blockchain());
this.compiler = (new Compiler(this.blockchainConfig)); this.compiler = (new Compiler(this.blockchainConfig));
this.contractsConfig = (new Config.Contracts(this.blockchainConfig, this.compiler)); this.contractsConfig = (new Config.Contracts(this.blockchainConfig, this.compiler));
this.chainManager = (new ChainManager());
}, },
tests: function(contractFiles) { tests: function(contractFiles) {
@ -33,9 +34,11 @@ Embark = {
chain.startChain(use_tmp); chain.startChain(use_tmp);
}, },
deployContracts: function(env, contractFiles, destFile) { deployContracts: function(env, contractFiles, destFile, chainFile) {
this.contractsConfig.init(contractFiles, env); this.contractsConfig.init(contractFiles, env);
var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig);
this.chainManager.loadConfigFile(chainFile)
var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager);
deploy.deploy_contracts(env); deploy.deploy_contracts(env);
return deploy.generate_abi_file(destFile); return deploy.generate_abi_file(destFile);
}, },

View File

@ -2,10 +2,14 @@ var ChainManager = require('../lib/chain_manager.js');
var Config = require('../lib/config/config.js'); var Config = require('../lib/config/config.js');
var Blockchain = require('../lib/blockchain.js'); var Blockchain = require('../lib/blockchain.js');
var assert = require('assert'); var assert = require('assert');
var fs = require('fs');
describe('embark.chain_manager', function() { describe('embark.chain_manager', function() {
var chainManager = (new ChainManager()).loadConfigFile('./test/support/chain_manager.json'); var chainFile = './test/support/chain_manager.json';
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml'); fs.writeFileSync(chainFile, '{}');
var chainManager = (new ChainManager()).loadConfigFile(chainFile);
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config('development');
describe('#init', function() { describe('#init', function() {
chainManager.init('development', blockchainConfig); chainManager.init('development', blockchainConfig);
@ -41,4 +45,16 @@ describe('embark.chain_manager', function() {
}); });
describe('#save', function() {
it('should save changes in the chain', function() {
chainManager.save();
var chainFile = './test/support/chain_manager.json';
var content = fs.readFileSync(chainFile).toString();
assert.equal(content, '{"0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f":{"contracts":{"d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e":{"name":"Foo","address":"0x123"}}}}');
});
});
}); });

View File

@ -9,10 +9,11 @@ setDeployConfig = function(config) {
var blockchainConfig = _blockchainConfig.config("development"); var blockchainConfig = _blockchainConfig.config("development");
var compiler = new Compiler(_blockchainConfig); var compiler = new Compiler(_blockchainConfig);
var contractsConfig = new Config.Contracts(blockchainConfig, compiler); var contractsConfig = new Config.Contracts(blockchainConfig, compiler);
var chainManager = (new ChainManager()).loadConfigFile('./test/support/chain_manager.json');
contractsConfig.loadConfigFile(config.contracts); contractsConfig.loadConfigFile(config.contracts);
contractsConfig.init(config.files, 'development'); contractsConfig.init(config.files, 'development');
compiler.init('development'); compiler.init('development');
return new Deploy('development', config.files, blockchainConfig, contractsConfig); return new Deploy('development', config.files, blockchainConfig, contractsConfig, chainManager);
} }
describe('embark.deploy', function() { describe('embark.deploy', function() {

View File

@ -1 +1 @@
{} {"0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f":{"contracts":{"d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e":{"name":"Foo","address":"0x123"}}}}