From 75d9998c578e67e831372d872d6041d393f78042 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 12 Oct 2018 11:23:12 -0400 Subject: [PATCH] refactor to not use the blockchain_connector directly --- cmd/cmd_controller.js | 2 +- lib/modules/blockchain_connector/index.js | 26 +++++++------ lib/modules/tests/test.js | 46 +++++++++++------------ 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index ee9921ff..b1fefeb0 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -554,7 +554,7 @@ class EmbarkController { function startServices(callback) { engine.startService("processManager"); engine.startService("libraryManager"); - engine.startService("web3", {wait: true}); // Empty web3 as Test changes it + engine.startService("web3", {wait: true}); engine.startService("deployment"); engine.startService("codeGenerator"); engine.startService("codeRunner"); diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 78edcb5e..ab494d5a 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -14,8 +14,7 @@ class BlockchainConnector { this.plugins = options.plugins; this.logger = embark.logger; this.events = embark.events; - this.contractsConfig = embark.config.contractsConfig; - this.blockchainConfig = embark.config.blockchainConfig; + this.config = embark.config; this.web3 = options.web3; this.isDev = options.isDev; this.web3Endpoint = ''; @@ -55,12 +54,12 @@ class BlockchainConnector { const self = this; this.web3 = new Web3(); - // TODO find a better way if (self.wait) { + self.wait = false; return cb(); } - let {type, host, port, accounts, protocol} = this.contractsConfig.deployment; + let {type, host, port, accounts, protocol, coverage} = this.config.contractsConfig.deployment; if (!BlockchainConnector.ACCEPTED_TYPES.includes(type)) { this.logger.error(__("contracts config error: unknown deployment type %s", type)); @@ -69,9 +68,9 @@ class BlockchainConnector { if (type === 'vm') { const sim = self._getSimulator(); - self.provider = sim.provider(self.contractsConfig.deployment); + self.provider = sim.provider(self.config.contractsConfig.deployment); - if (self.coverage) { + if (coverage) { // Here we patch the sendAsync method on the provider. The goal behind this is to force pure/constant/view calls to become // transactions, so that we can pull in execution traces and account for those executions in code coverage. // @@ -118,7 +117,7 @@ class BlockchainConnector { const providerOptions = { web3: this.web3, accountsConfig: accounts, - blockchainConfig: this.blockchainConfig, + blockchainConfig: this.config.blockchainConfig, logger: this.logger, isDev: this.isDev, type: type, @@ -130,9 +129,9 @@ class BlockchainConnector { self.provider.startWeb3Provider(() => { this.getNetworkId() .then(id => { - let networkId = self.blockchainConfig.networkId; - if (!networkId && constants.blockchain.networkIds[self.blockchainConfig.networkType]) { - networkId = constants.blockchain.networkIds[self.blockchainConfig.networkType]; + let networkId = self.config.blockchainConfig.networkId; + if (!networkId && constants.blockchain.networkIds[self.config.blockchainConfig.networkType]) { + networkId = constants.blockchain.networkIds[self.config.blockchainConfig.networkType]; } if (networkId && id.toString() !== networkId.toString()) { self.logger.warn(__('Connected to a blockchain node on network {{realId}} while your config specifies {{configId}}', {realId: id, configId: networkId})); @@ -244,6 +243,11 @@ class BlockchainConnector { registerRequests() { const self = this; + this.events.setCommandHandler("blockchain:reset", function(cb) { + self.isWeb3Ready = false; + self.initWeb3(cb); + }); + this.events.setCommandHandler("blockchain:get", function(cb) { cb(self.web3); }); @@ -344,7 +348,7 @@ class BlockchainConnector { self.logger.error(err); return cb(new Error(err)); } - let accountConfig = self.blockchainConfig.account; + let accountConfig = self.config.blockchainConfig.account; let selectedAccount = accountConfig && accountConfig.address; self.setDefaultAccount(selectedAccount || accounts[0]); cb(); diff --git a/lib/modules/tests/test.js b/lib/modules/tests/test.js index a02578ae..bad9b9b9 100644 --- a/lib/modules/tests/test.js +++ b/lib/modules/tests/test.js @@ -17,27 +17,22 @@ class Test { this.contracts = {}; this.firstDeployment = true; this.needConfig = true; - this.blockchainConnector = null; this.provider = null; this.accounts = []; } init(callback) { - this.gasLimit = 6000000; this.events.request('deploy:setGasLimit', this.gasLimit); - this.events.request('blockchain:object', (connector) => { - this.blockchainConnector = connector; - if (this.options.node !== 'embark') { - this.showNodeHttpWarning(); - return callback(); - } - if (!this.ipc.connected) { - this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?"); - process.exit(1); - } - return this.connectToIpcNode(callback); - }); + if (this.options.node !== 'embark') { + this.showNodeHttpWarning(); + return callback(); + } + if (!this.ipc.connected) { + this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?"); + process.exit(1); + } + return this.connectToIpcNode(callback); } connectToIpcNode(cb) { @@ -62,7 +57,6 @@ class Test { }); } - // TODO use event for this if (!this.simOptions.host && (this.options.node && this.options.node === 'vm')) { this.simOptions.type = 'vm'; } else if (this.simOptions.host || (this.options.node && this.options.node !== 'vm')) { @@ -78,18 +72,22 @@ class Test { } this.configObj.contractsConfig.deployment = this.simOptions; - this.blockchainConnector.contractsConfig = this.configObj.contractsConfig; - this.blockchainConnector.isWeb3Ready = false; - this.blockchainConnector.wait = false; - this.blockchainConnector.coverage = this.options.coverage; + this.configObj.contractsConfig.deployment.coverage = this.options.coverage; + this.events.request("config:contractsConfig:set", this.configObj.contractsConfig, () => { + this.events.request('blockchain:reset', (err) => { + if (err) { + this.logger.error('Error restarting the blockchain connection'); + } + callback(err); + }); + }); - this.blockchainConnector.initWeb3(callback); } showNodeHttpWarning() { if (this.options.node.startsWith('http')) { this.logger.warn("You are using http to connect to the node, as a result the gas details won't be correct." + - " For correct gas details reporting, please use a websockets connection to your node."); + " For correct gas details reporting, please use a websockets connection to your node."); } } @@ -127,7 +125,7 @@ class Test { return callback(__("contracts config error: unknown deployment type %s", type)); } - if(accounts || port !== this.simOptions.port || type !== this.simOptions.type || host !== this.simOptions.host) { + if (accounts || port !== this.simOptions.port || type !== this.simOptions.type || host !== this.simOptions.host) { resetServices = true; } @@ -166,7 +164,7 @@ class Test { options = {}; } if (!callback) { - callback = function () { + callback = function() { }; } if (!options.contracts) { @@ -276,7 +274,7 @@ class Test { }); } - ], function (err, accounts) { + ], function(err, accounts) { if (err) { self.logger.error(__('terminating due to error')); self.logger.error(err.message || err);