make deployment async

This commit is contained in:
Iuri Matias 2015-10-08 15:30:47 -04:00
parent 32ac41a9cb
commit 95823a8e54
6 changed files with 106 additions and 132 deletions

View File

@ -1,4 +1,6 @@
var Embark = require('embark-framework'); var Embark = require('embark-framework');
var ethersim = require('ethersim');
var web3 = require('web3');
Embark.init(); Embark.init();
Embark.blockchainConfig.loadConfigFile('config/blockchain.yml'); Embark.blockchainConfig.loadConfigFile('config/blockchain.yml');
Embark.contractsConfig.loadConfigFile('config/contracts.yml'); Embark.contractsConfig.loadConfigFile('config/contracts.yml');
@ -6,7 +8,17 @@ Embark.contractsConfig.loadConfigFile('config/contracts.yml');
var files = ["app/contracts/simple_storage.sol"]; var files = ["app/contracts/simple_storage.sol"];
Embark.contractsConfig.init(files, 'development'); Embark.contractsConfig.init(files, 'development');
var EmbarkSpec = Embark.tests(files);
var Manager = ethersim.Manager;
var Provider = ethersim.Provider;
console.log("initializing");
var manager = new Manager();
web3.setProvider(new Provider(manager));
abi = Embark.deployContracts('development', files, "/tmp/abi.js", "chains.json", web3);
console.log(abi);
eval(abi);
describe("SimpleStorage", function() { describe("SimpleStorage", function() {
beforeAll(function() { beforeAll(function() {

View File

@ -75,7 +75,7 @@ Blockchain.prototype.run_command = function(address, use_tmp) {
cmd += "--unlock " + address + " "; cmd += "--unlock " + address + " ";
} }
if (config.bootNodes.boot == true){ if (config.bootNodes !== undefined && config.bootNodes.boot == true){
cmd += "--bootnodes "; cmd += "--bootnodes ";
for (var i = 0; i < config.bootNodes.enodes.length; i++){ for (var i = 0; i < config.bootNodes.enodes.length; i++){
cmd += config.bootNodes.enodes[i] + " "; cmd += config.bootNodes.enodes[i] + " ";

View File

@ -10,15 +10,6 @@ Compiler = function(blockchainConfig) {
Compiler.prototype.init = function(env) { Compiler.prototype.init = function(env) {
var config = this.blockchainConfig.config(env); var config = this.blockchainConfig.config(env);
try {
web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort));
primaryAddress = web3.eth.coinbase;
web3.eth.defaultAccount = primaryAddress;
} catch (e) {
throw new Error("can't connect to " + config.rpcHost + ":" + config.rpcPort + " check if an ethereum node is running");
}
console.log("address is : " + primaryAddress); console.log("address is : " + primaryAddress);
}; };

View File

@ -32,35 +32,68 @@ Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainMa
console.log("primary account address is : " + primaryAddress); console.log("primary account address is : " + primaryAddress);
}; };
Deploy.prototype.deploy_contract = function(contractObject, contractParams) { Deploy.prototype.deploy_contract = function(contractObject, contractParams, cb) {
var transactionHash = contractObject["new"].apply(contractObject, contractParams).transactionHash; console.log("called deploy_contract");
var receipt = null; var callback = function(e, contract) {
var time = 0; console.log("got receipt");
while ((receipt = web3.eth.getTransactionReceipt(transactionHash)) === null || receipt.contractAddress === null) { console.log(arguments);
sleep(1000); if(!e && contract.address !== undefined) {
time += 1; console.log("Contract mined! Address: " + contract.address);
if (time >= this.blockchainConfig.deployTimeout) { cb(contract.address);
return false;
} }
else {
console.log("error deploying");
exit();
} }
return receipt; };
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) { Deploy.prototype.deploy_contracts = function(env, cb) {
this.contractsManager.compileContracts(env); this.contractsManager.compileContracts(env);
all_contracts = this.contractsManager.all_contracts; var all_contracts = this.contractsManager.all_contracts;
this.contractDB = this.contractsManager.contractDB; this.contractDB = this.contractsManager.contractDB;
contractDependencies = this.contractsManager.contractDependencies;
this.deployedContracts = {}; this.deployedContracts = {};
for (k = 0; k < all_contracts.length; k++) { this.deploy_contract_list(all_contracts.length, env, all_contracts, cb);
className = all_contracts[k]; }
contract = this.contractDB[className];
Deploy.prototype.deploy_contract_list = function(index, env, all_contracts, cb) {
if(index === 0) {
cb();
}
else {
var _this = this;
this.deploy_contract_list(index - 1, env, all_contracts, function() {
var className = all_contracts[index - 1];
_this.deploy_a_contract(env, className, cb);
});
}
}
Deploy.prototype.deploy_a_contract = function(env, className, cb) {
var contractDependencies = this.contractsManager.contractDependencies;
var contract = this.contractDB[className];
if (contract.deploy === false) { if (contract.deploy === false) {
console.log("skipping " + className); console.log("skipping " + className);
continue; cb();
return;
} }
var realArgs = []; var realArgs = [];
@ -78,6 +111,7 @@ Deploy.prototype.deploy_contracts = function(env) {
//console.log("contract " + className + " at " + contractAddress); //console.log("contract " + className + " at " + contractAddress);
console.log("contract " + className + " at " + contract.address); console.log("contract " + className + " at " + contract.address);
cb();
} }
else { else {
var chainContract = this.chainManager.getContract(className, contract.compiled.code, realArgs); var chainContract = this.chainManager.getContract(className, contract.compiled.code, realArgs);
@ -86,6 +120,7 @@ Deploy.prototype.deploy_contracts = function(env) {
console.log("contract " + className + " is unchanged and already deployed at " + chainContract.address); console.log("contract " + className + " is unchanged and already deployed at " + chainContract.address);
this.deployedContracts[className] = chainContract.address; this.deployedContracts[className] = chainContract.address;
this.execute_cmds(contract.onDeploy); this.execute_cmds(contract.onDeploy);
cb();
} }
else { else {
@ -101,11 +136,9 @@ Deploy.prototype.deploy_contracts = function(env) {
console.log('trying to obtain ' + className + ' address...'); console.log('trying to obtain ' + className + ' address...');
while((receipt = this.deploy_contract(contractObject, contractParams)) === false) { var _this = this;
console.log("timeout... failed to deploy contract.. retrying..."); this.deploy_contract(contractObject, contractParams, function(contractAddress) {
} console.log("response!");
var contractAddress = receipt.contractAddress;
if (web3.eth.getCode(contractAddress) === "0x") { if (web3.eth.getCode(contractAddress) === "0x") {
console.log("========="); console.log("=========");
@ -115,17 +148,22 @@ Deploy.prototype.deploy_contracts = function(env) {
} }
else { else {
console.log("deployed " + className + " at " + contractAddress); console.log("deployed " + className + " at " + contractAddress);
this.chainManager.addContract(className, contract.compiled.code, realArgs, contractAddress); _this.chainManager.addContract(className, contract.compiled.code, realArgs, contractAddress);
this.chainManager.save(); _this.chainManager.save();
} }
this.deployedContracts[className] = contractAddress; _this.deployedContracts[className] = contractAddress;
this.execute_cmds(contract.onDeploy); _this.execute_cmds(contract.onDeploy);
}
}
}
cb();
});
//while((receipt = this.deploy_contract(contractObject, contractParams)) === false) {
// console.log("timeout... failed to deploy contract.. retrying...");
//}
}
}
}; };
Deploy.prototype.execute_cmds = function(cmds) { Deploy.prototype.execute_cmds = function(cmds) {
@ -147,11 +185,14 @@ Deploy.prototype.execute_cmds = function(cmds) {
} }
} }
Deploy.prototype.generate_abi_file = function() { Deploy.prototype.generate_abi_file = function(web3) {
var result; var result;
result = "";
if (web3 === undefined) {
result = "web3.setProvider(new web3.providers.HttpProvider('http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + "'));"; result = "web3.setProvider(new web3.providers.HttpProvider('http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + "'));";
result += "web3.eth.defaultAccount = web3.eth.accounts[0];"; result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
}
for(className in this.deployedContracts) { for(className in this.deployedContracts) {
var deployedContract = this.deployedContracts[className]; var deployedContract = this.deployedContracts[className];

View File

@ -10,7 +10,7 @@ var syncMe = require('sync-me');
var methodmissing = require('methodmissing'); var methodmissing = require('methodmissing');
var jasmine = require('jasmine'); var jasmine = require('jasmine');
var Tests = require('./test.js'); //var Tests = require('./test.js');
var Blockchain = require('./blockchain.js'); var Blockchain = require('./blockchain.js');
var Deploy = require('./deploy.js'); var Deploy = require('./deploy.js');
var Release = require('./ipfs.js'); var Release = require('./ipfs.js');
@ -26,9 +26,9 @@ Embark = {
this.chainManager = (new ChainManager()); this.chainManager = (new ChainManager());
}, },
tests: function(contractFiles) { //tests: function() {
return new Tests(this.contractsConfig, contractFiles); // return new Tests();
}, //},
startBlockchain: function(env, use_tmp) { startBlockchain: function(env, use_tmp) {
var chain = new Blockchain(this.blockchainConfig.config(env)); var chain = new Blockchain(this.blockchainConfig.config(env));
@ -45,13 +45,16 @@ Embark = {
return chain.getStartChainCommand(use_tmp); return chain.getStartChainCommand(use_tmp);
}, },
deployContracts: function(env, contractFiles, destFile, chainFile) { deployContracts: function(env, contractFiles, destFile, chainFile, cb) {
this.contractsConfig.init(contractFiles, env); this.contractsConfig.init(contractFiles, env);
this.chainManager.loadConfigFile(chainFile) this.chainManager.loadConfigFile(chainFile)
var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager); var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager);
deploy.deploy_contracts(env); deploy.deploy_contracts(env, function() {
return deploy.generate_abi_file(destFile); console.log("contracts deployed; generating abi file");
var result = deploy.generate_abi_file();
cb(result);
});
}, },
geth: function(env, args) { geth: function(env, args) {

View File

@ -1,73 +0,0 @@
var python = require('python').shell;
var mm = require('methodmissing');
var sync = require('sync-me');
py_exec = function(cmd) {
return sync(python, cmd)[1].trim();
};
TestContractWrapper = (function() {
function TestContractWrapper(contract, className, args) {
this.contract = contract.compiled;
this.className = className;
this.args = args;
this.initializeContract();
}
TestContractWrapper.prototype.initializeContract = function() {
example_abi = JSON.stringify(this.contract.info.abiDefinition);
example_binary = this.contract.code.slice(2);
py_exec("example_abi = '" + example_abi + "'");
py_exec("example_abi");
py_exec("example_binary = '" + example_binary + "'.decode('hex')");
py_exec("example_binary");
if (this.args === undefined) {
py_exec(this.className + "_contract = EvmContract(example_abi, example_binary, '" + this.className + "')");
}
else {
py_exec(this.className + "_contract = EvmContract(example_abi, example_binary, '" + this.className + "', [" + this.args.join(",") + "])");
}
this.contractVariable = this.className + "_contract";
};
TestContractWrapper.prototype.execCmd = function(method, args) {
var arg_list = [];
for (var key in args) {
var value = args[key];
arg_list.push(value);
}
data = py_exec(this.className + "_contract." + method + "(" + arg_list.join(",") + ")");
return data;
};
return TestContractWrapper;
})();
TestContract = function(contract, className, args) {
var wrapper = new TestContractWrapper(contract, className, args);
var Obj = mm(wrapper, function (key, args) {
return wrapper.execCmd(key, args);
});
return Obj;
}
test = function(contractsConfig, contractFiles) {
contractsConfig.init(contractFiles, 'development');
contractsConfig.compileContracts();
this.contractDB = contractsConfig.contractDB;
}
test.prototype.request = function(className, args) {
var contract = this.contractDB[className];
py_exec("from ethertdd import EvmContract");
return TestContract(contract, className, args);
}
module.exports = test;