All files / src/chainManagement createChains.js

92.86% Statements 52/56
50% Branches 2/4
100% Functions 2/2
92.86% Lines 52/56
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 1322x                         2x   2x 2x 2x 2x                   2x 1x 1x 1x 1x 1x 2x     2x 2x       1x 1x   1x 1x 1x 3x 3x 3x     3x 3x 3x   3x 3x             1x                     2x 1x   1x   1x   1x         1x       1x 1x 1x 2x   2x   2x 2x   2x         2x   2x 2x     2x           1x 1x 1x   1x             2x  
const path = require('path')
 
const {
  config: {
    selectors: { getChains, getCovenants, getPeers }
  },
  manifest: {
    selectors: {
      getGenesisBlocks,
      getCovenants: getCovenantsFromManifest,
      getPeers: getPeersFromManifest
    }
  }
} = require('interbit-covenant-tools')
 
const log = require('../log')
const deployCovenants = require('./deployCovenants')
const connectToPeers = require('./connectToPeers')
const { joinChains } = require('./joinChains')
 
/**
 * Uses a manifest to create chains from genesis blocks and configures the
 * cli with peers and covenants from the manifest.
 * @param {string} location - The file location to work from.
 * @param {Object} cli - The Interbit cli to configure and create chains on.
 * @param {Object} manifest - The Interbit manifest with config options.
 * @param {Object} options - Additional options for configuration.
 */
const createChainsFromManifest = async (location, cli, manifest, options) => {
  log.info('DEPLOYING COVENANTS')
  Eif (!options.connect) {
    const covenants = getCovenantsFromManifest(manifest)
    const covenantEntries = Object.values(covenants)
    for (const covenantManifest of covenantEntries) {
      const covenantLocation = path.resolve(
        `${location}/${covenantManifest.filename}`
      )
      await cli.deployCovenant(covenantLocation)
      log.info(`...deployed${covenantLocation}`)
    }
  }
 
  const peers = getPeersFromManifest(manifest)
  await connectToPeers(cli, peers)
 
  const genesisBlocks = getGenesisBlocks(manifest)
  const genesisBlockEntries = Object.values(genesisBlocks)
  for (const genesisBlock of genesisBlockEntries) {
    const chainId = genesisBlock.blockHash
    try {
      await cli.loadChain(chainId)
      log.info(`Loaded chain ${chainId}`)
    } catch (e) {
      Eif (e.message.startsWith('waitForState timeout after waiting 20000 ms')) {
        await cli.startChain({ genesisBlock })
        log.info(`Created chain ${chainId}`)
 
        const chain = cli.getChain(chainId)
        chain.dispatch({ type: '@@interbit/DEPLOY' })
      } else {
        throw e
      }
    }
  }
 
  log.success('Chains were created from manifest')
}
 
// SET FOR DEPRECATION: Pending issue #79
/**
 * Uses a config to create chains without genesis blocks or keys, and configures the
 * cli with peers and covenants from the manifest.
 * @deprecated
 * @param {Object} cli - The cli of the node to configure.
 * @param {Object} interbitConfig - The Interbit configuration to use.
 */
const createChainsFromConfig = async (cli, interbitConfig) => {
  log.info('BOOTING CHAINS')
 
  const chainsConfig = getChains(interbitConfig)
 
  const chainManifest = {}
 
  const covenantHashes = await deployCovenants({
    cli,
    covenantConfig: getCovenants(interbitConfig)
  })
 
  await connectToPeers(cli, getPeers(interbitConfig))
 
  // TODO: Make a root chain (this is the devMode createChains func) #276
 
  try {
    const chainAliases = Object.keys(chainsConfig)
    for (const chainAlias of chainAliases) {
      const chainConfig = chainsConfig[chainAlias]
 
      chainManifest[chainAlias] = {}
 
      const covenantHash = covenantHashes[chainConfig.covenant]
      const chainId = await cli.createChain()
 
      log.info('CREATING CHAIN: ', {
        chainAlias,
        covenantHash,
        chainId
      })
      await cli.applyCovenant(chainId, covenantHash)
 
      const chain = cli.getChain(chainId)
      chain.dispatch({ type: '@@interbit/START' })
 
      // add to manifest
      chainManifest[chainAlias] = {
        covenantHash,
        chainId
      }
    }
 
    log.info('BOOTING COMPLETE')
    await joinChains(chainManifest, cli, interbitConfig)
    log.success('Chains were created from config')
 
    return { chainManifest, covenantHashes }
  } catch (err) {
    log.error('CREATE CHAINS FAILURE:', err)
    throw err
  }
}
 
module.exports = { createChainsFromConfig, createChainsFromManifest }