All files / src/chainManagement joinChains.js

63.77% Statements 44/69
58.33% Branches 21/36
100% Functions 5/5
63.77% Lines 44/69
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                          2x   2x                       2x 1x 1x 1x 2x 2x 2x         2x         2x         2x           2x 2x 2x         2x   2x 2x     1x             2x 2x 1x   1x 1x       1x 1x         1x 1x       2x 2x 1x   1x 1x       1x 1x         1x 1x       2x         2x 2x                                         2x 2x 2x                               2x  
const {
  coreCovenant: {
    actionCreators: {
      startConsumeState,
      startProvideState,
      authorizeReceiveActions,
      authorizeSendActions
    }
  },
  config: {
    selectors: { getChainJoins, getJoinTypeForChain }
  },
  constants: { JOIN_TYPES }
} = require('interbit-covenant-tools')
 
const log = require('../log')
 
// SET FOR DEPRECATION: Pending issue #79
/**
 * Configures chains in the node's cli to be joined according to config.
 * Used within the interbit `start` script and set for pending deprecation. (#263)
 * @deprecated
 * @param {Object} manifest - The covenant hashes and chain ids mapped to aliases
 *  for the node from the `generateDeploymentDetails` function.
 * @param {Object} cli - The cli for the node.
 * @param {Object} config - The interbit config file specifying join configuration.
 */
const joinChains = async (manifest, cli, config) => {
  log.info('JOINING CHAINS')
  try {
    for (const [chainAlias, chainDetails] of Object.entries(manifest)) {
      const chainInterface = await cli.getChain(chainDetails.chainId)
      Eif (getChainJoins(chainAlias, config)) {
        const provide = getJoinTypeForChain(
          chainAlias,
          JOIN_TYPES.PROVIDE,
          config
        )
        const consume = getJoinTypeForChain(
          chainAlias,
          JOIN_TYPES.CONSUME,
          config
        )
        const sendActionTo = getJoinTypeForChain(
          chainAlias,
          JOIN_TYPES.SEND,
          config
        )
        const receiveActionFrom = getJoinTypeForChain(
          chainAlias,
          JOIN_TYPES.RECEIVE,
          config
        )
 
        await establishConsumes(chainInterface, consume, manifest)
        await establishProvides(chainInterface, provide, manifest)
        await establishReceiveActions(
          chainInterface,
          receiveActionFrom,
          manifest
        )
        await establishSendActions(chainInterface, sendActionTo, manifest)
      }
      const state = await chainInterface.getState()
      log.info({ chainAlias, chainDetails, state })
    }
 
    log.success('JOINING COMPLETE')
  } catch (err) {
    log.error(err.message)
    throw err
  }
}
 
const establishConsumes = async (chainInterface, consume, manifest) => {
  if (!consume || !consume.length) {
    return
  }
  for (const { alias, path: mount, joinName } of consume) {
    Iif (!alias || !manifest[alias]) {
      log.warn(`Unknown alias: ${alias}`)
      continue
    }
    const { chainId: provider } = manifest[alias]
    const consumeAction = startConsumeState({
      provider,
      mount,
      joinName
    })
    log.action(consumeAction)
    await chainInterface.dispatch(consumeAction)
  }
}
 
const establishProvides = async (chainInterface, provide, manifest) => {
  if (!provide || !provide.length) {
    return
  }
  for (const { alias, path: statePath, joinName } of provide) {
    Iif (!alias || !manifest[alias]) {
      log.warn(`Unknown alias: ${alias}`)
      continue
    }
    const { chainId: consumer } = manifest[alias]
    const provideAction = startProvideState({
      consumer,
      statePath,
      joinName
    })
    log.action(provideAction)
    await chainInterface.dispatch(provideAction)
  }
}
 
const establishReceiveActions = async (
  chainInterface,
  receiveActionFrom,
  manifest
) => {
  Eif (!receiveActionFrom || !receiveActionFrom.length) {
    return
  }
  for (const { alias, authorizedActions } of receiveActionFrom) {
    if (!alias || !manifest[alias]) {
      log.warn(`Unknown alias: ${alias}`)
      continue
    }
    if (!authorizedActions) {
      log.warn(`No authorized actions for write join to: ${alias}`)
      continue
    }
    const { chainId: senderChainId } = manifest[alias]
    const authorizeReceiveAction = authorizeReceiveActions({
      senderChainId,
      permittedActions: authorizedActions
    })
    log.action(authorizeReceiveAction)
    await chainInterface.dispatch(authorizeReceiveAction)
  }
}
 
const establishSendActions = async (chainInterface, sendActionTo, manifest) => {
  Eif (!sendActionTo || !sendActionTo.length) {
    return
  }
  for (const { alias } of sendActionTo) {
    if (!alias || !manifest[alias]) {
      log.warn(`Unknown alias: ${alias}`)
      continue
    }
    const { chainId: receiverChainId } = manifest[alias]
    const authorizeSendAction = authorizeSendActions({
      receiverChainId
    })
    log.action(authorizeSendAction)
    await chainInterface.dispatch(authorizeSendAction)
  }
}
 
module.exports = { joinChains }