Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit a34c969

Browse files
Merge pull request #5329 from trufflesuite/fix-console-for-testnet
Fix console for test and migrate to work with public testnets and dashboard
2 parents cca7f78 + 02bb578 commit a34c969

File tree

4 files changed

+240
-63
lines changed

4 files changed

+240
-63
lines changed

packages/core/lib/command-utils.js

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ const globalCommandOptions = require("./global-command-options");
66
const debugModule = require("debug");
77
const debug = debugModule("core:command:run");
88
const commands = require("./commands/commands");
9+
const Web3 = require("web3");
10+
11+
const defaultHost = "127.0.0.1";
12+
const managedGanacheDefaultPort = 9545;
13+
const managedGanacheDefaultNetworkId = 5777;
14+
const managedDashboardDefaultPort = 24012;
915

1016
// this function takes an object with an array of input strings, an options
1117
// object, and a boolean determining whether we allow inexact matches for
@@ -206,9 +212,92 @@ const displayGeneralHelp = () => {
206212
.showHelp();
207213
};
208214

215+
/**
216+
* This is a function to configure the url from the user specified network settings in the config.
217+
* @param {TruffleConfig} customConfig - Default config with user specified settings.
218+
* @param {boolean} isDashboardNetwork - Check if the network is dashboard or not.
219+
* @returns a string with the configured url
220+
*/
221+
const getConfiguredNetworkUrl = function (customConfig, isDashboardNetwork) {
222+
const defaultPort = isDashboardNetwork
223+
? managedDashboardDefaultPort
224+
: managedGanacheDefaultPort;
225+
const configuredNetworkOptions = {
226+
host: customConfig.host || defaultHost,
227+
port: customConfig.port || defaultPort
228+
};
229+
const urlSuffix = isDashboardNetwork ? "/rpc" : "";
230+
return `http://${configuredNetworkOptions.host}:${configuredNetworkOptions.port}${urlSuffix}`;
231+
};
232+
233+
/**
234+
* This is a function to derive the config environment from the user specified settings.
235+
* @param {TruffleConfig} detectedConfig - Default config with user specified settings.
236+
* @param {string} network - Network name specified with the `--network` option.
237+
* @param {string} url - URL specified with the `--url` option.
238+
* @returns a TruffleConfig object with the user specified settings in the config
239+
*/
240+
const deriveConfigEnvironment = function (detectedConfig, network, url) {
241+
let configuredNetwork;
242+
243+
const configDefinesProvider =
244+
detectedConfig.networks[network] &&
245+
detectedConfig.networks[network].provider;
246+
247+
if (configDefinesProvider) {
248+
// Use "provider" specified in the config to connect to the network
249+
// along with the other network properties
250+
configuredNetwork = {
251+
network_id: "*",
252+
...detectedConfig.networks[network]
253+
};
254+
} else if (url) {
255+
// Use "url" to configure network (implies not "develop" and not "dashboard")
256+
configuredNetwork = {
257+
network_id: "*",
258+
url,
259+
provider: function () {
260+
return new Web3.providers.HttpProvider(url, {
261+
keepAlive: false
262+
});
263+
}
264+
};
265+
} else {
266+
// Otherwise derive network settings
267+
const customConfig = detectedConfig.networks[network] || {};
268+
const isDashboardNetwork = network === "dashboard";
269+
const configuredNetworkUrl = getConfiguredNetworkUrl(
270+
customConfig,
271+
isDashboardNetwork
272+
);
273+
const defaultNetworkId = isDashboardNetwork
274+
? "*"
275+
: managedGanacheDefaultNetworkId;
276+
277+
configuredNetwork = {
278+
network_id: customConfig.network_id || defaultNetworkId,
279+
provider: function () {
280+
return new Web3.providers.HttpProvider(configuredNetworkUrl, {
281+
keepAlive: false
282+
});
283+
},
284+
// customConfig will spread only when it is defined and ignored when undefined
285+
...customConfig
286+
};
287+
}
288+
289+
detectedConfig.networks[network] = {
290+
...configuredNetwork
291+
};
292+
293+
return detectedConfig;
294+
};
295+
209296
module.exports = {
210297
displayGeneralHelp,
211298
getCommand,
212299
prepareOptions,
213-
runCommand
300+
runCommand,
301+
getConfiguredNetworkUrl,
302+
deriveConfigEnvironment
214303
};

