All files / src/chainManagement connectToPeers.js

55.56% Statements 10/18
25% Branches 3/12
50% Functions 1/2
55.56% Lines 10/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 542x     2x                 2x           2x 2x   2x 2x       2x     2x                                             2x  
const log = require('../log')
 
// Port 443 appropriate for https deployment
const DEFAULT_PORT = 443
 
/**
 * Attempts to connect a node to peers.
 * @param {Object} cli - The cli that is connecting to peers.
 * @param {Array} manifestPeers - The list of peers to connect to.
 * @param {number} maxRetries - The maximum number of times to attempt each connection.
 * @param {number} timeout - The maximum time to wait for each connection attempt.
 */
const connectToPeers = async (
  cli,
  manifestPeers = [],
  maxRetries = 25,
  timeout = 20000
) => {
  const peerOverride = process.env.CONNECT_TO_PEERS
  const peers = peerOverride ? peerOverride.split(',') : manifestPeers
 
  log.info('CONNECTING TO PEERS', peers)
  for (const peer of peers) {
    const [toAddress, toPort] = peer.split(':')
    await tryConnect({ cli, toAddress, toPort, maxRetries, timeout })
  }
  log.success('Connected to peers')
}
 
const tryConnect = async ({
  cli,
  toAddress,
  toPort = DEFAULT_PORT,
  maxRetries = 25,
  timeout = 20000
}) => {
  if (toAddress && toPort) {
    try {
      log.info(`Connecting: ${toAddress}:${toPort}`)
      await cli.connect(
        Number(toPort),
        toAddress,
        Number(maxRetries),
        Number(timeout)
      )
      log.info(`Connected to: ${toAddress}:${toPort}`)
    } catch (error) {
      log.warn(`Connection failed: ${toAddress}:${toPort}`)
    }
  }
}
 
module.exports = connectToPeers