From 3a1828f1f650e74dd8ba2f788ce93429ce7d7c28 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 1 Aug 2015 18:04:22 -0400 Subject: [PATCH] fix contract deployment; timeout & retry --- lib/deploy.js | 33 ++++++++++++++++++++++++--------- package.json | 1 - test/support/blockchain.yml | 2 +- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/deploy.js b/lib/deploy.js index 6237b6e9..14405c75 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -3,7 +3,12 @@ var fs = require('fs'); var grunt = require('grunt'); var readYaml = require('read-yaml'); var Config = require('./config/config.js'); -var sleep = require('sleep'); + +// 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) { //this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); @@ -25,6 +30,20 @@ Deploy = function(env, contractFiles, blockchainConfig, contractsConfig) { console.log("primary account address is : " + primaryAddress); }; +Deploy.prototype.deploy_contract = function(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 >= 20) { + return false; + } + } + return receipt; +} + Deploy.prototype.deploy_contracts = function(env) { this.contractsManager.compileContracts(env); all_contracts = this.contractsManager.all_contracts; @@ -64,16 +83,12 @@ Deploy.prototype.deploy_contracts = function(env) { gasPrice: contract.gasPrice }); - var transactionHash = contractObject["new"].apply(contractObject, contractParams).transactionHash; - // TODO: get this with sync until a different mechanism is implemented - //this.deployedContracts[className] = contractAddress; - //console.log("address is " + contractAddress); - console.log('trying to obtain ' + className + ' address...'); - var receipt = null; - while ((receipt = web3.eth.getTransactionReceipt(transactionHash)) === null || receipt.contractAddress === null) { - sleep.sleep(1); + + while((receipt = this.deploy_contract(contractObject, contractParams)) === false) { + console.log("timeout... failed to deploy contract.. retrying..."); } + var contractAddress = receipt.contractAddress; this.deployedContracts[className] = contractAddress; diff --git a/package.json b/package.json index 8bd3e6e8..23fa772a 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "python": "^0.0.4", "read-yaml": "^1.0.0", "shelljs": "^0.5.0", - "sleep": "^3.0.0", "sync-me": "^0.1.1", "toposort": "^0.2.10", "web3": "^0.8.1", diff --git a/test/support/blockchain.yml b/test/support/blockchain.yml index 3bfb3fd2..3612a11c 100644 --- a/test/support/blockchain.yml +++ b/test/support/blockchain.yml @@ -6,7 +6,7 @@ development: genesis_block: config/genesis.json datadir: /tmp/embark mine_when_needed: true - gas_limit: 500000 + gas_limit: 1000000 gas_price: 10000000000000 console: false account: