move onDeploy to special configs module

This commit is contained in:
Iuri Matias 2018-05-20 19:29:26 -04:00 committed by Jonathan Rainville
parent 31833579a7
commit a664492d1f
3 changed files with 56 additions and 60 deletions

View File

@ -142,64 +142,13 @@ class Deploy {
let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit);
RunCode.doEval(contractCode, {web3: self.web3}); RunCode.doEval(contractCode, {web3: self.web3});
if (contract.onDeploy !== undefined) { let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions');
self.logger.info(__('executing onDeploy commands'));
let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); async.eachLimit(onDeployPlugins, 1, function(plugin, nextEach) {
RunCode.doEval(contractCode, {web3: self.web3}); plugin.call(plugin, contract, nextEach);
}, () => {
let withErrors = false; callback();
let regex = /\$\w+/g; });
let onDeployCode = contract.onDeploy.map((cmd) => {
let realCmd = cmd.replace(regex, (match) => {
let referedContractName = match.slice(1);
let referedContract = self.contractsManager.getContract(referedContractName);
if (!referedContract) {
self.logger.error(__('error executing onDeploy for ') + contract.className.cyan);
self.logger.error(referedContractName + __(' does not exist'));
self.logger.error(__("error running onDeploy: ") + cmd);
withErrors = true;
return;
}
if (referedContract && referedContract.deploy === false) {
self.logger.error(__('error executing onDeploy for ') + contract.className.cyan);
self.logger.error(referedContractName + __(" exists but has been set to not deploy"));
self.logger.error(__("error running onDeploy: ") + cmd);
withErrors = true;
return;
}
if (referedContract && !referedContract.deployedAddress) {
self.logger.error(__('error executing onDeploy for ') + contract.className.cyan);
self.logger.error(__("couldn't find a valid address for %s has it been deployed?", referedContractName));
self.logger.error(__("error running onDeploy: ") + cmd);
withErrors = true;
return;
}
return referedContract.deployedAddress;
});
return realCmd;
});
if (withErrors) {
contract.error = "onDeployCmdError";
return callback(new Error("error running onDeploy"));
}
// TODO: convert to for to avoid repeated callback
for(let cmd of onDeployCode) {
self.logger.info(__("executing: ") + cmd);
try {
RunCode.doEval(cmd, {web3: self.web3});
} catch(e) {
if (e.message.indexOf("invalid opcode") >= 0) {
self.logger.error(__('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation'));
}
return callback(new Error(e));
}
}
}
callback();
}); });
} }
@ -212,6 +161,7 @@ class Deploy {
let deployObject; let deployObject;
async.waterfall([ async.waterfall([
// TODO: can potentially go to a beforeDeploy plugin
function getAccounts(next) { function getAccounts(next) {
self.blockchain.getAccounts(function (err, _accounts) { self.blockchain.getAccounts(function (err, _accounts) {
if (err) { if (err) {

View File

@ -26,6 +26,7 @@ var Plugin = function(options) {
this.embarkjs_code = []; this.embarkjs_code = [];
this.embarkjs_init_code = {}; this.embarkjs_init_code = {};
this.afterContractsDeployActions = []; this.afterContractsDeployActions = [];
this.onDeployActions = [];
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
this.config = options.config; this.config = options.config;
@ -209,6 +210,11 @@ Plugin.prototype.registerAfterAllContractsDeploy = function(cb) {
this.pluginTypes.push('afterContractsDeployActions'); this.pluginTypes.push('afterContractsDeployActions');
}; };
Plugin.prototype.registerOnDeployContracts = function(cb) {
this.onDeployActions.push(cb);
this.pluginTypes.push('onDeployActions');
};
Plugin.prototype.runFilePipeline = function() { Plugin.prototype.runFilePipeline = function() {
var self = this; var self = this;

View File

@ -13,6 +13,7 @@ class SpecialConfigs {
this.contractsConfig = embark.config.contractsConfig; this.contractsConfig = embark.config.contractsConfig;
this.registerAfterDeployAction(); this.registerAfterDeployAction();
this.registerOnDeployAction();
} }
replaceWithAddresses(cmd, cb) { replaceWithAddresses(cmd, cb) {
@ -24,17 +25,17 @@ class SpecialConfigs {
self.events.request('contracts:contract', referedContractName, (referedContract) => { self.events.request('contracts:contract', referedContractName, (referedContract) => {
if (!referedContract) { if (!referedContract) {
self.logger.error(referedContractName + ' does not exist'); self.logger.error(referedContractName + ' does not exist');
self.logger.error("error running afterDeploy: " + cmd); self.logger.error("error running cmd: " + cmd);
return reject(new Error("ReferedContractDoesNotExist")); return reject(new Error("ReferedContractDoesNotExist"));
} }
if (referedContract && referedContract.deploy === false) { if (referedContract && referedContract.deploy === false) {
self.logger.error(referedContractName + " exists but has been set to not deploy"); self.logger.error(referedContractName + " exists but has been set to not deploy");
self.logger.error("error running afterDeploy: " + cmd); self.logger.error("error running cmd: " + cmd);
return reject(new Error("ReferedContracSetToNotdeploy")); return reject(new Error("ReferedContracSetToNotdeploy"));
} }
if (referedContract && !referedContract.deployedAddress) { if (referedContract && !referedContract.deployedAddress) {
self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?"); self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?");
self.logger.error("error running afterDeploy: " + cmd); self.logger.error("error running cmd: " + cmd);
return reject(new Error("ReferedContractAddressNotFound")); return reject(new Error("ReferedContractAddressNotFound"));
} }
return resolve(referedContract.deployedAddress); return resolve(referedContract.deployedAddress);
@ -78,6 +79,45 @@ class SpecialConfigs {
}); });
} }
registerOnDeployAction() {
const self = this;
this.embark.registerOnDeployContracts((contract, cb) => {
if (!contract.onDeploy) {
return cb();
}
self.logger.info(__('executing onDeploy commands'));
let onDeployCmds = contract.onDeploy;
async.mapLimit(onDeployCmds, 1, (cmd, nextMapCb) => {
self.replaceWithAddresses(cmd, nextMapCb);
}, (err, result) => {
if (err) {
return cb(new Error("error running onDeploy for " + contract.className.cyan));
}
let onDeployCode = result;
// TODO: convert to for to avoid repeated callback
for(let cmd of onDeployCode) {
self.logger.info("==== executing: " + cmd);
try {
// TODO: request and re-add web3 object if necessary
//RunCode.doEval(cmd, self.blockchain.web3);
RunCode.doEval(cmd);
} catch(e) {
if (e.message.indexOf("invalid opcode") >= 0) {
self.logger.error('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation');
}
return cb(new Error(e));
}
}
cb();
});
});
}
} }
module.exports = SpecialConfigs; module.exports = SpecialConfigs;