All files / src/chainManagement startInterbit.js

96.55% Statements 28/29
63.64% Branches 7/11
50% Functions 2/4
96.55% Lines 28/29
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 652x       2x   2x                             2x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x     4x 4x 4x     4x 4x       4x 4x 4x   4x 4x 4x             4x   4x     2x  
const _ = require('lodash')
const {
  createHypervisor,
  createCli
} = require('interbit-core/dist/bundle.server')
 
const log = require('../log')
 
/**
 * Starts an interbit node, returning the cli, hypervisor, and a function
 * to safely cleanup the node.
 * @param {Object} keyPair - The key pair to use to create and authorize this
 *  node in the network.
 * @param {String} keyPair.publicKey - Public key for this key pair.
 * @param {String} keyPair.privateKey - Private key for this key pair.
 * @param {Object} options - The options used to configure and create this node.
 * @param {String|number} options.port - Port for this node to bind to.
 * @param {String} options.dbPath - Filepath to hold this node's database.
 * @returns {{ cli:Object, hypervisor:Object, cleanup:Function }} The cli interface
 *  for the Interbit node, the node's hypervisor, and a function to cleanup the node.
 */
const startInterbit = async (keyPair, options = {}) => {
  const port = _.get(options, ['port']) || 5000
  const dbPath = _.get(options, ['dbPath'])
  const originalDbPath = process.env.DB_PATH
 
  let cli = { shutdown: () => {} }
  let hypervisor = { stopHyperBlocker: () => {} }
  const cleanup = async () => {
    log.info('SHUTTING-DOWN CLI')
    await cli.shutdown()
    log.info('STOPPING HYPERVISOR')
    hypervisor.stopHyperBlocker()
  }
 
  try {
    Eif (dbPath) {
      process.env.DB_PATH = dbPath
    }
 
    log.info(`STARTING HYPERVISOR: DB_PATH=${process.env.DB_PATH}`)
    hypervisor = keyPair
      ? await createHypervisor({ keyPair })
      : await createHypervisor()
 
    log.info(`CREATING CLI: PORT=${port}`)
    cli = await createCli(hypervisor)
    await cli.startServer(port)
  } finally {
    Eif (dbPath) {
      Eif (originalDbPath) {
        process.env.DB_PATH = originalDbPath
      } else {
        delete process.env.DB_PATH
      }
    }
  }
 
  log.success('Started interbit node')
 
  return { hypervisor, cli, cleanup }
}
 
module.exports = startInterbit