Merge pull request #895 from embark-framework/bugfix/race-condition-deploy-tracker

Fix race condition on deploy tracker
This commit is contained in:
Iuri Matias 2018-09-22 10:36:15 -04:00 committed by GitHub
commit 9ed08f234d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 50 deletions

View File

@ -49,53 +49,58 @@ class DeployManager {
}
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);
async.waterfall([
function (next) {
self.plugins.emitAndRunActionsForEvent("deploy:beforeAll", next);
},
function () {
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;
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'));
}
}
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);
}
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'));
}
]);
});
});
}

View File

@ -19,7 +19,7 @@ class DeployTracker {
registerEvents() {
const self = this;
this.events.on("deploy:beforeAll", this.setCurrentChain.bind(this));
this.embark.registerActionForEvent("deploy:beforeAll", 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();
});
}