diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 664f0d05..04e32a59 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -21,34 +21,87 @@ class Blockchain { this.isWeb3Ready = false; this.web3StartedInProcess = false; + this.registerProcessLaunch(); + if (!this.web3) { this.initWeb3(); } else { this.isWeb3Ready = true; } - this.registerServiceCheck(); + //this.registerServiceCheck(); this.registerRequests(); this.registerWeb3Object(); } - initWeb3(cb) { - if (!cb) { - cb = function(){}; - } - if (this.isWeb3Ready) { - return cb(); - } + registerProcessLaunch() { + const self = this; + this.events.request('processes:register', 'blockchain', (cb) => { + self.startBlockchainNode(cb); + }); + } + + initWeb3() { const self = this; this.web3 = new Web3(); if (this.contractsConfig.deployment.type !== "rpc" && this.contractsConfig.deployment.type !== "ws") { const message = __("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type); this.logger.error(message); - return cb(message); + //return cb(message); } const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws'; - + + this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`; + + const providerOptions = { + web3: this.web3, + accountsConfig: this.contractsConfig.deployment.accounts, + blockchainConfig: this.blockchainConfig, + logger: this.logger, + isDev: this.isDev, + type: this.contractsConfig.deployment.type, + web3Endpoint: self.web3Endpoint + }; + this.provider = new Provider(providerOptions); + + self.assertNodeConnection(true, (err) => { + if (!err) { + self.provider.startWeb3Provider(() => { + self.provider.fundAccounts(() => { + self.registerWeb3Object(); + + self.events.emit("check:backOnline:Ethereum"); + }); + }); + return; + } + + self.events.request("processes:launch", "blockchain", () => { + console.dir("======> blockchain launched!"); + self.provider.startWeb3Provider(() => { + self.provider.fundAccounts(() => { + self.registerWeb3Object(); + + self.events.emit("check:backOnline:Ethereum"); + }); + }); + }); + }); + } + + initWeb3_old() { + const self = this; + this.web3 = new Web3(); + + if (this.contractsConfig.deployment.type !== "rpc" && this.contractsConfig.deployment.type !== "ws") { + const message = __("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type); + this.logger.error(message); + //return cb(message); + } + + const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws'; + this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`; const providerOptions = { @@ -90,10 +143,11 @@ class Blockchain { return next(); } self.web3StartedInProcess = true; - self.startBlockchainNode(() => { + //self.startBlockchainNode(() => { + self.events.request("processes:launch", "blockchain", () => { // Need to re-initialize web3 to connect to the new blockchain node self.provider.stop(); - self.initWeb3(cb); + //self.initWeb3(cb); }); }); }, @@ -120,6 +174,7 @@ class Blockchain { } startBlockchainNode(callback) { + console.dir("==> startBlockchainNode"); const self = this; let blockchainProcess = new BlockchainProcessLauncher({ events: self.events, @@ -132,9 +187,11 @@ class Blockchain { blockchainProcess.startBlockchainNode(); self.events.once(constants.blockchain.blockchainReady, () => { + console.dir("==> blockchainReady"); callback(); }); self.events.once(constants.blockchain.blockchainExit, () => { + console.dir("==> blockchainExit"); self.provider.stop(); callback(); }); diff --git a/lib/modules/deployment/index.js b/lib/modules/deployment/index.js index 85b4167e..11946a73 100644 --- a/lib/modules/deployment/index.js +++ b/lib/modules/deployment/index.js @@ -1,6 +1,7 @@ let async = require('async'); const utils = require('../utils/utils.js'); +//require("../utils/debug_util.js")(__filename, async); const utils = require('../utils/utils.js'); //require("../utils/debug_util.js")(__filename, async); diff --git a/lib/processes/process_manager.js b/lib/processes/process_manager.js new file mode 100644 index 00000000..4da9ddea --- /dev/null +++ b/lib/processes/process_manager.js @@ -0,0 +1,39 @@ + +class ProcessManager { + constructor(options) { + const self = this; + this.logger = options.logger; + this.events = options.events; + this.plugins = options.plugins; + this.processes = {}; + + self.events.setCommandHandler('processes:register', (name, cb) => { + console.dir("=====> registering " + name); + this.processes[name] = { + state: 'unstarted', + cb: cb + } + }); + + self.events.setCommandHandler('processes:launch', (name, cb) => { + let process = self.processes[name]; + // TODO: should make distinction between starting and running + if (process.state != 'unstarted') { + console.dir("=====> already started " + name); + return cb(); + } + console.dir("=====> launching " + name); + process.state = 'starting'; + //let pry = require('pryjs'); + //eval(pry.it); + process.cb.apply(process.cb, [() => { + process.state = 'running'; + console.dir("=====> launched " + name); + cb(); + }]); + }); + } + +} + +module.exports = ProcessManager;