2018-05-18 17:15:49 -04:00
|
|
|
const async = require('async');
|
|
|
|
|
2018-05-23 09:33:32 -04:00
|
|
|
const utils = require('../utils/utils');
|
2018-06-04 15:36:43 -04:00
|
|
|
const IPC = require('./ipc');
|
2018-05-22 15:46:02 -04:00
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
class Engine {
|
|
|
|
constructor(options) {
|
|
|
|
this.env = options.env;
|
2018-05-18 15:48:28 -04:00
|
|
|
this.client = options.client;
|
|
|
|
this.locale = options.locale;
|
2017-03-30 20:12:39 +09:00
|
|
|
this.embarkConfig = options.embarkConfig;
|
|
|
|
this.interceptLogs = options.interceptLogs;
|
2017-03-31 07:34:43 -04:00
|
|
|
this.version = options.version;
|
2018-04-19 14:25:43 +10:00
|
|
|
this.logFile = options.logFile;
|
2018-04-17 16:17:59 +10:00
|
|
|
this.logLevel = options.logLevel;
|
2018-04-19 14:25:43 +10:00
|
|
|
this.events = options.events;
|
2018-04-25 10:34:17 -04:00
|
|
|
this.context = options.context;
|
2018-06-15 09:37:52 +10:00
|
|
|
this.useDashboard = options.useDashboard;
|
2018-07-15 23:49:24 -05:00
|
|
|
this.webServerConfig = options.webServerConfig;
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
2017-03-03 01:22:12 -05:00
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
init(_options) {
|
2018-06-02 09:54:32 -04:00
|
|
|
const Events = require('./events.js');
|
|
|
|
const Logger = require('./logger.js');
|
|
|
|
const Config = require('./config.js');
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
let options = _options || {};
|
2018-04-19 14:25:43 +10:00
|
|
|
this.events = options.events || this.events || new Events();
|
|
|
|
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
|
2018-07-15 23:49:24 -05:00
|
|
|
this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context, webServerConfig: this.webServerConfig});
|
2017-03-30 20:38:14 +09:00
|
|
|
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
|
|
|
|
this.plugins = this.config.plugins;
|
2018-07-10 08:49:08 -04:00
|
|
|
this.isDev = this.config && this.config.blockchainConfig && (this.config.blockchainConfig.isDev || this.config.blockchainConfig.default);
|
2017-03-30 20:38:14 +09:00
|
|
|
|
2018-04-30 15:56:43 +10:00
|
|
|
if (this.interceptLogs || this.interceptLogs === undefined) {
|
2018-07-27 17:20:36 -04:00
|
|
|
utils.interceptLogs(console, this.logger);
|
2018-04-30 15:56:43 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 15:39:30 -05:00
|
|
|
registerModule(moduleName, options) {
|
2018-05-30 12:26:49 -04:00
|
|
|
this.plugins.loadInternalPlugin(moduleName, options || {});
|
2017-12-16 15:39:30 -05:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
startService(serviceName, _options) {
|
|
|
|
let options = _options || {};
|
|
|
|
|
|
|
|
let services = {
|
2018-06-01 12:27:12 -04:00
|
|
|
"serviceMonitor": this.serviceMonitor,
|
2017-03-30 20:38:14 +09:00
|
|
|
"pipeline": this.pipelineService,
|
2018-05-23 11:16:56 -04:00
|
|
|
"codeRunner": this.codeRunnerService,
|
2017-08-03 19:29:09 -04:00
|
|
|
"codeGenerator": this.codeGeneratorService,
|
2017-03-30 20:38:14 +09:00
|
|
|
"deployment": this.deploymentService,
|
|
|
|
"fileWatcher": this.fileWatchService,
|
|
|
|
"webServer": this.webServerService,
|
2018-05-25 12:25:02 -05:00
|
|
|
"namingSystem": this.namingSystem,
|
2017-12-30 15:52:51 -05:00
|
|
|
"web3": this.web3Service,
|
2018-04-24 10:27:11 +10:00
|
|
|
"libraryManager": this.libraryManagerService,
|
2018-07-19 23:31:28 +03:00
|
|
|
"processManager": this.processManagerService,
|
2018-07-20 20:03:55 +03:00
|
|
|
"storage": this.storageService,
|
|
|
|
"graph": this.graphService
|
2017-03-30 20:38:14 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
let service = services[serviceName];
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
throw new Error("unknown service: " + serviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to be careful with circular references due to passing the web3 object
|
|
|
|
//this.logger.trace("calling: " + serviceName + "(" + JSON.stringify(options) + ")");
|
|
|
|
return service.apply(this, [options]);
|
|
|
|
}
|
|
|
|
|
2018-07-19 23:31:28 +03:00
|
|
|
processManagerService(_options) {
|
2018-07-27 17:33:50 -04:00
|
|
|
const ProcessManager = require('./processes/processManager.js');
|
2018-07-26 12:37:33 -04:00
|
|
|
this.processManager = new ProcessManager({
|
2018-07-19 23:31:28 +03:00
|
|
|
events: this.events,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-20 20:03:55 +03:00
|
|
|
graphService(_options) {
|
2018-07-30 14:31:24 -04:00
|
|
|
this.registerModule('graph');
|
2018-07-20 20:03:55 +03:00
|
|
|
}
|
|
|
|
|
2017-10-14 10:13:30 -04:00
|
|
|
pipelineService(_options) {
|
2018-05-10 10:10:09 -04:00
|
|
|
const self = this;
|
|
|
|
this.events.emit("status", "Building Assets");
|
2018-06-02 09:54:32 -04:00
|
|
|
const Pipeline = require('../pipeline/pipeline.js');
|
2018-05-10 10:10:09 -04:00
|
|
|
const pipeline = new Pipeline({
|
2018-07-19 20:47:11 -05:00
|
|
|
env: this.env,
|
2018-05-10 10:10:09 -04:00
|
|
|
buildDir: this.config.buildDir,
|
|
|
|
contractsFiles: this.config.contractsFiles,
|
|
|
|
assetFiles: this.config.assetFiles,
|
|
|
|
events: this.events,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins
|
|
|
|
});
|
|
|
|
this.events.on('code-generator-ready', function () {
|
|
|
|
self.events.request('code', function (abi, contractsJSON) {
|
2018-05-15 14:42:06 -04:00
|
|
|
pipeline.build(abi, contractsJSON, null, () => {
|
2018-05-10 10:10:09 -04:00
|
|
|
self.events.emit('outputDone');
|
2017-07-06 18:48:20 -04:00
|
|
|
});
|
2017-07-05 08:35:51 -04:00
|
|
|
});
|
2017-03-30 20:38:14 +09:00
|
|
|
});
|
2017-03-16 07:31:52 -04:00
|
|
|
}
|
2017-03-30 20:38:14 +09:00
|
|
|
|
2018-06-01 12:27:12 -04:00
|
|
|
serviceMonitor() {
|
|
|
|
const self = this;
|
2018-06-02 09:54:32 -04:00
|
|
|
const ServicesMonitor = require('./services_monitor.js');
|
2018-06-01 12:27:12 -04:00
|
|
|
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins});
|
|
|
|
this.servicesMonitor.addCheck('embarkVersion', function (cb) {
|
|
|
|
return cb({name: 'Embark ' + self.version, status: 'on'});
|
|
|
|
}, 0);
|
|
|
|
this.servicesMonitor.startMonitor();
|
|
|
|
}
|
|
|
|
|
2018-05-28 11:10:09 -04:00
|
|
|
namingSystem(_options) {
|
2018-05-30 12:26:49 -04:00
|
|
|
this.registerModule('ens');
|
2018-05-28 11:10:09 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 11:16:56 -04:00
|
|
|
codeRunnerService(_options) {
|
2018-07-20 19:37:50 +03:00
|
|
|
const CodeRunner = require('./modules/coderunner/codeRunner.js');
|
2018-05-21 17:22:19 -04:00
|
|
|
this.codeRunner = new CodeRunner({
|
2018-05-23 11:16:56 -04:00
|
|
|
plugins: this.plugins,
|
|
|
|
events: this.events,
|
|
|
|
logger: this.logger
|
2018-05-21 17:22:19 -04:00
|
|
|
});
|
2018-05-23 11:16:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
codeGeneratorService(_options) {
|
|
|
|
let self = this;
|
2018-05-21 17:22:19 -04:00
|
|
|
|
2018-07-27 14:54:25 -04:00
|
|
|
this.registerModule('code_generator', {plugins: self.plugins, env: self.env});
|
2018-05-23 11:16:13 -04:00
|
|
|
|
|
|
|
const generateCode = function () {
|
2018-07-27 14:54:25 -04:00
|
|
|
self.events.request("code-generator:embarkjs:build", () => {
|
2018-01-10 10:43:25 -05:00
|
|
|
self.events.emit('code-generator-ready');
|
|
|
|
});
|
2017-03-30 20:38:14 +09:00
|
|
|
};
|
2018-05-31 10:09:26 -04:00
|
|
|
const cargo = async.cargo((_tasks, callback) => {
|
|
|
|
generateCode();
|
2018-05-15 14:42:06 -04:00
|
|
|
self.events.once('outputDone', callback);
|
|
|
|
});
|
2018-05-30 06:56:51 -04:00
|
|
|
const addToCargo = function () {
|
2018-05-31 10:09:26 -04:00
|
|
|
cargo.push({});
|
2018-05-15 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.events.on('contractsDeployed', addToCargo);
|
|
|
|
this.events.on('blockchainDisabled', addToCargo);
|
|
|
|
this.events.on('asset-changed', addToCargo);
|
2017-03-03 01:22:12 -05:00
|
|
|
}
|
2017-03-03 21:48:32 -05:00
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
deploymentService(options) {
|
|
|
|
let self = this;
|
2017-12-16 15:39:30 -05:00
|
|
|
|
2018-07-20 20:22:12 +03:00
|
|
|
this.registerModule('compiler', {plugins: self.plugins});
|
2018-05-18 23:19:08 -04:00
|
|
|
|
2018-06-04 18:15:27 -04:00
|
|
|
this.ipc = new IPC({logger: this.logger, ipcRole: options.ipcRole});
|
2018-06-11 16:40:14 -04:00
|
|
|
if (this.ipc.isServer()) {
|
|
|
|
this.ipc.serve();
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:37:52 +10:00
|
|
|
this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard});
|
2018-05-30 12:26:49 -04:00
|
|
|
this.registerModule('vyper');
|
|
|
|
this.registerModule('profiler');
|
|
|
|
this.registerModule('deploytracker');
|
|
|
|
this.registerModule('specialconfigs');
|
2018-06-08 14:36:35 -04:00
|
|
|
this.registerModule('console_listener', {ipc: this.ipc});
|
2018-05-20 10:46:36 -04:00
|
|
|
|
2018-07-27 15:36:16 -04:00
|
|
|
// TODO: need to refactor dependencies before moving into a module
|
2018-06-02 09:54:32 -04:00
|
|
|
const ContractsManager = require('../contracts/contracts.js');
|
2018-02-21 18:43:34 -05:00
|
|
|
this.contractsManager = new ContractsManager({
|
|
|
|
contractFiles: this.config.contractsFiles,
|
|
|
|
contractsConfig: this.config.contractsConfig,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins,
|
2018-04-30 15:56:43 +10:00
|
|
|
gasLimit: false,
|
|
|
|
events: this.events
|
2018-02-21 18:43:34 -05:00
|
|
|
});
|
|
|
|
|
2018-07-27 16:03:15 -04:00
|
|
|
this.registerModule('deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile});
|
2017-03-30 20:38:14 +09:00
|
|
|
|
2018-04-30 15:56:43 +10:00
|
|
|
this.events.on('file-event', function (fileType) {
|
2018-05-15 15:41:24 -04:00
|
|
|
clearTimeout(self.fileTimeout);
|
|
|
|
self.fileTimeout = setTimeout(() => {
|
|
|
|
// TODO: still need to redeploy contracts because the original contracts
|
|
|
|
// config is being corrupted
|
|
|
|
if (fileType === 'asset') {
|
|
|
|
// Throttle file changes so we re-write only once for all files
|
2018-05-15 14:42:06 -04:00
|
|
|
self.events.emit('asset-changed', self.contractsManager);
|
2018-05-15 15:41:24 -04:00
|
|
|
}
|
|
|
|
// TODO: for now need to deploy on asset changes as well
|
|
|
|
// because the contractsManager config is corrupted after a deploy
|
|
|
|
if (fileType === 'contract' || fileType === 'config') {
|
|
|
|
self.config.reloadConfig();
|
2018-05-29 17:23:29 -04:00
|
|
|
|
|
|
|
self.events.request('deploy:contracts', () => {});
|
2018-05-15 15:41:24 -04:00
|
|
|
}
|
|
|
|
}, 50);
|
2017-03-30 20:12:39 +09:00
|
|
|
});
|
|
|
|
}
|
2017-03-10 22:00:30 -05:00
|
|
|
|
2017-10-14 10:13:30 -04:00
|
|
|
fileWatchService(_options) {
|
2018-02-27 15:49:21 -05:00
|
|
|
this.events.emit("status", "Watching for changes");
|
2018-06-02 09:54:32 -04:00
|
|
|
const Watch = require('../pipeline/watch.js');
|
2018-04-30 15:56:43 +10:00
|
|
|
this.watch = new Watch({logger: this.logger, events: this.events});
|
|
|
|
this.watch.start();
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
|
|
|
|
2018-07-15 23:49:24 -05:00
|
|
|
webServerService(_options) {
|
|
|
|
this.registerModule('webserver', _options);
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
|
|
|
|
2018-05-25 17:13:57 +10:00
|
|
|
storageService(_options) {
|
2018-07-08 23:41:37 +03:00
|
|
|
this.registerModule('storage', {plugins: this.plugins});
|
2018-07-08 20:40:06 +03:00
|
|
|
this.registerModule('ipfs');
|
|
|
|
this.registerModule('swarm');
|
2018-04-24 10:27:11 +10:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
web3Service(options) {
|
2018-07-20 15:21:23 +03:00
|
|
|
this.registerModule('blockchain_process', {
|
|
|
|
locale: this.locale,
|
|
|
|
isDev: this.isDev
|
|
|
|
});
|
|
|
|
|
2018-07-27 16:54:33 -04:00
|
|
|
this.registerModule('blockchain_connector', {
|
2018-05-18 14:58:05 -04:00
|
|
|
isDev: this.isDev,
|
2018-05-18 18:31:47 -04:00
|
|
|
web3: options.web3
|
2018-07-27 16:54:33 -04:00
|
|
|
});
|
2018-04-10 15:14:00 -04:00
|
|
|
|
2018-07-27 15:47:41 -04:00
|
|
|
this.registerModule('whisper');
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
2017-12-30 15:52:51 -05:00
|
|
|
|
2017-12-30 17:07:13 -05:00
|
|
|
libraryManagerService(_options) {
|
2018-07-20 21:14:52 +03:00
|
|
|
this.registerModule('library_manager');
|
2017-12-30 15:52:51 -05:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
2017-03-11 10:29:45 -05:00
|
|
|
|
2017-03-03 01:22:12 -05:00
|
|
|
module.exports = Engine;
|