All files / src/chainManagement configureChains.js

92% Statements 23/25
50% Branches 1/2
50% Functions 1/2
92% Lines 23/25
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              2x   2x 2x   2x                       2x 1x 1x   1x 2x 2x     2x 2x 2x       2x 2x 2x     2x     2x 2x 2x 2x     1x     2x  
const {
  rootCovenant: {
    actionCreators: { setManifest }
  },
  manifest: {
    selectors: { getRootChildren, getChainIdByAlias, getCovenantHashByAlias }
  }
} = require('interbit-covenant-tools')
 
const log = require('../log')
const configureJoins = require('./configureJoins')
 
process.on('unhandledRejection', reason => {
  log.error(`Caught unhandled rejection. Reason: ${reason}`)
  log.error(reason)
})
 
/**
 * Configures the chains on a node to the specifications in the Interbit
 * manifest file passed as `interbitManifest`. Applies covenants, configures joins, and
 * dispatches the manifest to the chain.
 * @param {Object} cli - Interbit cli for the node to configure.
 * @param {Object} interbitManifest - Manifest file specifying configuration.
 */
const configureChains = async (cli, interbitManifest) => {
  const childChains = getRootChildren(interbitManifest)
  const childChainEntries = Object.entries(childChains)
 
  for (const [chainAlias, chainEntry] of childChainEntries) {
    const chainId = getChainIdByAlias(chainAlias, interbitManifest)
    const chainInterface = cli.getChain(chainId)
 
    // TODO: Set covenants in watchers after cascading deployment is available
    const covenantHash = getCovenantHashByAlias(chainAlias, interbitManifest)
    log.info(`Applying covenant ${covenantHash} to ${chainAlias} (${chainId})`)
    await cli.applyCovenant(chainId, covenantHash)
 
    // TODO: Apply interbit-covenant-tools to root #267
 
    const joins = chainEntry.joins
    Eif (joins) {
      configureJoins(chainInterface, joins, interbitManifest)
    }
 
    chainInterface.dispatch({ type: '@@interbit/DEPLOY' })
 
    // TODO: Only dispatch this to the root once cascading deployment is available
    const setManifestAction = setManifest(interbitManifest)
    log.info('Set the manifest')
    log.action(setManifestAction)
    chainInterface.dispatch(setManifestAction)
  }
 
  log.success('Chains were configured')
}
 
module.exports = configureChains