From 5531b60f105d059514819df2b25ba564ee3d4939 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 14 Feb 2020 16:44:38 -0500 Subject: [PATCH] fix(@embark/ganache): fix connection to other nodes from Ganache Using tests with a custom --node didn't work, because Ganache always used it's own provider. Now, it actually checks before if there is not another node started before using its own provider (+1 squashed commits) --- packages/plugins/ganache/package.json | 1 + packages/plugins/ganache/src/index.js | 33 +++++++++++++++++++++++--- packages/stack/blockchain/src/index.js | 22 +++++++++++------ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/packages/plugins/ganache/package.json b/packages/plugins/ganache/package.json index ab5f9a230..a182cf1a4 100644 --- a/packages/plugins/ganache/package.json +++ b/packages/plugins/ganache/package.json @@ -48,6 +48,7 @@ "core-js": "3.4.3", "embark-core": "^5.2.0-nightly.4", "embark-i18n": "^5.1.1", + "embark-utils": "^5.2.0-nightly.3", "ganache-cli": "6.8.2" }, "devDependencies": { diff --git a/packages/plugins/ganache/src/index.js b/packages/plugins/ganache/src/index.js index 390baf348..092089169 100644 --- a/packages/plugins/ganache/src/index.js +++ b/packages/plugins/ganache/src/index.js @@ -1,4 +1,5 @@ import {__} from 'embark-i18n'; +const {testRpcWithEndpoint, testWsEndpoint} = require('embark-utils'); const constants = require('embark-core/constants'); class Ganache { @@ -8,7 +9,12 @@ class Ganache { this.embark.events.request("blockchain:node:register", constants.blockchain.clients.ganache, { isStartedFn: (cb) => { - cb(null, !!this.currentProvider); // Always assume it's started, because it's just a provider (nothing to start) + if (this.currentProvider) { + return cb(null, true); + } + this._doCheck((err, started) => { + cb(err,started); + }); }, launchFn: (cb) => { this._getProvider(); // No need to return anything, we just want to populate currentProvider @@ -19,8 +25,18 @@ class Ganache { this.currentProvider = null; cb(); }, - provider: async (_endpoint) => { - return this._getProvider(); + provider: (_endpoint) => { + if (this.currentProvider) { + return this.currentProvider; + } + return new Promise(resolve => { + this._doCheck(async (_err, started) => { + if (_err || !started) { + return resolve(this._getProvider()); + } + resolve(await this.embark.events.request2('blockchain:node:provider:template')); + }); + }); } }); @@ -77,6 +93,17 @@ class Ganache { }); }); } + + _doCheck(cb) { + const endpoint = this.embark.config.blockchainConfig.endpoint; + if (!endpoint) { + return cb(null, false); + } + if (endpoint.startsWith('ws')) { + return testWsEndpoint(endpoint, (err) => cb(null, !err)); + } + testRpcWithEndpoint(endpoint, (err) => cb(null, !err)); + } } module.exports = Ganache; diff --git a/packages/stack/blockchain/src/index.js b/packages/stack/blockchain/src/index.js index b3e11fb95..a0411f573 100644 --- a/packages/stack/blockchain/src/index.js +++ b/packages/stack/blockchain/src/index.js @@ -45,13 +45,7 @@ export default class Blockchain { if (!provider) { // Set default provider function clientFunctions.provider = async () => { - if (this.blockchainConfig.endpoint.startsWith('ws')) { - return new Web3.providers.WebsocketProvider(this.blockchainConfig.endpoint, { - headers: { Origin: constants.embarkResourceOrigin } - }); - } - const web3 = new Web3(this.blockchainConfig.endpoint); - return web3.currentProvider; + return this.getProviderFromTemplate(this.blockchainConfig.endpoint); }; } @@ -137,6 +131,10 @@ export default class Blockchain { } }); + this.events.setCommandHandler('blockchain:node:provider:template', (cb) => { + cb(null, this.getProviderFromTemplate(this.blockchainConfig.endpoint)); + }); + this.events.setCommandHandler("blockchain:client:register", (clientName, getProviderFunction) => { this.blockchainClients[clientName] = getProviderFunction; }); @@ -164,6 +162,16 @@ export default class Blockchain { } } + getProviderFromTemplate(endpoint) { + if (endpoint.startsWith('ws')) { + return new Web3.providers.WebsocketProvider(endpoint, { + headers: { Origin: constants.embarkResourceOrigin } + }); + } + const web3 = new Web3(endpoint); + return web3.currentProvider; + } + addArtifactFile(_params, cb) { if (!this.blockchainConfig.enabled) { cb();