| 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 | 2x
2x
2x
2x
2x
12x
12x
12x
12x
12x
10x
10x
10x
10x
10x
10x
10x
2x
2x
12x
12x
26x
12x
12x
26x
16x
26x
19x
7x
11x
1x
10x
2x
25x
24x
24x
21x
21x
21x
21x
21x
21x
21x
21x
2x
31x
124x
62x
124x
31x
21x
31x
2x
25x
1x
24x
31x
21x
2x
31x
31x
31x
2x
24x
24x
6x
2x
| const _ = require('lodash')
const hashObject = require('./hash')
const {
rootCovenant: { actionTypes },
config: {
selectors: {
getAdminValidators,
getChainCovenant,
getChainJoins,
getParentByChainAlias
},
validateConfig
},
constants: { ROOT_CHAIN_ALIAS }
} = require('interbit-covenant-tools')
const {
genesisBlockSelectors: { getChainId }
} = require('../genesisBlock')
const createManifestTree = (interbitConfig, manifest) => {
const config = validateConfig(interbitConfig)
const { genesisBlocks } = manifest
const chainId = getChainId(genesisBlocks[ROOT_CHAIN_ALIAS])
const validators = getAdminValidators(config)
const chains = getRootSubtrees(ROOT_CHAIN_ALIAS, config, manifest)
const covenant = ROOT_CHAIN_ALIAS
const covenantHashMap = getCovenantHashMap(
ROOT_CHAIN_ALIAS,
manifest.covenants,
chains
)
const joins = configureCascadingJoins(
undefined,
Object.keys(chains),
minimumJoinconfig
)
const chainIdMap = mergeJoinAndChildChainIds(joins, chains, manifest)
const manifestEntry = {
alias: ROOT_CHAIN_ALIAS,
chainId,
chainIdMap,
validators,
covenant,
covenantHashMap,
joins,
chains
}
const hash = hashObject(manifestEntry)
return {
[ROOT_CHAIN_ALIAS]: {
...manifestEntry,
hash
}
}
}
const minimumJoinconfig = {
consume: [],
provide: [],
receiveActionFrom: [],
sendActionTo: []
}
const getRootSubtrees = (chainAlias, config, manifest) => {
const staticChainEntries = Object.entries(config.staticChains)
const allChildChains = staticChainEntries.reduce(
(accum, [, chainConfig]) => accum.concat(chainConfig.childChains),
[]
)
const visited = []
const subtrees = staticChainEntries.reduce((accum, [childAlias]) => {
const isChainSubtreeRoot = !allChildChains.find(
childChain => childChain === childAlias
)
if (isChainSubtreeRoot) {
return {
...accum,
[childAlias]: getManifestEntry(childAlias, config, manifest, visited)
}
}
return accum
}, {})
if (_.isEmpty(subtrees)) {
throw new Error(
'Config contains malformed childChains structure. ChildChains must form one or many trees when constructed.'
)
}
return subtrees
}
const getManifestEntry = (chainAlias, config, manifest, visited) => {
const nowVisited = checkForCycles(chainAlias, visited)
const parentAlias =
getParentByChainAlias(chainAlias, config) || ROOT_CHAIN_ALIAS
const childChains = getChainsChildren(
chainAlias,
config,
manifest,
nowVisited
)
const covenant = getChainCovenant(chainAlias, config)
const covenantHashMap = getCovenantHashMap(
covenant,
manifest.covenants,
childChains
)
const existingJoins = getChainJoins(chainAlias, config)
const joins = configureCascadingJoins(
parentAlias,
Object.keys(childChains),
existingJoins
)
const chainIdMap = mergeJoinAndChildChainIds(joins, childChains, manifest)
const manifestEntry = {
alias: chainAlias,
chainId: getChainId(manifest.genesisBlocks[chainAlias]),
chainIdMap,
validators: getAdminValidators(config),
covenant,
covenantHashMap,
joins,
chains: childChains
}
const hash = hashObject(manifestEntry)
return {
...manifestEntry,
hash
}
}
const mergeJoinAndChildChainIds = (joins, childChains, manifest) => {
const joinChainIds = Object.values(joins).reduce((accum, joinsOfType) => {
const joinedChainIds = joinsOfType.reduce(
(accumJoin, join) => ({
...accumJoin,
[join.alias]: manifest.chains[join.alias]
}),
{}
)
return {
...accum,
...joinedChainIds
}
}, {})
const childChainIds = Object.values(childChains).reduce(
(accum, childChainEntry) => ({
...accum,
[childChainEntry.alias]: manifest.chains[childChainEntry.alias]
}),
{}
)
return {
...childChainIds,
...joinChainIds
}
}
const checkForCycles = (chainAlias, visited) => {
if (visited.find(node => node === chainAlias)) {
throw new Error(
`Config contains malformed childChains structure and must form one or many trees. "${chainAlias}" was referenced twice.`
)
}
return visited.concat(chainAlias)
}
const getCovenantHashMap = (myCovenantAlias, allCovenants, chains) => ({
[myCovenantAlias]: allCovenants[myCovenantAlias].hash,
...Object.values(chains).reduce(
(accum, chain) => ({
...accum,
...chain.covenantHashMap
}),
{}
)
})
const configureCascadingJoins = (parentAlias, childAliases, existingJoins) => {
const parentJoin = parentAlias
? {
alias: parentAlias,
authorizedActions: [actionTypes.SET_MANIFEST]
}
: []
const childJoins = childAliases.map(childAlias => ({
alias: childAlias
}))
return {
...existingJoins,
sendActionTo: existingJoins.sendActionTo.concat(childJoins),
receiveActionFrom: existingJoins.receiveActionFrom.concat(parentJoin)
}
}
const getChainsChildren = (chainAlias, config, manifest, visited) => {
const chainConfig = config.staticChains[chainAlias]
return chainConfig.childChains.reduce(
(accum, childAlias) => ({
...accum,
[childAlias]: getManifestEntry(childAlias, config, manifest, visited)
}),
{}
)
}
module.exports = {
createManifestTree
}
|