refactor contracts deployment

This commit is contained in:
Iuri Matias 2019-07-23 17:24:11 -04:00
parent d4f9fd2e58
commit d80f04850a
3 changed files with 121 additions and 113 deletions

View File

@ -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');
}

View File

@ -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")
})
})
})
})

View File

@ -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) {