From 39840bbd7a28eae34c9db97de4cd288c0f38af9c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 5 Aug 2015 22:19:21 -0400 Subject: [PATCH] take into account contract args and classname when recording contract --- lib/chain_manager.js | 8 ++++---- lib/deploy.js | 25 ++++++++++++------------- test/chain_manager.js | 13 +++++++------ test/deploy.js | 1 + test/support/chain_manager.json | 2 +- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/chain_manager.js b/lib/chain_manager.js index 9dc8e5c32..d63c8b850 100644 --- a/lib/chain_manager.js +++ b/lib/chain_manager.js @@ -36,15 +36,15 @@ ChainManager.prototype.init = function(env, config) { this.currentChain = this.chainManagerConfig[chainId]; } -ChainManager.prototype.addContract = function(contractName, code, address) { - this.currentChain.contracts[sha3_256(code)] = { +ChainManager.prototype.addContract = function(contractName, code, args, address) { + this.currentChain.contracts[sha3_256(code + contractName + args.join(','))] = { name: contractName, address: address } } -ChainManager.prototype.getContract = function(code) { - return this.currentChain.contracts[sha3_256(code)]; +ChainManager.prototype.getContract = function(contractName, code, args) { + return this.currentChain.contracts[sha3_256(code + contractName + args.join(','))]; } ChainManager.prototype.save = function() { diff --git a/lib/deploy.js b/lib/deploy.js index 2c6b85613..899a88229 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -58,6 +58,15 @@ Deploy.prototype.deploy_contracts = function(env) { className = all_contracts[k]; 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) { this.deployedContracts[className] = contract.address; @@ -66,7 +75,7 @@ Deploy.prototype.deploy_contracts = function(env) { console.log("contract " + className + " at " + contract.address); } else { - var chainContract = this.chainManager.getContract(contract.compiled.code); + var chainContract = this.chainManager.getContract(className, contract.compiled.code, realArgs); if (chainContract != undefined) { 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); - 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); - } - } - - contractParams = realArgs; + contractParams = realArgs.slice(); contractParams.push({ from: primaryAddress, data: contract.compiled.code, @@ -114,7 +113,7 @@ Deploy.prototype.deploy_contracts = function(env) { } this.deployedContracts[className] = contractAddress; - this.chainManager.addContract(className, contract.compiled.code, contractAddress); + this.chainManager.addContract(className, contract.compiled.code, realArgs, contractAddress); this.chainManager.save(); console.log("deployed " + className + " at " + contractAddress); diff --git a/test/chain_manager.js b/test/chain_manager.js index 26dcd13dd..e2add352a 100644 --- a/test/chain_manager.js +++ b/test/chain_manager.js @@ -15,7 +15,7 @@ describe('embark.chain_manager', function() { chainManager.init('development', blockchainConfig); it('should initialize chain', function() { - var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] + var chain = chainManager.chainManagerConfig['0xcd9c11da1e46f86ce40a38b6ef84cfdfa6ea92598a27538f0e87da6d7a5c73d5'] assert.equal(chain != undefined, true); }); }); @@ -23,10 +23,11 @@ describe('embark.chain_manager', function() { describe('#addContract', 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'] - var contract = chain.contracts["d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e"] + console.log(chainManager.chainManagerConfig); + var chain = chainManager.chainManagerConfig['0xcd9c11da1e46f86ce40a38b6ef84cfdfa6ea92598a27538f0e87da6d7a5c73d5'] + var contract = chain.contracts["d5d91a8825c5c253dff531ddda2354c4014f5699b7bcbea70207cfdcb37b6c8b"] assert.equal(contract.name, "Foo"); assert.equal(contract.address, "0x123"); @@ -37,7 +38,7 @@ describe('embark.chain_manager', function() { describe('#getContract', 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.address, "0x123"); @@ -52,7 +53,7 @@ describe('embark.chain_manager', function() { var chainFile = './test/support/chain_manager.json'; 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"}}}}'); }); }); diff --git a/test/deploy.js b/test/deploy.js index c5df8511e..7e7fd6b22 100644 --- a/test/deploy.js +++ b/test/deploy.js @@ -1,6 +1,7 @@ var Config = require('../lib/config/config.js'); var Deploy = require('../lib/deploy.js'); var Compiler = require('../lib/compiler.js'); +var ChainManager = require('../lib/chain_manager.js'); var assert = require('assert'); var web3 = require('web3'); diff --git a/test/support/chain_manager.json b/test/support/chain_manager.json index ccb1943fb..0b027530b 100644 --- a/test/support/chain_manager.json +++ b/test/support/chain_manager.json @@ -1 +1 @@ -{"0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f":{"contracts":{"d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e":{"name":"Foo","address":"0x123"}}}} \ No newline at end of file +{"0xcd9c11da1e46f86ce40a38b6ef84cfdfa6ea92598a27538f0e87da6d7a5c73d5":{"contracts":{"d5d91a8825c5c253dff531ddda2354c4014f5699b7bcbea70207cfdcb37b6c8b":{"name":"Foo","address":"0x123"}}}} \ No newline at end of file