From 1912981d7d7fed41ad85506fe1ec3cf3fdb8079e Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 29 May 2018 17:23:29 -0400 Subject: [PATCH] move contract deploy code to contract deployer; move it to events --- .../{deploy.js => contract_deployer.js} | 44 ++++------------ lib/contracts/deploy_manager.js | 52 ++++++++++++++----- lib/core/engine.js | 13 ++++- lib/index.js | 11 ++-- lib/tests/test.js | 2 +- 5 files changed, 66 insertions(+), 56 deletions(-) rename lib/contracts/{deploy.js => contract_deployer.js} (90%) diff --git a/lib/contracts/deploy.js b/lib/contracts/contract_deployer.js similarity index 90% rename from lib/contracts/deploy.js rename to lib/contracts/contract_deployer.js index 20aac7d38..6d48d51bf 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/contract_deployer.js @@ -2,13 +2,19 @@ let async = require('async'); //require("../utils/debug_util.js")(__filename, async); let utils = require('../utils/utils.js'); -class Deploy { +class ContractDeployer { constructor(options) { + const self = this; + this.blockchain = options.blockchain; this.logger = options.logger; this.events = options.events; this.plugins = options.plugins; this.gasLimit = options.gasLimit; + + self.events.setCommandHandler('deploy:contract', (contract, cb) => { + self.checkAndDeployContract(contract, null, cb); + }); } // TODO: determining the arguments could also be in a module since it's not @@ -117,7 +123,7 @@ class Deploy { // TODO: can be moved into a afterDeploy event // just need to figure out the gasLimit coupling issue - self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { + self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit, (contractCode) => { self.events.request('runcode:eval', contractCode); return callback(); }); @@ -186,6 +192,7 @@ class Deploy { }); }, function applyBeforeDeploy(next) { + self.events.emit('deploy:contract:beforeDeploy', {contract: contract}); self.plugins.runActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next); }, function createDeployObject(next) { @@ -233,7 +240,7 @@ class Deploy { // TODO: can be moved into a afterDeploy event // just need to figure out the gasLimit coupling issue - self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { + self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit, (contractCode) => { self.events.request('runcode:eval', contractCode); self.plugins.runActionsForEvent('deploy:contract:deployed', {contract: contract}, () => { return next(null, receipt); @@ -244,35 +251,6 @@ class Deploy { ], callback); } - deployAll(done) { - let self = this; - this.logger.info(__("deploying contracts")); - this.events.emit("deploy:beforeAll"); - - self.events.request('contracts:list', (contracts) => { - async.eachOfSeries(contracts, - function (contract, key, callback) { - self.logger.trace(arguments); - self.checkAndDeployContract(contract, null, callback); - }, - function (err, _results) { - if (err) { - self.logger.error(__("error deploying contracts")); - self.logger.error(err.message); - self.logger.debug(err.stack); - } - if (contracts.length === 0) { - self.logger.info(__("no contracts found")); - return done(); - } - self.logger.info(__("finished deploying contracts")); - self.logger.trace(arguments); - done(err); - } - ); - }); - - } } -module.exports = Deploy; +module.exports = ContractDeployer; diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index afdb91b5f..fe986673a 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -1,9 +1,8 @@ let async = require('async'); -//require("../utils/debug_util.js")(__filename, async); -let Deploy = require('./deploy.js'); class DeployManager { constructor(options) { + const self = this; this.config = options.config; this.logger = options.logger; this.blockchainConfig = this.config.blockchainConfig; @@ -17,6 +16,41 @@ class DeployManager { this.fatalErrors = false; this.deployOnlyOnConfig = false; this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false; + + this.events.setCommandHandler('deploy:contracts', (cb) => { + self.deployContracts(cb); + }); + } + + deployAll(done) { + let self = this; + this.logger.info(__("deploying contracts")); + this.events.emit("deploy:beforeAll"); + + self.events.request('contracts:list', (contracts) => { + async.eachOfSeries(contracts, + function (contract, key, callback) { + contract._gasLimit = self.gasLimit; + self.events.request('deploy:contract', contract, () => { + callback(); + }); + }, + function (err, _results) { + if (err) { + self.logger.error(__("error deploying contracts")); + self.logger.error(err.message); + self.logger.debug(err.stack); + } + if (contracts.length === 0) { + self.logger.info(__("no contracts found")); + return done(); + } + self.logger.info(__("finished deploying contracts")); + self.logger.trace(arguments); + done(err); + } + ); + }); } deployContracts(done) { @@ -62,18 +96,7 @@ class DeployManager { }, function deployAllContracts(callback) { - let deploy = new Deploy({ - blockchain: self.blockchain, - contractsManager: self.contractsManager, - logger: self.logger, - events: self.events, - chainConfig: self.chainConfig, - env: self.config.env, - plugins: self.plugins, - gasLimit: self.gasLimit - }); - - deploy.deployAll(function (err) { + self.deployAll(function (err) { if (!err) { self.events.emit('contractsDeployed', self.contractsManager); } @@ -84,6 +107,7 @@ class DeployManager { }); }, function runAfterDeploy(callback) { + self.events.emit('contracts:deploy:afterAll'); self.plugins.runActionsForEvent('contracts:deploy:afterAll', callback); } ], function (err, _result) { diff --git a/lib/core/engine.js b/lib/core/engine.js index 82b7aa260..71383aac0 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -6,6 +6,7 @@ const Config = require('./config.js'); const Blockchain = require('../contracts/blockchain.js'); const Compiler = require('../contracts/compiler.js'); const ContractsManager = require('../contracts/contracts.js'); +const ContractDeployer = require('../contracts/contract_deployer.js'); const DeployManager = require('../contracts/deploy_manager.js'); const CodeGenerator = require('../contracts/code_generator.js'); const ServicesMonitor = require('./services_monitor.js'); @@ -242,6 +243,14 @@ class Engine { onlyCompile: options.onlyCompile }); + this.contractDeployer = new ContractDeployer({ + blockchain: this.blockchain, + logger: this.logger, + events: this.events, + plugins: this.plugins + //gasLimit: options.gasLimit + }); + this.events.on('file-event', function (fileType) { clearTimeout(self.fileTimeout); self.fileTimeout = setTimeout(() => { @@ -255,8 +264,8 @@ class Engine { // because the contractsManager config is corrupted after a deploy if (fileType === 'contract' || fileType === 'config') { self.config.reloadConfig(); - self.deployManager.deployContracts(function () { - }); + + self.events.request('deploy:contracts', () => {}); } }, 50); }); diff --git a/lib/index.js b/lib/index.js index 828026158..f8a0f6457 100644 --- a/lib/index.js +++ b/lib/index.js @@ -167,7 +167,7 @@ class Embark { engine.events.on('check:backOnline:Ethereum', function () { engine.logger.info(__('Ethereum node detected') + '..'); engine.config.reloadConfig(); - engine.deployManager.deployContracts(function () { + engine.events.request('deploy:contracts', function() { engine.logger.info(__('Deployment Done')); }); }); @@ -191,6 +191,7 @@ class Embark { engine.logger.info(__("Ready").underline); engine.events.emit("status", __("Ready").green); }); + if (options.runWebserver) { engine.startService("webServer", { host: options.serverHost, @@ -255,7 +256,7 @@ class Embark { callback(); }, function deploy(callback) { - engine.deployManager.deployContracts(function (err) { + engine.events.request('deploy:contracts', function(err) { callback(err); }); } @@ -306,7 +307,7 @@ class Embark { engine.startService("deployment", {onlyCompile: true}); engine.startService("codeGenerator"); - engine.deployManager.deployContracts(function (err) { + engine.events.request('deploy:contracts', function(err) { callback(err); }); } @@ -418,7 +419,6 @@ class Embark { callback(); }, function deploy(callback) { - // 2. upload to storage (outputDone event triggered after webpack finished) engine.events.on('outputDone', function () { cmdPlugin.uploadCmds[0].cb() .then((success) => { @@ -427,8 +427,7 @@ class Embark { .catch(callback); }); - // 1. build the contracts and dapp webpack - engine.deployManager.deployContracts(function (err) { + engine.events.request('deploy:contracts', function(err) { engine.logger.info(__("finished deploying").underline); if(err){ callback(err); diff --git a/lib/tests/test.js b/lib/tests/test.js index 956f4ac27..a17a5d033 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -88,7 +88,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) { self.engine.contractsManager.gasLimit = 6000000; self.engine.deployManager.fatalErrors = true; self.engine.deployManager.deployOnlyOnConfig = true; - self.engine.deployManager.deployContracts(function(err, _result) { + self.engine.events.request('deploy:contracts', function(err) { if (err) { callback(err); }