From e20d7bfc6a04f46ab80d102f02e046959e4a795c Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Sat, 22 Sep 2018 14:43:10 +0100 Subject: [PATCH] Fix race condition on deploy tracker --- lib/modules/deployment/index.js | 108 +++++++++++++++-------------- lib/modules/deploytracker/index.js | 11 ++- 2 files changed, 61 insertions(+), 58 deletions(-) diff --git a/lib/modules/deployment/index.js b/lib/modules/deployment/index.js index 1ea9064f8..33bb6788d 100644 --- a/lib/modules/deployment/index.js +++ b/lib/modules/deployment/index.js @@ -41,63 +41,69 @@ class DeployManager { deployAll(done) { let self = this; - - self.events.request('contracts:dependencies', (err, contractDependencies) => { - self.events.request('contracts:list', (err, contracts) => { - if (err) { - return done(err); - } - - self.logger.info(__("deploying contracts")); - self.events.emit("deploy:beforeAll"); - - const contractDeploys = {}; - const errors = []; - contracts.forEach(contract => { - function deploy(result, callback) { - if (typeof result === 'function') { - callback = result; + async.waterfall([ + function loadTracker(next) { + self.events.request("deployTracker:load", next); + }, + function doDeployAll() { + self.events.request('contracts:dependencies', (err, contractDependencies) => { + self.events.request('contracts:list', (err, contracts) => { + if (err) { + return done(err); } - contract._gasLimit = self.gasLimit; - self.events.request('deploy:contract', contract, (err) => { - if (err) { - contract.error = err.message || err; - self.logger.error(err.message || err); - errors.push(err); + + self.logger.info(__("deploying contracts")); + self.events.emit("deploy:beforeAll"); + + const contractDeploys = {}; + const errors = []; + contracts.forEach(contract => { + function deploy(result, callback) { + if (typeof result === 'function') { + callback = result; + } + contract._gasLimit = self.gasLimit; + self.events.request('deploy:contract', contract, (err) => { + if (err) { + contract.error = err.message || err; + self.logger.error(err.message || err); + errors.push(err); + } + callback(); + }); } - 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); }); - } - const className = contract.className; - if (!contractDependencies[className] || contractDependencies[className].length === 0) { - contractDeploys[className] = deploy; - return; - } - contractDeploys[className] = cloneDeep(contractDependencies[className]); - contractDeploys[className].push(deploy); - }); - - try { - async.auto(contractDeploys, function(_err, _results) { - if (errors.length) { - _err = __("Error deploying contracts. Please fix errors to continue."); - self.logger.error(_err); - return done(_err); + try { + async.auto(contractDeploys, function(_err, _results) { + if (errors.length) { + _err = __("Error deploying contracts. Please fix errors to continue."); + self.logger.error(_err); + return done(_err); + } + if (contracts.length === 0) { + self.logger.info(__("no contracts found")); + return done(); + } + self.logger.info(__("finished deploying contracts")); + done(err); + }); + } catch (e) { + self.logger.error(e.message || e); + done(__('Error deploying')); } - if (contracts.length === 0) { - self.logger.info(__("no contracts found")); - return done(); - } - self.logger.info(__("finished deploying contracts")); - done(err); }); - } catch (e) { - self.logger.error(e.message || e); - done(__('Error deploying')); - } - }); - }); + }); + } + ]); } deployContracts(done) { diff --git a/lib/modules/deploytracker/index.js b/lib/modules/deploytracker/index.js index 54b17aed7..19766cdc9 100644 --- a/lib/modules/deploytracker/index.js +++ b/lib/modules/deploytracker/index.js @@ -19,7 +19,7 @@ class DeployTracker { registerEvents() { const self = this; - this.events.on("deploy:beforeAll", this.setCurrentChain.bind(this)); + this.events.setCommandHandler("deployTracker:load", this.setCurrentChain.bind(this)); this.events.on("deploy:contract:deployed", (contract) => { self.trackContract(contract.className, contract.realRuntimeBytecode, contract.realArgs, contract.deployedAddress); @@ -43,15 +43,12 @@ class DeployTracker { }); } - // TODO: just an event might not be enought to the async nature - // it needs to be a plugin api before deploy, that makes the deployment wait - setCurrentChain() { + setCurrentChain(cb) { const self = this; if (this.chainConfig === false) { this.currentChain = {contracts: []}; - //return cb(); + return cb(); } - this.events.request("blockchain:block:byNumber", 0, function(_err, block) { let chainId = block.hash; @@ -62,7 +59,7 @@ class DeployTracker { self.currentChain = self.chainConfig[chainId]; self.currentChain.name = self.env; - //cb(); + cb(); }); }