From 38445c9e7fe34ee59012374fa5dad50824ad5326 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 9 Oct 2015 13:20:35 -0400 Subject: [PATCH] fix deployment --- lib/chain_manager.js | 13 +++---------- lib/deploy.js | 41 ++++++++++------------------------------- lib/index.js | 10 +++------- lib/test.js | 2 +- 4 files changed, 17 insertions(+), 49 deletions(-) diff --git a/lib/chain_manager.js b/lib/chain_manager.js index ca8cff78b..1e909a7c9 100644 --- a/lib/chain_manager.js +++ b/lib/chain_manager.js @@ -1,12 +1,10 @@ var fs = require('fs'); -var web3 = require('web3'); var sha3_256 = require('js-sha3').sha3_256; -ChainManager = function(_web3) { +ChainManager = function() { this.chainManagerConfig = {}; this.currentChain = {}; this.file = ""; - this.web3 = _web3; } ChainManager.prototype.loadConfigFile = function(filename) { @@ -25,12 +23,8 @@ ChainManager.prototype.loadConfig = function(config) { return this; }; -ChainManager.prototype.init = function(env, config) { - if (this.web3 === undefined) { - web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort)); - } - - var block = this.web3.eth.getBlock(0); +ChainManager.prototype.init = function(env, config, web3) { + var block = web3.eth.getBlock(0); if(!block){ throw new Error("Cannot get the genesis block, is embark blockchain running ?"); } @@ -41,7 +35,6 @@ ChainManager.prototype.init = function(env, config) { } this.currentChain = this.chainManagerConfig[chainId]; - //this.web3 = web3; } ChainManager.prototype.addContract = function(contractName, code, args, address) { diff --git a/lib/deploy.js b/lib/deploy.js index 7e2c93540..dfebb4aa4 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -4,28 +4,17 @@ var grunt = require('grunt'); var readYaml = require('read-yaml'); var Config = require('./config/config.js'); -// Ugly, but sleep lib has issues on osx -sleep = function sleep(ms) { - var start = new Date().getTime(); - while (new Date().getTime() < start + ms); -} - -Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainManager, _web3) { +Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainManager, withProvider, withChain, _web3) { if (_web3 !== undefined) { web3 = _web3; } - //this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); - 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 = contractsConfig; this.contractsConfig = this.contractsManager.config(env); this.deployedContracts = {}; + this.blockchainConfig = blockchainConfig; try { - if (_web3 === undefined) { + if (withProvider) { web3.setProvider(new web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort)); } primaryAddress = web3.eth.coinbase; @@ -34,6 +23,10 @@ Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainMa throw new Error("==== can't connect to " + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + " check if an ethereum node is running"); } + this.chainManager = chainManager; + this.chainManager.init(env, this.blockchainConfig, web3); + this.withChain = withChain; + console.log("primary account address is : " + primaryAddress); }; @@ -55,18 +48,6 @@ Deploy.prototype.deploy_contract = function(contractObject, contractParams, cb) contractParams.push(callback); contractObject["new"].apply(contractObject, contractParams); - - //var transactionHash = contractObject["new"].apply(contractObject, contractParams).transactionHash; - //var receipt = null; - //var time = 0; - //while ((receipt = web3.eth.getTransactionReceipt(transactionHash)) === null || receipt.contractAddress === null) { - // sleep(1000); - // time += 1; - // if (time >= this.blockchainConfig.deployTimeout) { - // return false; - // } - //} - //return receipt; } Deploy.prototype.deploy_contracts = function(env, cb) { @@ -114,7 +95,6 @@ Deploy.prototype.deploy_a_contract = function(env, className, cb) { if (contract.address !== undefined) { this.deployedContracts[className] = contract.address; - //console.log("contract " + className + " at " + contractAddress); console.log("contract " + className + " at " + contract.address); cb(); } @@ -154,7 +134,9 @@ Deploy.prototype.deploy_a_contract = function(env, className, cb) { else { console.log("deployed " + className + " at " + contractAddress); _this.chainManager.addContract(className, contract.compiled.code, realArgs, contractAddress); - _this.chainManager.save(); + if (_this.withChain) { + _this.chainManager.save(); + } } _this.deployedContracts[className] = contractAddress; @@ -164,9 +146,6 @@ Deploy.prototype.deploy_a_contract = function(env, className, cb) { cb(); }); - //while((receipt = this.deploy_contract(contractObject, contractParams)) === false) { - // console.log("timeout... failed to deploy contract.. retrying..."); - //} } } }; diff --git a/lib/index.js b/lib/index.js index f3229654a..844334abb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,13 +25,9 @@ Embark = { else { this.web3 = web3; } - this.chainManager = (new ChainManager(this.web3)); + this.chainManager = (new ChainManager()); }, - //tests: function() { - // return new Tests(); - //}, - startBlockchain: function(env, use_tmp) { var chain = new Blockchain(this.blockchainConfig.config(env)); chain.startChain(use_tmp); @@ -47,11 +43,11 @@ Embark = { return chain.getStartChainCommand(use_tmp); }, - deployContracts: function(env, contractFiles, destFile, chainFile, withProvider, cb) { + deployContracts: function(env, contractFiles, destFile, chainFile, withProvider, withChain, cb) { this.contractsConfig.init(contractFiles, env); this.chainManager.loadConfigFile(chainFile) - var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager, this.web3); + var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager, withProvider, withChain, this.web3); deploy.deploy_contracts(env, function() { console.log("contracts deployed; generating abi file"); var result = "" diff --git a/lib/test.js b/lib/test.js index d62cdd587..bd9a04a1d 100644 --- a/lib/test.js +++ b/lib/test.js @@ -16,7 +16,7 @@ Test = function(cb) { Embark.contractsConfig.init(files, 'development'); - Embark.deployContracts('development', files, "/tmp/abi.js", "chains.json", false, function(abi) { + Embark.deployContracts('development', files, "/tmp/abi.js", "chains.json", false, false, function(abi) { console.log("return abi"); console.log(abi); eval(abi);