2017-03-29 17:50:05 +00:00
|
|
|
let Web3 = require('web3');
|
|
|
|
let utils = require('../utils/utils.js');
|
|
|
|
let Events = require('./events.js');
|
|
|
|
let Logger = require('./logger.js');
|
|
|
|
let Config = require('./config.js');
|
|
|
|
let DeployManager = require('../contracts/deploy_manager.js');
|
|
|
|
let ABIGenerator = require('../contracts/abi.js');
|
|
|
|
let ServicesMonitor = require('./services_monitor.js');
|
|
|
|
let Pipeline = require('../pipeline/pipeline.js');
|
2017-03-30 11:38:14 +00:00
|
|
|
let Server = require('../pipeline/server.js');
|
2017-03-29 17:50:05 +00:00
|
|
|
let Watch = require('../pipeline/watch.js');
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
class Engine {
|
|
|
|
constructor(options) {
|
|
|
|
this.env = options.env;
|
|
|
|
this.embarkConfig = options.embarkConfig;
|
|
|
|
this.interceptLogs = options.interceptLogs;
|
2017-03-31 11:34:43 +00:00
|
|
|
this.version = options.version;
|
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) {
|
|
|
|
let self = this;
|
|
|
|
let options = _options || {};
|
|
|
|
this.events = new Events();
|
|
|
|
this.logger = options.logger || new Logger({logLevel: 'debug'});
|
|
|
|
this.config = new Config({env: this.env, logger: this.logger, events: this.events});
|
|
|
|
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
|
|
|
|
this.plugins = this.config.plugins;
|
|
|
|
|
|
|
|
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger});
|
|
|
|
this.servicesMonitor.addCheck('embarkVersion', function (cb) {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: 'Embark ' + self.version, status: 'on'});
|
2017-03-30 11:38:14 +00:00
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
startMonitor() {
|
|
|
|
let self = this;
|
|
|
|
if (this.plugins) {
|
|
|
|
let servicePlugins = this.plugins.getPluginsFor('serviceChecks');
|
|
|
|
servicePlugins.forEach(function (plugin) {
|
|
|
|
plugin.serviceChecks.forEach(function (pluginCheck) {
|
|
|
|
self.servicesMonitor.addCheck(pluginCheck.checkName, pluginCheck.checkFn, pluginCheck.time);
|
|
|
|
});
|
2017-03-16 11:31:52 +00:00
|
|
|
});
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
|
|
|
this.servicesMonitor.startMonitor();
|
|
|
|
}
|
|
|
|
|
|
|
|
startService(serviceName, _options) {
|
|
|
|
let options = _options || {};
|
|
|
|
|
|
|
|
let services = {
|
|
|
|
"pipeline": this.pipelineService,
|
|
|
|
"abi": this.abiService,
|
|
|
|
"deployment": this.deploymentService,
|
|
|
|
"fileWatcher": this.fileWatchService,
|
|
|
|
"webServer": this.webServerService,
|
|
|
|
"ipfs": this.ipfsService,
|
|
|
|
"web3": this.web3Service
|
|
|
|
};
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelineService(options) {
|
|
|
|
let self = this;
|
|
|
|
this.logger.setStatus("Building Assets");
|
|
|
|
let pipeline = new Pipeline({
|
|
|
|
buildDir: this.config.buildDir,
|
|
|
|
contractsFiles: this.config.contractsFiles,
|
|
|
|
assetFiles: this.config.assetFiles,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins
|
2017-03-16 11:31:52 +00:00
|
|
|
});
|
2017-04-04 10:37:50 +00:00
|
|
|
this.events.on('abi', function (abi, contractsJSON) {
|
2017-03-30 11:38:14 +00:00
|
|
|
self.currentAbi = abi;
|
2017-04-04 10:37:50 +00:00
|
|
|
self.contractsJSON = contractsJSON;
|
|
|
|
pipeline.build(abi, contractsJSON);
|
2017-03-30 11:38:14 +00:00
|
|
|
self.events.emit('outputDone');
|
|
|
|
});
|
|
|
|
// TODO: still need to redeploy contracts because the original contracts
|
|
|
|
// config is being corrupted
|
|
|
|
//this.events.on('file-event', function(fileType, path) {
|
|
|
|
// if (fileType === 'asset') {
|
|
|
|
// self.config.reloadConfig();
|
2017-04-04 10:37:50 +00:00
|
|
|
// pipeline.build(self.abi, self.contractsJSON, path);
|
2017-03-30 11:38:14 +00:00
|
|
|
// self.events.emit('outputDone');
|
|
|
|
// }
|
|
|
|
//});
|
2017-03-16 11:31:52 +00:00
|
|
|
}
|
2017-03-30 11:38:14 +00:00
|
|
|
|
|
|
|
abiService(options) {
|
|
|
|
let self = this;
|
|
|
|
let generateABICode = function (contractsManager) {
|
|
|
|
let abiGenerator = new ABIGenerator({
|
|
|
|
blockchainConfig: self.config.blockchainConfig,
|
|
|
|
contractsManager: contractsManager,
|
|
|
|
plugins: self.plugins,
|
|
|
|
storageConfig: self.config.storageConfig,
|
|
|
|
communicationConfig: self.config.communicationConfig
|
|
|
|
});
|
|
|
|
let embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
|
|
|
|
let vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
|
|
|
|
let vanillaContractsABI = abiGenerator.generateContracts(false);
|
2017-04-04 10:37:50 +00:00
|
|
|
let contractsJSON = abiGenerator.generateContractsJSON();
|
2017-03-30 11:38:14 +00:00
|
|
|
|
2017-04-04 10:37:50 +00:00
|
|
|
self.events.emit('abi-contracts-vanila', vanillaContractsABI, contractsJSON);
|
|
|
|
self.events.emit('abi-vanila', vanillaABI, contractsJSON);
|
|
|
|
self.events.emit('abi', embarkJSABI, contractsJSON);
|
2017-03-30 11:38:14 +00:00
|
|
|
};
|
|
|
|
this.events.on('contractsDeployed', generateABICode);
|
|
|
|
this.events.on('blockchainDisabled', generateABICode);
|
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;
|
|
|
|
this.deployManager = new DeployManager({
|
|
|
|
web3: options.web3 || self.web3,
|
|
|
|
trackContracts: options.trackContracts,
|
|
|
|
config: this.config,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins,
|
|
|
|
events: this.events
|
2017-03-04 02:48:32 +00:00
|
|
|
});
|
2017-03-30 11:38:14 +00:00
|
|
|
|
|
|
|
this.events.on('file-event', function (fileType, path) {
|
|
|
|
// TODO: for now need to deploy on asset chanes as well
|
|
|
|
// because the contractsManager config is corrupted after a deploy
|
|
|
|
//if (fileType === 'contract' || fileType === 'config') {
|
|
|
|
self.config.reloadConfig();
|
|
|
|
self.deployManager.deployContracts(function () {
|
|
|
|
});
|
|
|
|
//}
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-11 03:00:30 +00:00
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
fileWatchService(options) {
|
|
|
|
this.logger.setStatus("Watching for changes");
|
|
|
|
let watch = new Watch({logger: this.logger, events: this.events});
|
|
|
|
watch.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
webServerService(options) {
|
|
|
|
let self = this;
|
|
|
|
let webServerConfig = this.config.webServerConfig;
|
|
|
|
if (!webServerConfig.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let host = options.host || webServerConfig.host;
|
|
|
|
let port = options.port || webServerConfig.port;
|
|
|
|
|
|
|
|
this.logger.setStatus("Starting Server");
|
|
|
|
let server = new Server({
|
|
|
|
logger: this.logger,
|
|
|
|
host: host,
|
|
|
|
port: port
|
|
|
|
});
|
|
|
|
|
|
|
|
self.servicesMonitor.addCheck('Webserver', function (cb) {
|
|
|
|
let devServer = 'Webserver (http://' + host + ':' + port + ')';
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: devServer, status: 'on'});
|
2017-03-30 11:38:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
server.start(function () {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ipfsService(options) {
|
|
|
|
let self = this;
|
|
|
|
self.servicesMonitor.addCheck('IPFS', function (cb) {
|
|
|
|
utils.checkIsAvailable('http://localhost:5001', function (available) {
|
|
|
|
if (available) {
|
|
|
|
//Ideally this method should be in an IPFS API JSONRPC wrapper
|
|
|
|
//The URL should also be flexible to accept non-default IPFS url
|
|
|
|
self.logger.trace("Checking IPFS version...");
|
2017-04-02 18:40:10 +00:00
|
|
|
utils.httpGet('http://localhost:5001/api/v0/version', function (res) {
|
2017-03-30 11:38:14 +00:00
|
|
|
let body = '';
|
|
|
|
res.on('data', function (d) {
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', function () {
|
|
|
|
try {
|
|
|
|
let parsed = JSON.parse(body);
|
|
|
|
if (parsed.Version) {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: ("IPFS " + parsed.Version), status: 'on'});
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: "IPFS ", status: 'on'});
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
2017-03-11 03:00:30 +00:00
|
|
|
}
|
2017-03-30 11:38:14 +00:00
|
|
|
catch (e) {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: "IPFS ", status: 'off'});
|
2017-03-11 03:00:30 +00:00
|
|
|
}
|
2017-03-30 11:38:14 +00:00
|
|
|
});
|
|
|
|
res.on('error', function (err) {
|
|
|
|
self.logger.trace("Check IPFS version error: " + err);
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: "IPFS ", status: 'off'});
|
2017-03-30 11:38:14 +00:00
|
|
|
});
|
2017-03-11 03:00:30 +00:00
|
|
|
});
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: "IPFS ", status: 'off'});
|
2017-03-30 11:38:14 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-11 03:00:30 +00:00
|
|
|
});
|
2017-03-11 15:29:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
web3Service(options) {
|
|
|
|
let self = this;
|
|
|
|
this.web3 = options.web3;
|
|
|
|
if (this.web3 === undefined) {
|
|
|
|
this.web3 = new Web3();
|
|
|
|
let web3Endpoint = 'http://' + this.config.blockchainConfig.rpcHost + ':' + this.config.blockchainConfig.rpcPort;
|
|
|
|
this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint));
|
2017-03-11 15:29:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
self.servicesMonitor.addCheck('Ethereum', function (cb) {
|
|
|
|
if (self.web3.isConnected()) {
|
|
|
|
return cb({
|
|
|
|
name: (self.web3.version.node.split("/")[0] + " " + self.web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)"),
|
2017-04-02 03:22:43 +00:00
|
|
|
status: 'on'
|
2017-03-30 11:38:14 +00:00
|
|
|
});
|
2017-03-11 15:29:45 +00:00
|
|
|
} else {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: "No Blockchain node found", status: 'off'});
|
2017-03-11 15:29:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-30 11:38:14 +00:00
|
|
|
self.servicesMonitor.addCheck('Whisper', function (cb) {
|
|
|
|
self.web3.version.getWhisper(function (err, res) {
|
|
|
|
if (err) {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: 'Whisper', status: 'off'});
|
2017-03-30 11:38:14 +00:00
|
|
|
} else {
|
2017-04-02 03:22:43 +00:00
|
|
|
return cb({name: 'Whisper', status: 'on'});
|
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;
|