move web3 init to its own module

This commit is contained in:
Iuri Matias 2018-05-18 17:15:49 -04:00 committed by Jonathan Rainville
parent 28ef2ed067
commit 4d0ebc03ad
2 changed files with 68 additions and 66 deletions

View File

@ -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;

View File

@ -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
});
}