All files / src/chainManagement watchChain.js

33.33% Statements 11/33
0% Branches 0/6
0% Functions 0/10
34.38% Lines 11/32
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 932x   2x                         2x         2x   2x                       2x                               2x                                           2x         2x         2x             2x      
const { diff } = require('deep-diff')
 
const watchChain = (cli, chainInterface) => {
  let prevManifest = {}
 
  chainInterface.subscribe(() => {
    const rootState = chainInterface.getState()
    if (isManifestUpdated(rootState, prevManifest)) {
      const currManifest = getManifestFromState(rootState)
      applyManifestUpdates(cli, prevManifest, currManifest)
      prevManifest = currManifest
    }
  })
}
 
const isManifestUpdated = (rootState, prevManifest) => {
  const manifest = getManifestFromState(rootState)
  return manifest.deepEqual(prevManifest)
}
 
const getManifestFromState = rootState => rootState.manifest
 
const applyManifestUpdates = (cli, prevManifest, currManifest) => {
  // What are we diffing? The whole thing? Not helpful
 
  const difference = diff(prevManifest, currManifest)
 
  applyCovenantChanges(cli, difference)
  applyJoinChanges(cli, difference)
  applyRemovedChains(cli, difference)
  applyNewChains(cli, difference)
  applyChildChanges(cli, difference)
}
 
const applyCovenantChanges = (cli, difference) => {
  const isCovenantDifferent = difference.covenants
  if (isCovenantDifferent) {
    // For each changed covenant...
    // - Deploy the packed covenant ?
    // - Put it in the file layer but only if we're root?
    //
    // Blocked on cascading deployment spec #258
    //
    // I suspect that the thing to do is apply a rootWatcher for
    // thing watches that are cli specific (ie. deployCovenant, updating
    // static apps, root chain stuff) and a watcher for each child chain
    // to apply the changes from the root.
  }
}
 
const applyJoinChanges = (cli, difference) => {
  // TODO: Actually use the specified chainInterfaces
  // This will actually get the cli chain's ID, not the chain we are supposed to currently be changing
  const rootState = cli.getState()
  const MY_CHAIN_ID = rootState.interbit.chainId
  const isJoinsDifferent = difference.manifest[MY_CHAIN_ID].joins
  if (isJoinsDifferent) {
    // Re: Blocked on #258
    // This one to me is DEFINITELY in the camp of chain specific watcher...
    // This differentiation of the watcher types stems from the manifest
    // structure not being repeated exactly.
    //
    // The root of the manifest contains extra information that is not useful
    // to child chains such as file locations of covenants (for loading into
    // the file layer/deploying) and genesis blocks (for creating new chains)
    //
    // These root level manifest changes are ONLY in the root chain (hah)
    // As such, the interbit-covenant-tools needs to be able to tell it is the root chain
    // and pass itself it's own portion of the manifest tree to watch.
  }
}
 
const applyRemovedChains = (cli, difference) => {
  // Re: Blocked on #258
  // This belongs in the root cli watcher
}
 
const applyNewChains = (cli, difference) => {
  // Re: Blocked on #258
  // This belongs in the root cli watcher
}
 
const applyChildChanges = (cli, difference) => {
  // Dispatch SET_MANIFEST with subset of manifest to each child specified in manifest
  //
  // Re: Blocked on #258
  // This belongs to the chain specific watcher
}
 
module.exports = {
  watchChain
}