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

Internal improvement: Improve network check #2510

Merged
merged 10 commits into from
Oct 31, 2019
32 changes: 19 additions & 13 deletions packages/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const debug = require("debug")("provider");
const Web3 = require("web3");
const { Web3Shim, InterfaceAdapter } = require("@truffle/interface-adapter");
const wrapper = require("./wrapper");
const DEFAULT_NETWORK_CHECK_TIMEOUT = 5000;

module.exports = {
wrap: function(provider, options) {
Expand Down Expand Up @@ -33,33 +34,38 @@ module.exports = {
},

testConnection: function(options) {
let networkCheckTimeout, networkType;
const { networks, network } = options;
if (networks[network]) {
networkCheckTimeout =
networks[network].networkCheckTimeout || DEFAULT_NETWORK_CHECK_TIMEOUT;
networkType = networks[network].type;
} else {
networkCheckTimeout = DEFAULT_NETWORK_CHECK_TIMEOUT;
}
const provider = this.getProvider(options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still think calling getProvider and instantiating a new InterfaceAdapter/Web3Shim here is redundant, esp considering the rest of Environment.detect uses the already instantiated adapter/shim in the helpers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that using getProvider gives us is more flexibility with what options can be. Not using it to get the provider makes it so that the provider property will need to be a provider and it will not accept a host and port property etc. I'm not sure what behavior we want for this method. I'm not sure I have a strong preference, any thoughts?

Copy link
Contributor

@CruzMolina CruzMolina Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getter for config.provider will already return Provider.create which calls getProvider & then returns a normalized Provider. This also happens right before the testConnection call in Environment.detect.

It makes sense that testConnection should be testing an already normalized Provider.
Also makes sense to DRY up the code in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I understand what you are saying that it should have already parsed and set a provider on the config object. However what I am pointing out is that testConnection is exposed in this package. The getProvider method allows for the input to be a little more flexible.

I don't know what you mean by DRY here because bye using the getProvider method you are re-using existing code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I agree as an exposed pkg util func it's useful. It still feels repetitive to implement it in the Environment.detect flow.

const interfaceAdapter = new InterfaceAdapter({ provider });
const web3 = new Web3Shim({ provider });
const web3 = new Web3Shim({ provider, networkType });
eggplantzzz marked this conversation as resolved.
Show resolved Hide resolved
const interfaceAdapter = new InterfaceAdapter({ provider, networkType });
return new Promise((resolve, reject) => {
console.log("Testing the provider...");
console.log("=======================");
const noResponseFromNetworkCall = setTimeout(() => {
console.log(
"> There was a timeout while attempting to connect " +
"to the network."
);
const errorMessage =
"Failed to connect to the network using the " +
"supplied provider.\n Check to see that your provider is valid.";
"There was a timeout while attempting to connect to the network." +
"\n Check to see that your provider is valid.\n If you " +
"have a slow internet connection, try configuring a longer " +
"timeout in your Truffle config. Use the " +
"networks[networkName].networkCheckTimeout property to do this.";
throw new Error(errorMessage);
}, 20000);
}, networkCheckTimeout);
web3.eth
.getBlockNumber()
.then(() => {
console.log("> The test was successful!");
clearTimeout(noResponseFromNetworkCall);
resolve(true);
})
.catch(error => {
console.log(
"> Something went wrong while attempting to connect " +
"to the network."
"to the network. Check your network configuration."
);
clearTimeout(noResponseFromNetworkCall);
reject(error);
Expand Down