move contract deploy code to contract deployer; move it to events

This commit is contained in:
Iuri Matias 2018-05-29 17:23:29 -04:00
parent 70f38d863e
commit 1912981d7d
5 changed files with 66 additions and 56 deletions

View File

@ -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;

View File

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

View File

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

View File

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

View File

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