index.js
1 const async = require('async'); 2 const utils = require('../../utils/utils.js'); 3 const constants = require('../../constants'); 4 const BlockchainProcessLauncher = require('./blockchainProcessLauncher'); 5 6 class BlockchainModule { 7 8 constructor(embark, options) { 9 this.logger = embark.logger; 10 this.events = embark.events; 11 this.blockchainConfig = embark.config.blockchainConfig; 12 this.contractsConfig = embark.config.contractsConfig; 13 this.embark = embark; 14 this.locale = options.locale; 15 this.isDev = options.isDev; 16 this.ipc = options.ipc; 17 this.client = options.client; 18 19 this.registerBlockchainProcess(); 20 } 21 22 registerBlockchainProcess() { 23 const self = this; 24 this.events.request('processes:register', 'blockchain', (cb) => { 25 self.assertNodeConnection(true, (connected) => { 26 if (connected) return cb(); 27 self.startBlockchainNode(cb); 28 this.listenToCommands(); 29 this.registerConsoleCommands(); 30 }); 31 }); 32 33 if (!this.ipc.isServer()) return; 34 self.ipc.on('blockchain:node', (_message, cb) => { 35 cb(null, utils.buildUrlFromConfig(self.contractsConfig.deployment)); 36 }); 37 } 38 39 listenToCommands() { 40 this.events.setCommandHandler('logs:ethereum:turnOn', (cb) => { 41 this.events.emit('logs:ethereum:enable'); 42 return cb(null, 'Enabling Geth logs'); 43 }); 44 45 this.events.setCommandHandler('logs:ethereum:turnOff', (cb) => { 46 this.events.emit('logs:ethereum:disable'); 47 return cb(null, 'Disabling Geth logs'); 48 }); 49 } 50 51 registerConsoleCommands() { 52 const self = this; 53 self.embark.registerConsoleCommand((cmd, _options) => { 54 return { 55 match: () => cmd === 'log geth on', 56 process: (cb) => self.events.request('logs:ethereum:turnOn', cb) 57 }; 58 }); 59 60 self.embark.registerConsoleCommand((cmd, _options) => { 61 return { 62 match: () => cmd === 'log geth off', 63 process: (cb) => self.events.request('logs:ethereum:turnOff', cb) 64 }; 65 }); 66 } 67 68 assertNodeConnection(noLogs, cb) { 69 if (typeof noLogs === 'function') { 70 cb = noLogs; 71 noLogs = false; 72 } 73 const self = this; 74 75 async.waterfall([ 76 function checkWeb3State(next) { 77 self.events.request("blockchain:web3:isReady", (connected) => { 78 if (connected) { 79 return next(connected); 80 } 81 next(); 82 }); 83 }, 84 function pingEndpoint(next) { 85 if (!self.contractsConfig || !self.contractsConfig.deployment || !self.contractsConfig.deployment.host) { 86 return next(); 87 } 88 const {host, port, type, protocol} = self.contractsConfig.deployment; 89 utils.pingEndpoint(host, port, type, protocol, self.blockchainConfig.wsOrigins.split(',')[0], next); 90 } 91 ], function(err) { 92 if (err === true || err === undefined) { 93 return cb(true); 94 } 95 return cb(false); 96 }); 97 } 98 99 startBlockchainNode(callback) { 100 const self = this; 101 102 let blockchainProcess = new BlockchainProcessLauncher({ 103 events: self.events, 104 logger: self.logger, 105 normalizeInput: utils.normalizeInput, 106 blockchainConfig: self.blockchainConfig, 107 locale: self.locale, 108 client: self.client, 109 isDev: self.isDev, 110 embark: this.embark 111 }); 112 113 blockchainProcess.startBlockchainNode(); 114 self.events.once(constants.blockchain.blockchainReady, () => { 115 callback(); 116 }); 117 self.events.once(constants.blockchain.blockchainExit, () => { 118 callback(); 119 }); 120 } 121 122 } 123 124 module.exports = BlockchainModule;