diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 3bb86e54..ef16668a 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -155,7 +155,7 @@ class CodeGenerator { for (let className in this.contractsManager.contracts) { let contract = this.contractsManager.contracts[className]; let abi = JSON.stringify(contract.abiDefinition); - result += Templates.vanilla_contract({className: className, abi: abi, contract: contract}); + result += Templates.vanilla_contract({className: className, abi: abi, contract: contract, gasLimit: 6000000}); } return result; } @@ -185,7 +185,7 @@ class CodeGenerator { let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined"; block += Templates.embarkjs_contract({className: className, abi: abi, contract: contract, contractAddress: contractAddress, gasEstimates: gasEstimates}); } else { - block += Templates.vanilla_contract({className: className, abi: abi, contract: contract}); + block += Templates.vanilla_contract({className: className, abi: abi, contract: contract, gasLimit: (isDeployment ? 6000000 : false)}); } result += Templates.exec_when_ready({block: block}); @@ -195,11 +195,11 @@ class CodeGenerator { return result; } - generateContractCode(contract) { + generateContractCode(contract, gasLimit) { let abi = JSON.stringify(contract.abiDefinition); let block = ""; - block += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract}); + block += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: gasLimit}); return block; } diff --git a/lib/contracts/code_templates/vanilla-contract.js.ejs b/lib/contracts/code_templates/vanilla-contract.js.ejs index 684b6d71..9de12472 100644 --- a/lib/contracts/code_templates/vanilla-contract.js.ejs +++ b/lib/contracts/code_templates/vanilla-contract.js.ejs @@ -3,3 +3,7 @@ <%- className %>.options.address = '<%- contract.deployedAddress %>'; <%- className %>.address = '<%- contract.deployedAddress %>'; <%- className %>.options.from = web3.eth.defaultAccount; +if (<%- gasLimit %>) { + <%- className %>.options.gas = <%- gasLimit %>; + <%- className %>.options.gasLimit = <%- gasLimit %>; +} diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 9713050d..952fbdbb 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -14,6 +14,7 @@ class ContractsManager { this.logger = options.logger; this.plugins = options.plugins; this.contractDependencies = {}; + this.gasLimit = options.gasLimit; } build(done) { @@ -286,6 +287,9 @@ class ContractsManager { adjustedGas = Math.round(maxGas * 1.40); adjustedGas += 25000; contract.gas = adjustedGas; + if (this.gasLimit && this.gasLimit > contract.gas) { + contract.gas = this.gasLimit; + } } } } diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 52e9f180..4638a49e 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -13,6 +13,7 @@ class Deploy { this.logger = options.logger; this.env = options.env; this.chainConfig = options.chainConfig; + this.gasLimit = options.gasLimit; } initTracker(cb) { @@ -86,7 +87,7 @@ class Deploy { // always run contractCode so other functionality like 'afterDeploy' can also work let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager}); - let contractCode = codeGenerator.generateContractCode(contract); + let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); RunCode.doEval(contractCode, self.web3); return callback(); @@ -106,13 +107,13 @@ class Deploy { // always run contractCode so other functionality like 'afterDeploy' can also work let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager}); - let contractCode = codeGenerator.generateContractCode(contract); + let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); RunCode.doEval(contractCode, self.web3); if (contract.onDeploy !== undefined) { self.logger.info('executing onDeploy commands'); - let contractCode = codeGenerator.generateContractCode(contract); + let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); RunCode.doEval(contractCode, self.web3); let withErrors = false; @@ -241,6 +242,8 @@ class Deploy { return callback(null, receipt.contractAddress); } self.logger.contractsState(self.contractsManager.contractsState()); + }).on('error', function(error) { + return callback(new Error("error deploying =" + contract.className + "= due to error: " + error.message)); }); }); } diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 0796006f..91c8b125 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -14,6 +14,8 @@ class DeployManager { this.plugins = options.plugins; this.web3 = options.web3; this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false; + this.gasLimit = false; + this.fatalErrors = false; } deployContracts(done) { @@ -31,7 +33,8 @@ class DeployManager { contractFiles: self.config.contractsFiles, contractsConfig: self.config.contractsConfig, logger: self.logger, - plugins: self.plugins + plugins: self.plugins, + gasLimit: self.gasLimit }); contractsManager.build(callback); }, @@ -73,7 +76,8 @@ class DeployManager { contractsManager: contractsManager, logger: self.logger, chainConfig: self.chainConfig, - env: self.config.env + env: self.config.env, + gasLimit: self.gasLimit }); deploy.initTracker(function() { @@ -81,6 +85,9 @@ class DeployManager { if (!err) { self.events.emit('contractsDeployed', contractsManager); } + if (err && self.fatalErrors) { + return callback(err); + } callback(null, contractsManager, web3); }); }); diff --git a/lib/core/test.js b/lib/core/test.js index 1bba7882..3ff9bc76 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -73,17 +73,17 @@ Test.prototype.deployAll = function(contractsConfig, cb) { }); }); + self.engine.deployManager.gasLimit = 6000000; + self.engine.deployManager.fatalErrors = true; self.engine.deployManager.deployContracts(function(err, _result) { - console.log("deployed contracts"); if (err) { - console.log(err); callback(err); } }); } ], function(err, result) { if (err) { - console.log("got error"); + console.log('terminating due to error'); process.exit(); } // this should be part of the waterfall and not just something done at the diff --git a/test_app/app/contracts/simple_storage.sol b/test_app/app/contracts/simple_storage.sol index 58c90a35..dbec6e45 100644 --- a/test_app/app/contracts/simple_storage.sol +++ b/test_app/app/contracts/simple_storage.sol @@ -13,6 +13,9 @@ contract SimpleStorage is Ownable { function set(uint x) public { storedData = x; + for(uint i = 0; i < 1000; i++) { + storedData += i; + } } function set2(uint x, uint unusedGiveWarning) public onlyOwner { diff --git a/test_app/test/simple_storage_spec.js b/test_app/test/simple_storage_spec.js index 3c123c0c..9459cfa4 100644 --- a/test_app/test/simple_storage_spec.js +++ b/test_app/test/simple_storage_spec.js @@ -34,7 +34,7 @@ contract("SimpleStorage", function() { it("set storage value", function(done) { SimpleStorage.methods.set(150).send().then(function() { SimpleStorage.methods.get().call().then(function(result) { - assert.equal(result, 150); + assert.equal(result, 499650); done(); }); });