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)
This commit is contained in:
Jonathan Rainville 2020-02-14 16:44:38 -05:00 committed by Iuri Matias
parent b4286bf59a
commit 5531b60f10
3 changed files with 46 additions and 10 deletions

View File

@ -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": {

View File

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

View File

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