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); //require("../utils/debug_util.js")(__filename, async);
let utils = require('../utils/utils.js'); let utils = require('../utils/utils.js');
class Deploy { class ContractDeployer {
constructor(options) { constructor(options) {
const self = this;
this.blockchain = options.blockchain; this.blockchain = options.blockchain;
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
this.plugins = options.plugins; this.plugins = options.plugins;
this.gasLimit = options.gasLimit; 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 // 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 // TODO: can be moved into a afterDeploy event
// just need to figure out the gasLimit coupling issue // 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.events.request('runcode:eval', contractCode);
return callback(); return callback();
}); });
@ -186,6 +192,7 @@ class Deploy {
}); });
}, },
function applyBeforeDeploy(next) { function applyBeforeDeploy(next) {
self.events.emit('deploy:contract:beforeDeploy', {contract: contract});
self.plugins.runActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next); self.plugins.runActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next);
}, },
function createDeployObject(next) { function createDeployObject(next) {
@ -233,7 +240,7 @@ class Deploy {
// TODO: can be moved into a afterDeploy event // TODO: can be moved into a afterDeploy event
// just need to figure out the gasLimit coupling issue // 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.events.request('runcode:eval', contractCode);
self.plugins.runActionsForEvent('deploy:contract:deployed', {contract: contract}, () => { self.plugins.runActionsForEvent('deploy:contract:deployed', {contract: contract}, () => {
return next(null, receipt); return next(null, receipt);
@ -244,35 +251,6 @@ class Deploy {
], callback); ], 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'); let async = require('async');
//require("../utils/debug_util.js")(__filename, async);
let Deploy = require('./deploy.js');
class DeployManager { class DeployManager {
constructor(options) { constructor(options) {
const self = this;
this.config = options.config; this.config = options.config;
this.logger = options.logger; this.logger = options.logger;
this.blockchainConfig = this.config.blockchainConfig; this.blockchainConfig = this.config.blockchainConfig;
@ -17,6 +16,41 @@ class DeployManager {
this.fatalErrors = false; this.fatalErrors = false;
this.deployOnlyOnConfig = false; this.deployOnlyOnConfig = false;
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : 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) { deployContracts(done) {
@ -62,18 +96,7 @@ class DeployManager {
}, },
function deployAllContracts(callback) { function deployAllContracts(callback) {
let deploy = new Deploy({ self.deployAll(function (err) {
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) {
if (!err) { if (!err) {
self.events.emit('contractsDeployed', self.contractsManager); self.events.emit('contractsDeployed', self.contractsManager);
} }
@ -84,6 +107,7 @@ class DeployManager {
}); });
}, },
function runAfterDeploy(callback) { function runAfterDeploy(callback) {
self.events.emit('contracts:deploy:afterAll');
self.plugins.runActionsForEvent('contracts:deploy:afterAll', callback); self.plugins.runActionsForEvent('contracts:deploy:afterAll', callback);
} }
], function (err, _result) { ], function (err, _result) {

View File

@ -6,6 +6,7 @@ const Config = require('./config.js');
const Blockchain = require('../contracts/blockchain.js'); const Blockchain = require('../contracts/blockchain.js');
const Compiler = require('../contracts/compiler.js'); const Compiler = require('../contracts/compiler.js');
const ContractsManager = require('../contracts/contracts.js'); const ContractsManager = require('../contracts/contracts.js');
const ContractDeployer = require('../contracts/contract_deployer.js');
const DeployManager = require('../contracts/deploy_manager.js'); const DeployManager = require('../contracts/deploy_manager.js');
const CodeGenerator = require('../contracts/code_generator.js'); const CodeGenerator = require('../contracts/code_generator.js');
const ServicesMonitor = require('./services_monitor.js'); const ServicesMonitor = require('./services_monitor.js');
@ -242,6 +243,14 @@ class Engine {
onlyCompile: options.onlyCompile 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) { this.events.on('file-event', function (fileType) {
clearTimeout(self.fileTimeout); clearTimeout(self.fileTimeout);
self.fileTimeout = setTimeout(() => { self.fileTimeout = setTimeout(() => {
@ -255,8 +264,8 @@ class Engine {
// because the contractsManager config is corrupted after a deploy // because the contractsManager config is corrupted after a deploy
if (fileType === 'contract' || fileType === 'config') { if (fileType === 'contract' || fileType === 'config') {
self.config.reloadConfig(); self.config.reloadConfig();
self.deployManager.deployContracts(function () {
}); self.events.request('deploy:contracts', () => {});
} }
}, 50); }, 50);
}); });

View File

@ -167,7 +167,7 @@ class Embark {
engine.events.on('check:backOnline:Ethereum', function () { engine.events.on('check:backOnline:Ethereum', function () {
engine.logger.info(__('Ethereum node detected') + '..'); engine.logger.info(__('Ethereum node detected') + '..');
engine.config.reloadConfig(); engine.config.reloadConfig();
engine.deployManager.deployContracts(function () { engine.events.request('deploy:contracts', function() {
engine.logger.info(__('Deployment Done')); engine.logger.info(__('Deployment Done'));
}); });
}); });
@ -191,6 +191,7 @@ class Embark {
engine.logger.info(__("Ready").underline); engine.logger.info(__("Ready").underline);
engine.events.emit("status", __("Ready").green); engine.events.emit("status", __("Ready").green);
}); });
if (options.runWebserver) { if (options.runWebserver) {
engine.startService("webServer", { engine.startService("webServer", {
host: options.serverHost, host: options.serverHost,
@ -255,7 +256,7 @@ class Embark {
callback(); callback();
}, },
function deploy(callback) { function deploy(callback) {
engine.deployManager.deployContracts(function (err) { engine.events.request('deploy:contracts', function(err) {
callback(err); callback(err);
}); });
} }
@ -306,7 +307,7 @@ class Embark {
engine.startService("deployment", {onlyCompile: true}); engine.startService("deployment", {onlyCompile: true});
engine.startService("codeGenerator"); engine.startService("codeGenerator");
engine.deployManager.deployContracts(function (err) { engine.events.request('deploy:contracts', function(err) {
callback(err); callback(err);
}); });
} }
@ -418,7 +419,6 @@ class Embark {
callback(); callback();
}, },
function deploy(callback) { function deploy(callback) {
// 2. upload to storage (outputDone event triggered after webpack finished)
engine.events.on('outputDone', function () { engine.events.on('outputDone', function () {
cmdPlugin.uploadCmds[0].cb() cmdPlugin.uploadCmds[0].cb()
.then((success) => { .then((success) => {
@ -427,8 +427,7 @@ class Embark {
.catch(callback); .catch(callback);
}); });
// 1. build the contracts and dapp webpack engine.events.request('deploy:contracts', function(err) {
engine.deployManager.deployContracts(function (err) {
engine.logger.info(__("finished deploying").underline); engine.logger.info(__("finished deploying").underline);
if(err){ if(err){
callback(err); callback(err);

View File

@ -88,7 +88,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
self.engine.contractsManager.gasLimit = 6000000; self.engine.contractsManager.gasLimit = 6000000;
self.engine.deployManager.fatalErrors = true; self.engine.deployManager.fatalErrors = true;
self.engine.deployManager.deployOnlyOnConfig = true; self.engine.deployManager.deployOnlyOnConfig = true;
self.engine.deployManager.deployContracts(function(err, _result) { self.engine.events.request('deploy:contracts', function(err) {
if (err) { if (err) {
callback(err); callback(err);
} }