fix contract deployment; timeout & retry

This commit is contained in:
Iuri Matias 2015-08-01 18:04:22 -04:00
parent dec7ae8a94
commit 3a1828f1f6
3 changed files with 25 additions and 11 deletions

View File

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

View File

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

View File

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