2017-03-29 15:37:30 +00:00
|
|
|
let async = require('async');
|
2018-01-05 20:10:47 +00:00
|
|
|
//require("../utils/debug_util.js")(__filename, async);
|
2017-03-29 15:37:30 +00:00
|
|
|
let Deploy = require('./deploy.js');
|
2017-03-01 01:42:03 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class DeployManager {
|
|
|
|
constructor(options) {
|
|
|
|
this.config = options.config;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.blockchainConfig = this.config.blockchainConfig;
|
2017-12-21 16:21:36 +00:00
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
this.events = options.events;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.plugins = options.plugins;
|
2018-05-18 22:31:47 +00:00
|
|
|
this.blockchain = options.blockchain;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false;
|
2018-02-21 23:43:34 +00:00
|
|
|
this.contractsManager = options.contractsManager;
|
2018-01-13 16:38:10 +00:00
|
|
|
this.gasLimit = false;
|
|
|
|
this.fatalErrors = false;
|
2018-03-11 12:28:03 +00:00
|
|
|
this.deployOnlyOnConfig = false;
|
2018-03-22 19:09:01 +00:00
|
|
|
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false;
|
2017-03-01 01:42:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 17:48:07 +00:00
|
|
|
deployContracts(compileOnlyIfNeeded, done) {
|
|
|
|
if (typeof compileOnlyIfNeeded === 'function') {
|
|
|
|
done = compileOnlyIfNeeded;
|
|
|
|
compileOnlyIfNeeded = false;
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
let self = this;
|
|
|
|
|
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__("Blockchain component is disabled in the config").underline);
|
2017-03-31 11:34:43 +00:00
|
|
|
this.events.emit('blockchainDisabled', {});
|
2017-03-30 11:12:39 +00:00
|
|
|
return done();
|
2017-03-11 15:29:45 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function buildContracts(callback) {
|
2018-05-29 17:48:07 +00:00
|
|
|
if (compileOnlyIfNeeded) {
|
|
|
|
if (self.contractsManager.compiledContracts && Object.keys(self.contractsManager.compiledContracts).length) {
|
|
|
|
// Contracts have already been compiled
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('compiling');
|
2018-03-11 12:28:03 +00:00
|
|
|
self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor
|
2018-05-20 13:15:28 +00:00
|
|
|
self.contractsManager.build(() => {
|
|
|
|
callback();
|
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
},
|
2018-05-20 14:46:36 +00:00
|
|
|
|
|
|
|
// TODO: shouldn't be necessary
|
2018-05-20 13:15:28 +00:00
|
|
|
function checkCompileOnly(callback){
|
2018-03-22 19:09:01 +00:00
|
|
|
if(self.onlyCompile){
|
2018-05-20 13:15:28 +00:00
|
|
|
self.events.emit('contractsDeployed', self.contractsManager);
|
2018-03-22 19:09:01 +00:00
|
|
|
return done();
|
|
|
|
}
|
2018-05-20 13:15:28 +00:00
|
|
|
return callback();
|
2018-03-22 19:09:01 +00:00
|
|
|
},
|
2018-05-20 10:46:12 +00:00
|
|
|
|
2018-05-20 13:08:03 +00:00
|
|
|
// TODO: could be implemented as an event (beforeDeployAll)
|
2018-05-20 13:15:28 +00:00
|
|
|
function checkIsConnectedToBlockchain(callback) {
|
2018-05-23 13:33:32 +00:00
|
|
|
self.blockchain.onReady(() => {
|
2018-05-22 21:34:54 +00:00
|
|
|
self.blockchain.assertNodeConnection((err) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2018-01-05 20:10:47 +00:00
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
},
|
2018-05-20 13:08:03 +00:00
|
|
|
|
|
|
|
// TODO: this can be done on the fly or as part of the initialization
|
2018-05-20 13:15:28 +00:00
|
|
|
function determineDefaultAccount(callback) {
|
2018-05-20 13:08:03 +00:00
|
|
|
self.blockchain.determineDefaultAccount((err) => {
|
2018-05-20 13:15:28 +00:00
|
|
|
callback(err);
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
|
|
|
},
|
2018-05-20 10:46:12 +00:00
|
|
|
|
2018-05-20 13:15:28 +00:00
|
|
|
function deployAllContracts(callback) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let deploy = new Deploy({
|
2018-05-18 22:31:47 +00:00
|
|
|
blockchain: self.blockchain,
|
2018-05-20 13:15:28 +00:00
|
|
|
contractsManager: self.contractsManager,
|
2017-03-30 11:12:39 +00:00
|
|
|
logger: self.logger,
|
2018-02-27 20:40:05 +00:00
|
|
|
events: self.events,
|
2017-03-30 11:12:39 +00:00
|
|
|
chainConfig: self.chainConfig,
|
2018-01-13 16:38:10 +00:00
|
|
|
env: self.config.env,
|
2018-01-17 23:04:19 +00:00
|
|
|
plugins: self.plugins,
|
2018-01-13 16:38:10 +00:00
|
|
|
gasLimit: self.gasLimit
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
2018-01-05 20:10:47 +00:00
|
|
|
|
2018-05-19 02:40:47 +00:00
|
|
|
deploy.deployAll(function (err) {
|
|
|
|
if (!err) {
|
2018-05-20 13:15:28 +00:00
|
|
|
self.events.emit('contractsDeployed', self.contractsManager);
|
2018-05-19 02:40:47 +00:00
|
|
|
}
|
|
|
|
if (err && self.fatalErrors) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-05-20 13:15:28 +00:00
|
|
|
callback();
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
2017-12-21 16:21:36 +00:00
|
|
|
},
|
2018-05-20 14:46:36 +00:00
|
|
|
function runAfterDeploy(callback) {
|
|
|
|
let afterDeployPlugins = self.plugins.getPluginsProperty('afterContractsDeployActions', 'afterContractsDeployActions');
|
2017-12-21 16:21:36 +00:00
|
|
|
|
2018-05-20 14:46:36 +00:00
|
|
|
async.eachLimit(afterDeployPlugins, 1, function(plugin, nextEach) {
|
|
|
|
plugin.call(plugin, nextEach);
|
|
|
|
}, () => {
|
|
|
|
callback();
|
2017-12-21 16:21:36 +00:00
|
|
|
});
|
2017-02-24 13:20:03 +00:00
|
|
|
}
|
2018-05-20 13:15:28 +00:00
|
|
|
], function (err, _result) {
|
|
|
|
done(err);
|
2017-02-24 13:20:03 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
}
|
2017-02-24 13:20:03 +00:00
|
|
|
|
|
|
|
module.exports = DeployManager;
|