From 4d0ebc03ad0ae5eafc2cac1132e9b2abdb973165 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 18 May 2018 17:15:49 -0400 Subject: [PATCH] move web3 init to its own module --- lib/contracts/blockchain.js | 53 +++++++++++++++++++++++- lib/core/engine.js | 81 ++++++++----------------------------- 2 files changed, 68 insertions(+), 66 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 46fc2793..c7fd3867 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -1,9 +1,60 @@ +const Web3 = require('web3'); class Blockchain { - constructor(optiosn) { + constructor(options) { this.plugins = options.plugins; this.logger = options.logger; + this.config = options.config; + this.contractsConfig = this.config.contractsConfig; + this.web3 = options.web3; + + if (!this.web3) { + this.initWeb3(); + } + this.registerServiceCheck(); } + + initWeb3() { + this.web3 = new Web3(); + if (this.contractsConfig.deployment.type === "rpc") { + let web3Endpoint = 'http://' + this.contractsConfig.deployment.host + ':' + this.contractsConfig.deployment.port; + this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint)); + } else { + throw new Error("contracts config error: unknown deployment type " + this.contractsConfig.deployment.type); + } + } + + registerServiceCheck() { + const self = this; + + this.addCheck('Ethereum', function (cb) { + if (self.web3.currentProvider === undefined) { + return cb({name: "No Blockchain node found", status: 'off'}); + } + + self.web3.eth.getAccounts(function(err, _accounts) { + if (err) { + return cb({name: "No Blockchain node found", status: 'off'}); + } + + // TODO: web3_clientVersion method is currently not implemented in web3.js 1.0 + self.web3._requestManager.send({method: 'web3_clientVersion', params: []}, (err, version) => { + if (err) { + return cb({name: "Ethereum node (version unknown)", status: 'on'}); + } + if (version.indexOf("/") < 0) { + return cb({name: version, status: 'on'}); + } + let nodeName = version.split("/")[0]; + let versionNumber = version.split("/")[1].split("-")[0]; + let name = nodeName + " " + versionNumber + " (Ethereum)"; + + return cb({name: name, status: 'on'}); + }); + }); + }); + } + } module.exports = Blockchain; diff --git a/lib/core/engine.js b/lib/core/engine.js index b41736e8..f54e9f11 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,16 +1,16 @@ -const Web3 = require('web3'); +const async = require('async'); + const Events = require('./events.js'); const Logger = require('./logger.js'); const Config = require('./config.js'); +const Blockchain = require('../contracts/blockchain.js'); const ContractsManager = require('../contracts/contracts.js'); const DeployManager = require('../contracts/deploy_manager.js'); const CodeGenerator = require('../contracts/code_generator.js'); const ServicesMonitor = require('./services_monitor.js'); +const Pipeline = require('../pipeline/pipeline.js'); const Watch = require('../pipeline/watch.js'); const LibraryManager = require('../versions/library_manager.js'); -const Pipeline = require('../pipeline/pipeline.js'); -const async = require('async'); -const Provider = require('../contracts/provider'); class Engine { constructor(options) { @@ -273,69 +273,20 @@ class Engine { } web3Service(options) { - let self = this; - this.web3 = options.web3; - let provider; - if (this.web3 === undefined) { - this.web3 = new Web3(); - if (this.config.contractsConfig.deployment.type === "rpc") { - const web3Endpoint = 'http://' + this.config.contractsConfig.deployment.host + ':' + this.config.contractsConfig.deployment.port; - const providerOptions = { - web3: this.web3, - accountsConfig: this.config.contractsConfig.deployment.accounts, - logger: this.logger, - isDev: this.isDev, - web3Endpoint - }; - provider = new Provider(providerOptions); - } else { - throw new Error("contracts config error: unknown deployment type " + this.config.contractsConfig.deployment.type); - } - } + this.blockchain = new Blockchain({ + contractsConfig: this.config.contractsConfig, + addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + events: this.events, + logger: this.logger, + options: options.web3 // TODO: might not be necessary + }); - async.waterfall([ - function (next) { - if (!provider) { - return next(); - } - provider.startWeb3Provider(next); - } - ], function (err) { - if (err) { - console.error(err); - } - self.servicesMonitor.addCheck('Ethereum', function (cb) { - if (self.web3.currentProvider === undefined) { - return cb({name: __("No Blockchain node found"), status: 'off'}); - } + this.web3 = this.blockchain.web3; - self.web3.eth.getAccounts(function(err, _accounts) { - if (err) { - return cb({name: __("No Blockchain node found"), status: 'off'}); - } - - // TODO: web3_clientVersion method is currently not implemented in web3.js 1.0 - self.web3._requestManager.send({method: 'web3_clientVersion', params: []}, (err, version) => { - if (err) { - return cb({name: __("Ethereum node (version unknown)"), status: 'on'}); - } - if (version.indexOf("/") < 0) { - return cb({name: version, status: 'on'}); - } - let nodeName = version.split("/")[0]; - let versionNumber = version.split("/")[1].split("-")[0]; - let name = nodeName + " " + versionNumber + " (Ethereum)"; - - return cb({name: name, status: 'on'}); - }); - }); - }); - - self.registerModule('whisper', { - addCheck: self.servicesMonitor.addCheck.bind(self.servicesMonitor), - communicationConfig: self.config.communicationConfig, - web3: self.web3 - }); + this.registerModule('whisper', { + addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + communicationConfig: this.config.communicationConfig, + web3: this.web3 }); }