All files / src/chainManagement configureJoins.js

100% Statements 41/41
100% Branches 8/8
100% Functions 9/9
100% Lines 41/41
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                          3x 3x                   3x 6x 6x 6x 6x   6x 6x 6x 8x   6x     3x 6x 3x   3x   2x 2x         2x           3x 6x 3x   3x   2x 2x         2x           3x 6x 3x   3x   3x 3x       3x           3x 6x 3x   3x 1x 1x     1x       3x  
const {
  coreCovenant: {
    actionCreators: {
      startConsumeState,
      startProvideState,
      authorizeReceiveActions,
      authorizeSendActions
    }
  },
  manifest: {
    selectors: { getChainIdByAlias }
  },
  constants: { JOIN_TYPES }
} = require('interbit-covenant-tools')
const log = require('../log')
 
/**
 * Configures joins on the chain whose interface is passed as `chainInterface`,
 * based on the `joins` parameter. Uses the manifest for chain ID resolution
 * in the joins.
 * @param {Object} chainInterface - Interface to the chain to be configured.
 * @param {Object} joins - Join configuration for the chain.
 * @param {Object} interbitManifest - Manifest containing chain ID to alias resolutions.
 */
const configureJoins = (chainInterface, joins, interbitManifest) => {
  const consume = configureConsume(joins[JOIN_TYPES.CONSUME], interbitManifest)
  const provide = configureProvide(joins[JOIN_TYPES.PROVIDE], interbitManifest)
  const send = configureSend(joins[JOIN_TYPES.SEND], interbitManifest)
  const receive = configureReceive(joins[JOIN_TYPES.RECEIVE], interbitManifest)
 
  const joinActions = [...consume, ...provide, ...send, ...receive]
  log.info('JOINING', joinActions)
  for (const action of joinActions) {
    chainInterface.dispatch(action)
  }
  log.success('Chain join actions dispatched')
}
 
const configureConsume = (consume, interbitManifest) => {
  if (!Array.isArray(consume)) {
    return []
  }
  return consume.reduce(
    (prev, { alias: chainAlias, path: mount, joinName }) => {
      const provider = getChainIdByAlias(chainAlias, interbitManifest)
      const consumeAction = startConsumeState({
        provider,
        mount,
        joinName
      })
      return prev.concat(consumeAction)
    },
    []
  )
}
 
const configureProvide = (provide, interbitManifest) => {
  if (!Array.isArray(provide)) {
    return []
  }
  return provide.reduce(
    (prev, { alias: chainAlias, path: statePath, joinName }) => {
      const consumer = getChainIdByAlias(chainAlias, interbitManifest)
      const provideAction = startProvideState({
        consumer,
        statePath,
        joinName
      })
      return prev.concat(provideAction)
    },
    []
  )
}
 
const configureReceive = (receive, interbitManifest) => {
  if (!Array.isArray(receive)) {
    return []
  }
  return receive.reduce(
    (prev, { alias: chainAlias, authorizedActions: permittedActions }) => {
      const senderChainId = getChainIdByAlias(chainAlias, interbitManifest)
      const receiveAction = authorizeReceiveActions({
        senderChainId,
        permittedActions
      })
      return prev.concat(receiveAction)
    },
    []
  )
}
 
const configureSend = (send, interbitManifest) => {
  if (!Array.isArray(send)) {
    return []
  }
  return send.reduce((prev, { alias: chainAlias }) => {
    const receiverChainId = getChainIdByAlias(chainAlias, interbitManifest)
    const sendAction = authorizeSendActions({
      receiverChainId
    })
    return prev.concat(sendAction)
  }, [])
}
 
module.exports = configureJoins