2018-05-18 21:15:49 +00:00
|
|
|
const async = require('async');
|
|
|
|
|
2018-05-23 13:33:32 +00:00
|
|
|
const utils = require('../utils/utils');
|
2018-06-04 19:36:43 +00:00
|
|
|
const IPC = require('./ipc');
|
2018-05-22 19:46:02 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Engine {
|
|
|
|
constructor(options) {
|
|
|
|
this.env = options.env;
|
2018-05-18 19:48:28 +00:00
|
|
|
this.client = options.client;
|
|
|
|
this.locale = options.locale;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.embarkConfig = options.embarkConfig;
|
|
|
|
this.interceptLogs = options.interceptLogs;
|
2017-03-31 11:34:43 +00:00
|
|
|
this.version = options.version;
|
2018-04-19 04:25:43 +00:00
|
|
|
this.logFile = options.logFile;
|
2018-04-17 06:17:59 +00:00
|
|
|
this.logLevel = options.logLevel;
|
2018-04-19 04:25:43 +00:00
|
|
|
this.events = options.events;
|
2018-04-25 14:34:17 +00:00
|
|
|
this.context = options.context;
|
2018-06-14 23:37:52 +00:00
|
|
|
this.useDashboard = options.useDashboard;
|
2018-07-16 04:49:24 +00:00
|
|
|
this.webServerConfig = options.webServerConfig;
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2017-03-03 06:22:12 +00:00
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
init(_options) {
|
2018-06-02 13:54:32 +00:00
|
|
|
const Events = require('./events.js');
|
|
|
|
const Logger = require('./logger.js');
|
|
|
|
const Config = require('./config.js');
|
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
let options = _options || {};
|
2018-04-19 04:25:43 +00: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-16 04:49:24 +00:00
|
|
|
this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context, webServerConfig: this.webServerConfig});
|
2017-03-30 11:38:14 +00:00
|
|
|
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
|
|
|
|
this.plugins = this.config.plugins;
|
2018-07-10 12:49:08 +00:00
|
|
|
this.isDev = this.config && this.config.blockchainConfig && (this.config.blockchainConfig.isDev || this.config.blockchainConfig.default);
|
2017-03-30 11:38:14 +00:00
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
if (this.interceptLogs || this.interceptLogs === undefined) {
|
2018-05-24 20:34:27 +00:00
|
|
|
this.doInterceptLogs();
|
2018-04-30 05:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doInterceptLogs() {
|
|
|
|
var self = this;
|
|
|
|
let context = {};
|
|
|
|
context.console = console;
|
|
|
|
|
|
|
|
context.console.log = function() {
|
2018-05-23 13:33:32 +00:00
|
|
|
self.logger.info(utils.normalizeInput(arguments));
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
context.console.warn = function() {
|
2018-05-23 13:33:32 +00:00
|
|
|
self.logger.warn(utils.normalizeInput(arguments));
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
context.console.info = function() {
|
2018-05-23 13:33:32 +00:00
|
|
|
self.logger.info(utils.normalizeInput(arguments));
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
context.console.debug = function() {
|
|
|
|
// TODO: ue JSON.stringify
|
2018-05-23 13:33:32 +00:00
|
|
|
self.logger.debug(utils.normalizeInput(arguments));
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
context.console.trace = function() {
|
2018-05-23 13:33:32 +00:00
|
|
|
self.logger.trace(utils.normalizeInput(arguments));
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
context.console.dir = function() {
|
2018-05-23 13:33:32 +00:00
|
|
|
self.logger.dir(utils.normalizeInput(arguments));
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 20:39:30 +00:00
|
|
|
registerModule(moduleName, options) {
|
2018-05-30 16:26:49 +00:00
|
|
|
this.plugins.loadInternalPlugin(moduleName, options || {});
|
2017-12-16 20:39:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
startService(serviceName, _options) {
|
|
|
|
let options = _options || {};
|
|
|
|
|
|
|
|
let services = {
|
2018-06-01 16:27:12 +00:00
|
|
|
"serviceMonitor": this.serviceMonitor,
|
2017-03-30 11:38:14 +00:00
|
|
|
"pipeline": this.pipelineService,
|
2018-05-23 15:16:56 +00:00
|
|
|
"codeRunner": this.codeRunnerService,
|
2017-08-03 23:29:09 +00:00
|
|
|
"codeGenerator": this.codeGeneratorService,
|
2017-03-30 11:38:14 +00:00
|
|
|
"deployment": this.deploymentService,
|
|
|
|
"fileWatcher": this.fileWatchService,
|
|
|
|
"webServer": this.webServerService,
|
2018-05-25 17:25:02 +00:00
|
|
|
"namingSystem": this.namingSystem,
|
2017-12-30 20:52:51 +00:00
|
|
|
"web3": this.web3Service,
|
2018-04-24 00:27:11 +00:00
|
|
|
"libraryManager": this.libraryManagerService,
|
2018-07-19 20:31:28 +00:00
|
|
|
"processManager": this.processManagerService,
|
2018-07-20 17:03:55 +00:00
|
|
|
"storage": this.storageService,
|
|
|
|
"graph": this.graphService
|
2017-03-30 11:38:14 +00: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 20:31:28 +00:00
|
|
|
processManagerService(_options) {
|
2018-07-20 15:57:41 +00:00
|
|
|
const ProcessManager = require('../processes/processManager.js');
|
2018-07-26 16:37:33 +00:00
|
|
|
this.processManager = new ProcessManager({
|
2018-07-19 20:31:28 +00:00
|
|
|
events: this.events,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-20 17:03:55 +00:00
|
|
|
graphService(_options) {
|
|
|
|
this.registerModule('graph', {engine: this});
|
|
|
|
}
|
|
|
|
|
2017-10-14 14:13:30 +00:00
|
|
|
pipelineService(_options) {
|
2018-05-10 14:10:09 +00:00
|
|
|
const self = this;
|
|
|
|
this.events.emit("status", "Building Assets");
|
2018-06-02 13:54:32 +00:00
|
|
|
const Pipeline = require('../pipeline/pipeline.js');
|
2018-05-10 14:10:09 +00:00
|
|
|
const pipeline = new Pipeline({
|
2018-07-20 01:47:11 +00:00
|
|
|
env: this.env,
|
2018-05-10 14:10:09 +00: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 18:42:06 +00:00
|
|
|
pipeline.build(abi, contractsJSON, null, () => {
|
2018-05-10 14:10:09 +00:00
|
|
|
self.events.emit('outputDone');
|
2017-07-06 22:48:20 +00:00
|
|
|
});
|
2017-07-05 12:35:51 +00:00
|
|
|
});
|
2017-03-30 11:38:14 +00:00
|
|
|
});
|
2017-03-16 11:31:52 +00:00
|
|
|
}
|
2017-03-30 11:38:14 +00:00
|
|
|
|
2018-06-01 16:27:12 +00:00
|
|
|
serviceMonitor() {
|
|
|
|
const self = this;
|
2018-06-02 13:54:32 +00:00
|
|
|
const ServicesMonitor = require('./services_monitor.js');
|
2018-06-01 16:27:12 +00: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 15:10:09 +00:00
|
|
|
namingSystem(_options) {
|
2018-05-30 16:26:49 +00:00
|
|
|
this.registerModule('ens');
|
2018-05-28 15:10:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 15:16:56 +00:00
|
|
|
codeRunnerService(_options) {
|
2018-07-20 16:37:50 +00:00
|
|
|
const CodeRunner = require('./modules/coderunner/codeRunner.js');
|
2018-05-21 21:22:19 +00:00
|
|
|
this.codeRunner = new CodeRunner({
|
2018-05-23 15:16:56 +00:00
|
|
|
plugins: this.plugins,
|
|
|
|
events: this.events,
|
|
|
|
logger: this.logger
|
2018-05-21 21:22:19 +00:00
|
|
|
});
|
2018-05-23 15:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
codeGeneratorService(_options) {
|
|
|
|
let self = this;
|
2018-05-21 21:22:19 +00:00
|
|
|
|
2018-07-27 18:54:25 +00:00
|
|
|
this.registerModule('code_generator', {plugins: self.plugins, env: self.env});
|
2018-05-23 15:16:13 +00:00
|
|
|
|
|
|
|
const generateCode = function () {
|
2018-07-27 18:54:25 +00:00
|
|
|
self.events.request("code-generator:embarkjs:build", () => {
|
2018-01-10 15:43:25 +00:00
|
|
|
self.events.emit('code-generator-ready');
|
|
|
|
});
|
2017-03-30 11:38:14 +00:00
|
|
|
};
|
2018-05-31 14:09:26 +00:00
|
|
|
const cargo = async.cargo((_tasks, callback) => {
|
|
|
|
generateCode();
|
2018-05-15 18:42:06 +00:00
|
|
|
self.events.once('outputDone', callback);
|
|
|
|
});
|
2018-05-30 10:56:51 +00:00
|
|
|
const addToCargo = function () {
|
2018-05-31 14:09:26 +00:00
|
|
|
cargo.push({});
|
2018-05-15 18:42:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.events.on('contractsDeployed', addToCargo);
|
|
|
|
this.events.on('blockchainDisabled', addToCargo);
|
|
|
|
this.events.on('asset-changed', addToCargo);
|
2017-03-03 06:22:12 +00:00
|
|
|
}
|
2017-03-04 02:48:32 +00:00
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
deploymentService(options) {
|
|
|
|
let self = this;
|
2017-12-16 20:39:30 +00:00
|
|
|
|
2018-07-20 17:22:12 +00:00
|
|
|
this.registerModule('compiler', {plugins: self.plugins});
|
2018-05-19 03:19:08 +00:00
|
|
|
|
2018-06-04 22:15:27 +00:00
|
|
|
this.ipc = new IPC({logger: this.logger, ipcRole: options.ipcRole});
|
2018-06-11 20:40:14 +00:00
|
|
|
if (this.ipc.isServer()) {
|
|
|
|
this.ipc.serve();
|
|
|
|
}
|
|
|
|
|
2018-06-14 23:37:52 +00:00
|
|
|
this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard});
|
2018-05-30 16:26:49 +00:00
|
|
|
this.registerModule('vyper');
|
|
|
|
this.registerModule('profiler');
|
|
|
|
this.registerModule('deploytracker');
|
|
|
|
this.registerModule('specialconfigs');
|
2018-06-08 18:36:35 +00:00
|
|
|
this.registerModule('console_listener', {ipc: this.ipc});
|
2018-05-20 14:46:36 +00:00
|
|
|
|
2018-07-27 19:36:16 +00:00
|
|
|
// TODO: need to refactor dependencies before moving into a module
|
2018-06-02 13:54:32 +00:00
|
|
|
const ContractsManager = require('../contracts/contracts.js');
|
2018-02-21 23:43:34 +00:00
|
|
|
this.contractsManager = new ContractsManager({
|
|
|
|
contractFiles: this.config.contractsFiles,
|
|
|
|
contractsConfig: this.config.contractsConfig,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins,
|
2018-04-30 05:56:43 +00:00
|
|
|
gasLimit: false,
|
|
|
|
events: this.events
|
2018-02-21 23:43:34 +00:00
|
|
|
});
|
|
|
|
|
2018-07-27 20:03:15 +00:00
|
|
|
this.registerModule('deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile});
|
2017-03-30 11:38:14 +00:00
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
this.events.on('file-event', function (fileType) {
|
2018-05-15 19:41:24 +00: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 18:42:06 +00:00
|
|
|
self.events.emit('asset-changed', self.contractsManager);
|
2018-05-15 19:41:24 +00: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 21:23:29 +00:00
|
|
|
|
|
|
|
self.events.request('deploy:contracts', () => {});
|
2018-05-15 19:41:24 +00:00
|
|
|
}
|
|
|
|
}, 50);
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-11 03:00:30 +00:00
|
|
|
|
2017-10-14 14:13:30 +00:00
|
|
|
fileWatchService(_options) {
|
2018-02-27 20:49:21 +00:00
|
|
|
this.events.emit("status", "Watching for changes");
|
2018-06-02 13:54:32 +00:00
|
|
|
const Watch = require('../pipeline/watch.js');
|
2018-04-30 05:56:43 +00:00
|
|
|
this.watch = new Watch({logger: this.logger, events: this.events});
|
|
|
|
this.watch.start();
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 04:49:24 +00:00
|
|
|
webServerService(_options) {
|
|
|
|
this.registerModule('webserver', _options);
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 07:13:57 +00:00
|
|
|
storageService(_options) {
|
2018-07-08 20:41:37 +00:00
|
|
|
this.registerModule('storage', {plugins: this.plugins});
|
2018-07-08 17:40:06 +00:00
|
|
|
this.registerModule('ipfs');
|
|
|
|
this.registerModule('swarm');
|
2018-04-24 00:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
web3Service(options) {
|
2018-07-20 12:21:23 +00:00
|
|
|
this.registerModule('blockchain_process', {
|
|
|
|
locale: this.locale,
|
|
|
|
isDev: this.isDev
|
|
|
|
});
|
|
|
|
|
2018-06-02 13:54:32 +00:00
|
|
|
const Blockchain = require('../contracts/blockchain.js');
|
2018-07-27 20:03:15 +00:00
|
|
|
(new Blockchain({
|
2018-05-18 21:15:49 +00:00
|
|
|
contractsConfig: this.config.contractsConfig,
|
2018-05-20 13:08:03 +00:00
|
|
|
blockchainConfig: this.config.blockchainConfig,
|
2018-05-18 21:15:49 +00:00
|
|
|
events: this.events,
|
|
|
|
logger: this.logger,
|
2018-05-18 18:58:05 +00:00
|
|
|
isDev: this.isDev,
|
2018-05-23 15:52:07 +00:00
|
|
|
locale: this.locale,
|
2018-05-18 22:31:47 +00:00
|
|
|
web3: options.web3
|
2018-07-27 20:03:15 +00:00
|
|
|
})); /*eslint no-new: "off"*/
|
2018-04-10 19:14:00 +00:00
|
|
|
|
2018-07-27 19:47:41 +00:00
|
|
|
this.registerModule('whisper');
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
2017-12-30 20:52:51 +00:00
|
|
|
|
2017-12-30 22:07:13 +00:00
|
|
|
libraryManagerService(_options) {
|
2018-07-20 18:14:52 +00:00
|
|
|
this.registerModule('library_manager');
|
2017-12-30 20:52:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
2017-03-11 15:29:45 +00:00
|
|
|
|
2017-03-03 06:22:12 +00:00
|
|
|
module.exports = Engine;
|