From d80f04850a3bc69178ace242ea419f2b40d6b5c9 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 23 Jul 2019 17:24:11 -0400 Subject: [PATCH] refactor contracts deployment --- packages/embark-deployment/src/index.js | 227 +++++++++++----------- packages/embark/src/cmd/cmd_controller.js | 6 +- packages/embark/src/lib/core/engine.js | 1 + 3 files changed, 121 insertions(+), 113 deletions(-) diff --git a/packages/embark-deployment/src/index.js b/packages/embark-deployment/src/index.js index 54a73630f..0de9f510d 100644 --- a/packages/embark-deployment/src/index.js +++ b/packages/embark-deployment/src/index.js @@ -1,18 +1,17 @@ import { __ } from 'embark-i18n'; const async = require('async'); -async._waterfall = async.waterfall; -let filename = "index.js"; -async.waterfall = function (_tasks, callback) { - let tasks = _tasks.map(function (t) { - let fn = function () { - console.log("async " + (new Error()).stack.split("\n")[1] + ": " + t.name); - t.apply(t, arguments); - }; - return fn; - }); - async._waterfall(tasks, callback); -}; - +// async._waterfall = async.waterfall; +// let filename = "index.js"; +// async.waterfall = function (_tasks, callback) { +// let tasks = _tasks.map(function (t) { +// let fn = function () { +// console.log("async " + (new Error()).stack.split("\n")[1] + ": " + t.name); +// t.apply(t, arguments); +// }; +// return fn; +// }); +// async._waterfall(tasks, callback); +// }; const ContractDeployer = require('./contract_deployer.js'); const cloneDeep = require('clone-deep'); @@ -39,116 +38,120 @@ class DeployManager { plugins: this.plugins }); - this.events.setCommandHandler('deploy:setGasLimit', (gasLimit) => { - self.gasLimit = gasLimit; + this.events.setCommandHandler('deployment:contracts:deploy', (contractsList, contractDependencies, cb) => { + self.deployContracts(contractsList, contractDependencies, cb); }); - this.events.setCommandHandler('deploy:contracts', (cb) => { - self.deployContracts(cb); - }); + // this.events.setCommandHandler('deploy:setGasLimit', (gasLimit) => { + // self.gasLimit = gasLimit; + // }); - this.events.setCommandHandler('deploy:contracts:test', (cb) => { - self.fatalErrors = true; - self.deployOnlyOnConfig = true; - self.deployContracts(cb); - }); + // this.events.setCommandHandler('deploy:contracts', (cb) => { + // self.deployContracts(cb); + // }); + + // this.events.setCommandHandler('deploy:contracts:test', (cb) => { + // self.fatalErrors = true; + // self.deployOnlyOnConfig = true; + // self.deployContracts(cb); + // }); } - deployAll(done) { + deployAll(contracts, contractDependencies, done) { let self = this; - self.events.request('contracts:dependencies', (err, contractDependencies) => { - self.events.request('contracts:list', (err, contracts) => { - if (err) { - return done(err); - } + // self.events.request('contracts:dependencies', (err, contractDependencies) => { + // self.events.request('contracts:list', (err, contracts) => { + // if (err) { + // return done(err); + // } - self.logger.info(__("deploying contracts")); - async.waterfall([ - function (next) { - self.logger.info(__('Executing pre-deploy actions...')); - self.plugins.emitAndRunActionsForEvent("deploy:beforeAll", (err) => { - // console.dir("== err") - // console.dir(err) - // TODO: err is a function for some reason - // if (err) { - // return next(err); - // } - self.logger.info(__('Pre-deploy actions done. Deploying contracts')); - next(); - }); - }, - function (next) { - const contractDeploys = {}; - const errors = []; - console.dir("=== contracts") - console.dir(contracts.map((x) => x.className)) - contracts.forEach(contract => { - function deploy(result, callback) { - if (typeof result === 'function') { - callback = result; - } - contract._gasLimit = self.gasLimit; - self.events.request('deploy:contract', contract, (err) => { - console.dir("contract deployed " + contract.className) - if (err) { - console.dir("== err deploying contract"); - console.dir(err); - contract.error = err.message || err; - if (contract.error === constants.blockchain.gasAllowanceError) { - self.logger.error(`[${contract.className}]: ${constants.blockchain.gasAllowanceErrorMessage}`); - } else { - self.logger.error(`[${contract.className}]: ${err.message || err}`); - } - errors.push(err); - } - callback(); - }); - } - - const className = contract.className; - if (!contractDependencies[className] || contractDependencies[className].length === 0) { - contractDeploys[className] = deploy; - return; - } - contractDeploys[className] = cloneDeep(contractDependencies[className]); - contractDeploys[className].push(deploy); - }); - - console.dir("== async.auto"); - console.dir(Object.keys(contractDeploys)); - console.dir(contractDeploys); - async.auto(contractDeploys, function(_err, _results) { - if (_err) { - console.dir("error deploying contracts") - console.dir(_err) - } - if (errors.length) { - _err = __("Error deploying contracts. Please fix errors to continue."); - self.logger.error(_err); - self.events.emit("outputError", __("Error deploying contracts, please check console")); - return next(_err); - } - if (contracts.length === 0) { - self.logger.info(__("no contracts found")); - return next(); - } - self.logger.info(__("finished deploying contracts")); - next(err); - }); - } - ], (err) => { - console.dir("==== finished deploying") - if (err) { - self.logger.error(err); - } - done(err); + self.logger.info(__("deploying contracts")); + async.waterfall([ + function (next) { + self.logger.info(__('Executing pre-deploy actions...')); + self.plugins.emitAndRunActionsForEvent("deploy:beforeAll", (err) => { + // console.dir("== err") + // console.dir(err) + // TODO: err is a function for some reason + // if (err) { + // return next(err); + // } + self.logger.info(__('Pre-deploy actions done. Deploying contracts')); + next(); }); - }); + }, + function (next) { + const contractDeploys = {}; + const errors = []; + console.dir("=== contracts") + console.dir(contracts.map((x) => x.className)) + contracts.forEach(contract => { + function deploy(result, callback) { + if (typeof result === 'function') { + callback = result; + } + contract._gasLimit = self.gasLimit; + self.events.request('deploy:contract', contract, (err) => { + console.dir("contract deployed " + contract.className) + if (err) { + console.dir("== err deploying contract"); + console.dir(err); + contract.error = err.message || err; + if (contract.error === constants.blockchain.gasAllowanceError) { + self.logger.error(`[${contract.className}]: ${constants.blockchain.gasAllowanceErrorMessage}`); + } else { + self.logger.error(`[${contract.className}]: ${err.message || err}`); + } + errors.push(err); + } + callback(); + }); + } + + const className = contract.className; + if (!contractDependencies[className] || contractDependencies[className].length === 0) { + contractDeploys[className] = deploy; + return; + } + contractDeploys[className] = cloneDeep(contractDependencies[className]); + contractDeploys[className].push(deploy); + }); + + console.dir("== async.auto"); + console.dir(Object.keys(contractDeploys)); + console.dir(contractDeploys); + async.auto(contractDeploys, function (_err, _results) { + if (_err) { + console.dir("error deploying contracts") + console.dir(_err) + } + if (errors.length) { + _err = __("Error deploying contracts. Please fix errors to continue."); + self.logger.error(_err); + self.events.emit("outputError", __("Error deploying contracts, please check console")); + return next(_err); + } + if (contracts.length === 0) { + self.logger.info(__("no contracts found")); + return next(); + } + self.logger.info(__("finished deploying contracts")); + next(err); + }); + } + ], (err) => { + console.dir("==== finished deploying") + if (err) { + self.logger.error(err); + } + done(err); }); + // }); + // }); } - deployContracts(done) { + deployContracts(contractsList, contractDependencies, done) { let self = this; if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { @@ -195,7 +198,7 @@ class DeployManager { }, function deployAllContracts(callback) { - self.deployAll(function (err) { + self.deployAll(contractsList, contractDependencies, function (err) { if (!err) { self.events.emit('contractsDeployed'); } diff --git a/packages/embark/src/cmd/cmd_controller.js b/packages/embark/src/cmd/cmd_controller.js index 74be0ebae..aab3a4a53 100644 --- a/packages/embark/src/cmd/cmd_controller.js +++ b/packages/embark/src/cmd/cmd_controller.js @@ -218,8 +218,12 @@ class EmbarkController { console.dir(_contractsConfig); let contractsConfig = cloneDeep(_contractsConfig); - engine.events.request("contracts:build", contractsConfig, compiledContracts, () => { + engine.events.request("contracts:build", contractsConfig, compiledContracts, (err, contractsList, contractDependencies) => { console.dir("contracts config build done") + + engine.events.request("deployment:contracts:deploy", contractsList, contractDependencies, () => { + console.dir("deployment done") + }) }) }) }) diff --git a/packages/embark/src/lib/core/engine.js b/packages/embark/src/lib/core/engine.js index 971e9da1e..93449c0f5 100644 --- a/packages/embark/src/lib/core/engine.js +++ b/packages/embark/src/lib/core/engine.js @@ -154,6 +154,7 @@ class Engine { contractsComponents(options) { this.registerModulePackage('embark-contracts-manager', {plugins: this.plugins, compileOnceOnly: options.compileOnceOnly}); + this.registerModulePackage('embark-deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile}); } startService(serviceName, _options) {