From a664492d1fafafbe7a8a93fb6a79b82f90197030 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 20 May 2018 19:29:26 -0400 Subject: [PATCH] move onDeploy to special configs module --- lib/contracts/deploy.js | 64 ++++------------------------- lib/core/plugin.js | 6 +++ lib/modules/specialconfigs/index.js | 46 +++++++++++++++++++-- 3 files changed, 56 insertions(+), 60 deletions(-) diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 663fb44df..0289ab76d 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -142,64 +142,13 @@ class Deploy { let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); RunCode.doEval(contractCode, {web3: self.web3}); - if (contract.onDeploy !== undefined) { - self.logger.info(__('executing onDeploy commands')); + let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions'); - let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); - RunCode.doEval(contractCode, {web3: self.web3}); - - let withErrors = false; - 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(); + async.eachLimit(onDeployPlugins, 1, function(plugin, nextEach) { + plugin.call(plugin, contract, nextEach); + }, () => { + callback(); + }); }); } @@ -212,6 +161,7 @@ class Deploy { let deployObject; async.waterfall([ + // TODO: can potentially go to a beforeDeploy plugin function getAccounts(next) { self.blockchain.getAccounts(function (err, _accounts) { if (err) { diff --git a/lib/core/plugin.js b/lib/core/plugin.js index b3ba3e34e..17feb99ad 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -26,6 +26,7 @@ var Plugin = function(options) { this.embarkjs_code = []; this.embarkjs_init_code = {}; this.afterContractsDeployActions = []; + this.onDeployActions = []; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -209,6 +210,11 @@ Plugin.prototype.registerAfterAllContractsDeploy = function(cb) { this.pluginTypes.push('afterContractsDeployActions'); }; +Plugin.prototype.registerOnDeployContracts = function(cb) { + this.onDeployActions.push(cb); + this.pluginTypes.push('onDeployActions'); +}; + Plugin.prototype.runFilePipeline = function() { var self = this; diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js index 86dc78f34..5ee049b82 100644 --- a/lib/modules/specialconfigs/index.js +++ b/lib/modules/specialconfigs/index.js @@ -13,6 +13,7 @@ class SpecialConfigs { this.contractsConfig = embark.config.contractsConfig; this.registerAfterDeployAction(); + this.registerOnDeployAction(); } replaceWithAddresses(cmd, cb) { @@ -24,17 +25,17 @@ class SpecialConfigs { self.events.request('contracts:contract', referedContractName, (referedContract) => { if (!referedContract) { 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")); } if (referedContract && referedContract.deploy === false) { 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")); } if (referedContract && !referedContract.deployedAddress) { 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 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;