Skip to content

Commit

Permalink
fix: allow connecting to infura nodes
Browse files Browse the repository at this point in the history
Use connectionTester instead of is-reachable to prevent false negatives when checking node connectivity.

This fixes #45
  • Loading branch information
TripleSpeeder committed Dec 10, 2019
1 parent cbadc6d commit 68fa986
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 104 deletions.
112 changes: 12 additions & 100 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"@ensdomains/resolver": "^0.1.13",
"@truffle/contract": "^4.1.0",
"@truffle/hdwallet-provider": "^1.0.25",
"connection-tester": "^0.2.0",
"content-hash": "^2.5.0",
"dotenv": "^8.2.0",
"eth-ens-namehash": "^2.0.8",
"is-reachable": "^4.0.0",
"web3": "^1.2.2",
"yargs": "^14.2.0"
},
Expand Down
22 changes: 19 additions & 3 deletions src/middleware/connectionCheckMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
const isReachable = require('is-reachable');
const url = require('url');
const connectionTester = require('connection-tester');

const connectionCheck = async ({verbose, web3}) => {
verbose && console.log(`Checking connectivity of web3 node at ${web3}...`)
// Do initial check if there is a service running on provided endpoint. Relying on Web3/HDWalletProvider
// to fail in case node is not reachable is flaky, see https://github.com/TripleSpeeder/ens-updater/issues/25
const nodeIsReachable = await isReachable(web3)
if (!nodeIsReachable) {
let web3Url = url.parse(web3)
let port = web3Url.port
if (!port) {
// no port specified. Derive from protocol.
switch(web3Url.protocol) {
case 'https:':
case 'wss:':
port = 443
break
case 'http:':
case 'ws:':
default:
port = 80
}
}
const nodeIsReachable = connectionTester.test(web3Url.hostname, port, 2000)
if (!nodeIsReachable.success) {
throw Error(`Node is not reachable at ${web3}`)
}
verbose && console.log('\tConnection check successfull.')
Expand Down

0 comments on commit 68fa986

Please sign in to comment.