improve gas handling and errors

This commit is contained in:
Iuri Matias 2018-01-13 11:38:10 -05:00
parent ef0bc04f77
commit ea8f3a2232
8 changed files with 34 additions and 13 deletions

View File

@ -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;
}

View File

@ -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 %>;
}

View File

@ -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;
}
}
}
}

View File

@ -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));
});
});
}

View File

@ -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);
});
});

View File

@ -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

View File

@ -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 {

View File

@ -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();
});
});