packages/core/lib/console-child.js

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,49 @@
11
const TruffleError = require("@truffle/error");
22
const Config = require("@truffle/config");
3-
const Web3 = require("web3");
43
const yargs = require("yargs");
4+
const { deriveConfigEnvironment } = require("./command-utils");
55

66
// we split off the part Truffle cares about and need to convert to an array
77
const input = process.argv[2].split(" -- ");
88
const inputStrings = input[1].split(" ");
99

10-
const managedGanacheDefaultHost = "127.0.0.1";
11-
const managedGanacheDefaultPort = 9545;
12-
const managedGanacheDefaultNetworkId = 5777;
13-
1410
// we need to make sure this function exists so ensjs doesn't complain as it requires
1511
// getRandomValues for some functionalities - webpack strips out the crypto lib
1612
// so we shim it here
1713
global.crypto = {
1814
getRandomValues: require("get-random-values")
1915
};
2016

21-
//detect config so we can get the provider and resolver without having to serialize
22-
//and deserialize them
23-
const { network, config, url } = yargs(input[0]).argv;
24-
const detectedConfig = Config.detect({ network, config });
25-
26-
function getConfiguredNetworkUrl(customConfig) {
27-
const configuredNetworkOptions = {
28-
host: customConfig.host || managedGanacheDefaultHost,
29-
port: customConfig.port || managedGanacheDefaultPort
30-
};
31-
return `http://${configuredNetworkOptions.host}:${configuredNetworkOptions.port}/`;
17+
function deriveConfig() {
18+
//detect config so we can get the provider and resolver without having to serialize
19+
//and deserialize them
20+
const { network, config, url } = yargs(input[0]).argv;
21+
const detectedConfig = Config.detect({ network, config });
22+
return deriveConfigEnvironment(detectedConfig, network, url);
3223
}
3324

34-
let configuredNetwork;
35-
36-
if (url) {
37-
// Use "url" to configure network (implies not "develop")
38-
configuredNetwork = {
39-
network_id: "*",
40-
url
41-
};
42-
} else {
43-
// Otherwise derive network settings
44-
const customConfig = detectedConfig.networks[network] || {};
45-
46-
configuredNetwork = {
47-
host: customConfig.host || managedGanacheDefaultHost,
48-
port: customConfig.port || managedGanacheDefaultPort,
49-
network_id: customConfig.network_id || managedGanacheDefaultNetworkId,
50-
url: getConfiguredNetworkUrl(customConfig)
51-
};
52-
}
25+
function main() {
26+
const { getCommand, prepareOptions, runCommand } = require("./command-utils");
27+
const config = deriveConfig();
28+
const command = getCommand({ inputStrings, options: {}, noAliases: false });
29+
const options = prepareOptions({
30+
command,
31+
inputStrings,
32+
options: config
33+
});
5334

54-
detectedConfig.networks[network] = {
55-
...configuredNetwork,
56-
provider: function () {
57-
return new Web3.providers.HttpProvider(configuredNetwork.url, {
58-
keepAlive: false
35+
runCommand(command, options)
36+
.then(() => process.exit(0))
37+
.catch(error => {
38+
// Perform error handling ourselves.
39+
if (error instanceof TruffleError) {
40+
console.log(error.message);
41+
} else {
42+
// Bubble up all other unexpected errors.
43+
console.log(error.stack || error.toString());
44+
}
45+
process.exit(1);
5946
});
60-
}
61-
};
62-
63-
const { getCommand, prepareOptions, runCommand } = require("./command-utils");
64-
const command = getCommand({ inputStrings, options: {}, noAliases: false });
65-
const options = prepareOptions({
66-
command,
67-
inputStrings,
68-
options: detectedConfig
69-
});
47+
}
7048

71-
runCommand(command, options)
72-
.then(() => process.exit(0))
73-
.catch(error => {
74-
// Perform error handling ourselves.
75-
if (error instanceof TruffleError) {
76-
console.log(error.message);
77-
} else {
78-
// Bubble up all other unexpected errors.
79-
console.log(error.stack || error.toString());
80-
}
81-
process.exit(1);
82-
});
49+
main();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const assert = require("chai").assert;
2+
const fs = require("fs-extra");
3+
const path = require("path");
4+
const tmp = require("tmp");
5+
const TruffleConfig = require("@truffle/config");
6+
const { deriveConfigEnvironment } = require("../../lib/command-utils");
7+
8+
let config;
9+
10+
function createSandbox(source) {
11+
if (!fs.existsSync(source)) {
12+
throw new Error(`Sandbox failed: source: ${source} does not exist`);
13+
}
14+
15+
const tempDir = tmp.dirSync({ unsafeCleanup: true });
16+
fs.copySync(source, tempDir.name);
17+
const config = TruffleConfig.load(
18+
path.join(tempDir.name, "truffle-config.js"),
19+
{}
20+
);
21+
return config;
22+
}
23+
24+
describe("command-utils", function () {
25+
before(function () {
26+
config = createSandbox(
27+
path.join(__dirname, "..", "sources", "command-utils")
28+
);
29+
});
30+
31+
describe("deriveConfigEnvironment", function () {
32+
it("returns a config with specified network object having a provider property", function () {
33+
const expectedNetworkConfig = config.networks.crazyTimeNetwork;
34+
const cfg = deriveConfigEnvironment(
35+
config,
36+
"crazyTimeNetwork",
37+
undefined
38+
);
39+
assert.equal(
40+
cfg.networks.crazyTimeNetwork.confirmations,
41+
expectedNetworkConfig.confirmations
42+
);
43+
assert.equal(
44+
cfg.networks.crazyTimeNetwork.customUserProperty,
45+
expectedNetworkConfig.customUserProperty
46+
);
47+
assert.equal(
48+
cfg.networks.crazyTimeNetwork.provider,
49+
expectedNetworkConfig.provider
50+
);
51+
});
52+
53+
it("returns a config with a network object having the specified url property ", function () {
54+
const testUrl = "http://localhost:5555";
55+
const cfg = deriveConfigEnvironment(config, "anyTimeNetwork", testUrl);
56+
assert.equal(cfg.networks["anyTimeNetwork"].url, testUrl);
57+
});
58+
59+
it("returns a config with a network object having user specified properties", function () {
60+
const expectedNetworkConfig = config.networks.funTimeNetwork;
61+
const cfg = deriveConfigEnvironment(config, "funTimeNetwork", undefined);
62+
assert.equal(
63+
cfg.networks.funTimeNetwork.host,
64+
expectedNetworkConfig.host
65+
);
66+
assert.equal(
67+
cfg.networks.funTimeNetwork.port,
68+
expectedNetworkConfig.port
69+
);
70+
assert.equal(
71+
cfg.networks.funTimeNetwork.confirmations,
72+
expectedNetworkConfig.confirmations
73+
);
74+
assert.equal(
75+
cfg.networks.funTimeNetwork.customUserProperty,
76+
expectedNetworkConfig.customUserProperty
77+
);
78+
});
79+
80+
it("returns a config with a dashboard network object having user specified properties", function () {
81+
const expectedNetworkConfig = config.networks.dashboard;
82+
const cfg = deriveConfigEnvironment(config, "dashboard", undefined);
83+
assert.equal(cfg.networks.dashboard.url, expectedNetworkConfig.url);
84+
assert.equal(
85+
cfg.networks.dashboard.confirmations,
86+
expectedNetworkConfig.confirmations
87+
);
88+
assert.equal(
89+
cfg.networks.dashboard.customUserProperty,
90+
expectedNetworkConfig.customUserProperty
91+
);
92+
});
93+
94+
it("returns a config with a develop network object having default managed Ganache properties", function () {
95+
const cfg = deriveConfigEnvironment(config, "develop", undefined);
96+
assert.equal(cfg.networks.develop.network_id, 5777);
97+
});
98+
});
99+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
networks: {
3+
funTimeNetwork: {
4+
host: "127.0.0.1",
5+
port: 5555,
6+
network_id: "*",
7+
confirmations: 2,
8+
customUserProperty: "Fun time"
9+
},
10+
crazyTimeNetwork: {
11+
network_id: "*",
12+
provider: () => "http://localhost:5555",
13+
confirmations: 2,
14+
customUserProperty: "Crazy time"
15+
},
16+
dashboard: {
17+
network_id: "*",
18+
confirmations: 2,
19+
customUserProperty: "Dashboard time"
20+
}
21+
}
22+
};

0 commit comments

Comments
 (0)