Skip to content

Commit 5531b60

Browse files
jrainvilleiurimatias
authored andcommitted
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)
1 parent b4286bf commit 5531b60

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

packages/plugins/ganache/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"core-js": "3.4.3",
4949
"embark-core": "^5.2.0-nightly.4",
5050
"embark-i18n": "^5.1.1",
51+
"embark-utils": "^5.2.0-nightly.3",
5152
"ganache-cli": "6.8.2"
5253
},
5354
"devDependencies": {

packages/plugins/ganache/src/index.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {__} from 'embark-i18n';
2+
const {testRpcWithEndpoint, testWsEndpoint} = require('embark-utils');
23
const constants = require('embark-core/constants');
34

45
class Ganache {
@@ -8,7 +9,12 @@ class Ganache {
89

910
this.embark.events.request("blockchain:node:register", constants.blockchain.clients.ganache, {
1011
isStartedFn: (cb) => {
11-
cb(null, !!this.currentProvider); // Always assume it's started, because it's just a provider (nothing to start)
12+
if (this.currentProvider) {
13+
return cb(null, true);
14+
}
15+
this._doCheck((err, started) => {
16+
cb(err,started);
17+
});
1218
},
1319
launchFn: (cb) => {
1420
this._getProvider(); // No need to return anything, we just want to populate currentProvider
@@ -19,8 +25,18 @@ class Ganache {
1925
this.currentProvider = null;
2026
cb();
2127
},
22-
provider: async (_endpoint) => {
23-
return this._getProvider();
28+
provider: (_endpoint) => {
29+
if (this.currentProvider) {
30+
return this.currentProvider;
31+
}
32+
return new Promise(resolve => {
33+
this._doCheck(async (_err, started) => {
34+
if (_err || !started) {
35+
return resolve(this._getProvider());
36+
}
37+
resolve(await this.embark.events.request2('blockchain:node:provider:template'));
38+
});
39+
});
2440
}
2541
});
2642

@@ -77,6 +93,17 @@ class Ganache {
7793
});
7894
});
7995
}
96+
97+
_doCheck(cb) {
98+
const endpoint = this.embark.config.blockchainConfig.endpoint;
99+
if (!endpoint) {
100+
return cb(null, false);
101+
}
102+
if (endpoint.startsWith('ws')) {
103+
return testWsEndpoint(endpoint, (err) => cb(null, !err));
104+
}
105+
testRpcWithEndpoint(endpoint, (err) => cb(null, !err));
106+
}
80107
}
81108

82109
module.exports = Ganache;

packages/stack/blockchain/src/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@ export default class Blockchain {
4545
if (!provider) {
4646
// Set default provider function
4747
clientFunctions.provider = async () => {
48-
if (this.blockchainConfig.endpoint.startsWith('ws')) {
49-
return new Web3.providers.WebsocketProvider(this.blockchainConfig.endpoint, {
50-
headers: { Origin: constants.embarkResourceOrigin }
51-
});
52-
}
53-
const web3 = new Web3(this.blockchainConfig.endpoint);
54-
return web3.currentProvider;
48+
return this.getProviderFromTemplate(this.blockchainConfig.endpoint);
5549
};
5650
}
5751

@@ -137,6 +131,10 @@ export default class Blockchain {
137131
}
138132
});
139133

134+
this.events.setCommandHandler('blockchain:node:provider:template', (cb) => {
135+
cb(null, this.getProviderFromTemplate(this.blockchainConfig.endpoint));
136+
});
137+
140138
this.events.setCommandHandler("blockchain:client:register", (clientName, getProviderFunction) => {
141139
this.blockchainClients[clientName] = getProviderFunction;
142140
});
@@ -164,6 +162,16 @@ export default class Blockchain {
164162
}
165163
}
166164

165+
getProviderFromTemplate(endpoint) {
166+
if (endpoint.startsWith('ws')) {
167+
return new Web3.providers.WebsocketProvider(endpoint, {
168+
headers: { Origin: constants.embarkResourceOrigin }
169+
});
170+
}
171+
const web3 = new Web3(endpoint);
172+
return web3.currentProvider;
173+
}
174+
167175
addArtifactFile(_params, cb) {
168176
if (!this.blockchainConfig.enabled) {
169177
cb();

0 commit comments

Comments
 (0)