take into account contract args and classname when recording contract

This commit is contained in:
Iuri Matias 2015-08-05 22:19:21 -04:00
parent d5d4cb81e3
commit 39840bbd7a
5 changed files with 25 additions and 24 deletions

View File

@ -36,15 +36,15 @@ ChainManager.prototype.init = function(env, config) {
this.currentChain = this.chainManagerConfig[chainId]; this.currentChain = this.chainManagerConfig[chainId];
} }
ChainManager.prototype.addContract = function(contractName, code, address) { ChainManager.prototype.addContract = function(contractName, code, args, address) {
this.currentChain.contracts[sha3_256(code)] = { this.currentChain.contracts[sha3_256(code + contractName + args.join(','))] = {
name: contractName, name: contractName,
address: address address: address
} }
} }
ChainManager.prototype.getContract = function(code) { ChainManager.prototype.getContract = function(contractName, code, args) {
return this.currentChain.contracts[sha3_256(code)]; return this.currentChain.contracts[sha3_256(code + contractName + args.join(','))];
} }
ChainManager.prototype.save = function() { ChainManager.prototype.save = function() {

View File

@ -58,6 +58,15 @@ Deploy.prototype.deploy_contracts = function(env) {
className = all_contracts[k]; className = all_contracts[k];
contract = this.contractDB[className]; contract = this.contractDB[className];
var realArgs = [];
for (var l = 0; l < contract.args.length; l++) {
arg = contract.args[l];
if (arg[0] === "$") {
realArgs.push(this.deployedContracts[arg.substr(1)]);
} else {
realArgs.push(arg);
}
}
if (contract.address !== undefined) { if (contract.address !== undefined) {
this.deployedContracts[className] = contract.address; this.deployedContracts[className] = contract.address;
@ -66,7 +75,7 @@ Deploy.prototype.deploy_contracts = function(env) {
console.log("contract " + className + " at " + contract.address); console.log("contract " + className + " at " + contract.address);
} }
else { else {
var chainContract = this.chainManager.getContract(contract.compiled.code); var chainContract = this.chainManager.getContract(className, contract.compiled.code, realArgs);
if (chainContract != undefined) { if (chainContract != undefined) {
console.log("contract " + className + " is unchanged and already deployed at " + chainContract.address); console.log("contract " + className + " is unchanged and already deployed at " + chainContract.address);
@ -77,17 +86,7 @@ Deploy.prototype.deploy_contracts = function(env) {
contractObject = web3.eth.contract(contract.compiled.info.abiDefinition); contractObject = web3.eth.contract(contract.compiled.info.abiDefinition);
realArgs = []; contractParams = realArgs.slice();
for (var l = 0; l < contract.args.length; l++) {
arg = contract.args[l];
if (arg[0] === "$") {
realArgs.push(this.deployedContracts[arg.substr(1)]);
} else {
realArgs.push(arg);
}
}
contractParams = realArgs;
contractParams.push({ contractParams.push({
from: primaryAddress, from: primaryAddress,
data: contract.compiled.code, data: contract.compiled.code,
@ -114,7 +113,7 @@ Deploy.prototype.deploy_contracts = function(env) {
} }
this.deployedContracts[className] = contractAddress; this.deployedContracts[className] = contractAddress;
this.chainManager.addContract(className, contract.compiled.code, contractAddress); this.chainManager.addContract(className, contract.compiled.code, realArgs, contractAddress);
this.chainManager.save(); this.chainManager.save();
console.log("deployed " + className + " at " + contractAddress); console.log("deployed " + className + " at " + contractAddress);

View File

@ -15,7 +15,7 @@ describe('embark.chain_manager', function() {
chainManager.init('development', blockchainConfig); chainManager.init('development', blockchainConfig);
it('should initialize chain', function() { it('should initialize chain', function() {
var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] var chain = chainManager.chainManagerConfig['0xcd9c11da1e46f86ce40a38b6ef84cfdfa6ea92598a27538f0e87da6d7a5c73d5']
assert.equal(chain != undefined, true); assert.equal(chain != undefined, true);
}); });
}); });
@ -23,10 +23,11 @@ describe('embark.chain_manager', function() {
describe('#addContract', function() { describe('#addContract', function() {
it('should register a contract in the chain', function() { it('should register a contract in the chain', function() {
chainManager.addContract("Foo", "123456", "0x123"); chainManager.addContract("Foo", "123456", [], "0x123");
var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] console.log(chainManager.chainManagerConfig);
var contract = chain.contracts["d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e"] var chain = chainManager.chainManagerConfig['0xcd9c11da1e46f86ce40a38b6ef84cfdfa6ea92598a27538f0e87da6d7a5c73d5']
var contract = chain.contracts["d5d91a8825c5c253dff531ddda2354c4014f5699b7bcbea70207cfdcb37b6c8b"]
assert.equal(contract.name, "Foo"); assert.equal(contract.name, "Foo");
assert.equal(contract.address, "0x123"); assert.equal(contract.address, "0x123");
@ -37,7 +38,7 @@ describe('embark.chain_manager', function() {
describe('#getContract', function() { describe('#getContract', function() {
it('should a contract in the chain', function() { it('should a contract in the chain', function() {
var contract = chainManager.getContract("123456"); var contract = chainManager.getContract("Foo", "123456", []);
assert.equal(contract.name, "Foo"); assert.equal(contract.name, "Foo");
assert.equal(contract.address, "0x123"); assert.equal(contract.address, "0x123");
@ -52,7 +53,7 @@ describe('embark.chain_manager', function() {
var chainFile = './test/support/chain_manager.json'; var chainFile = './test/support/chain_manager.json';
var content = fs.readFileSync(chainFile).toString(); var content = fs.readFileSync(chainFile).toString();
assert.equal(content, '{"0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f":{"contracts":{"d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e":{"name":"Foo","address":"0x123"}}}}'); assert.equal(content, '{"0xcd9c11da1e46f86ce40a38b6ef84cfdfa6ea92598a27538f0e87da6d7a5c73d5":{"contracts":{"d5d91a8825c5c253dff531ddda2354c4014f5699b7bcbea70207cfdcb37b6c8b\":{"name":"Foo","address":"0x123"}}}}');
}); });
}); });

View File

@ -1,6 +1,7 @@
var Config = require('../lib/config/config.js'); var Config = require('../lib/config/config.js');
var Deploy = require('../lib/deploy.js'); var Deploy = require('../lib/deploy.js');
var Compiler = require('../lib/compiler.js'); var Compiler = require('../lib/compiler.js');
var ChainManager = require('../lib/chain_manager.js');
var assert = require('assert'); var assert = require('assert');
var web3 = require('web3'); var web3 = require('web3');

View File

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