-
Notifications
You must be signed in to change notification settings - Fork 28
/
channel-manager.js
62 lines (51 loc) · 2.59 KB
/
channel-manager.js
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
const fabricCLI = require('./fabric-cli');
const cfg = require('./config');
const _ = require('lodash');
const util = require('./util');
const logger = cfg.log4js.getLogger('ChannelManager');
class ChannelManager {
async joinChannel(channelId, fabricStarterClient) {
try {
const ret = await fabricStarterClient.joinChannel(channelId);
return ret;
} catch(error) {
logger.error("Error joining channel", error.message);
throw new Error(error.message);
}
}
async applyConfigToChannel(channelId, currentChannelConfigFile, configUpdateRes, fabricClient, admin) {
// await fabricCLI.downloadOrdererMSP(); //todo: try/catch ?
let channelGroupConfig = await fabricCLI.translateChannelConfig(currentChannelConfigFile);
logger.debug(`Got channel config ${channelId}:`, channelGroupConfig);
try {
// let channelConfigEnvelope = JSON.parse(channelConfigBlock.toString());
// let channelGroupConfig = _.get(channelConfigEnvelope, "data.data[0].payload.data.config");
let updatedConfig = _.merge({}, channelGroupConfig);
if (_.get(updatedConfig, "channel_group.groups")) {
_.merge(updatedConfig.channel_group.groups, configUpdateRes.outputJson);
}
logger.debug(`Channel updated config ${channelId}:`, updatedConfig);
let configUpdate = await fabricCLI.computeChannelConfigUpdate(channelId, channelGroupConfig, updatedConfig);
logger.debug(`Got updated envelope ${channelId}:`, _.toString(configUpdate));
const txId = fabricClient.newTransactionID(admin);
try {
let signature = await fabricClient.signChannelConfig(configUpdate);
let update = await fabricClient.updateChannel({
txId, name: channelId, config: configUpdate,
orderer: /*fabricClient.getOrderer(cfg.ORDERER_ADDR),*/ this.createOrderer(fabricClient),
signatures: [signature]
});
logger.info(`Update channel result ${channelId}:`, update);
} catch (e) {
logger.error(e);
}
} catch (e) {
logger.error(`Couldn't fetch/translate config for channel ${channelId}`, e);
throw e;
}
}
createOrderer(fabricClient, addr=cfg.ORDERER_ADDR, ordererRootTLSFile=cfg.ORDERER_TLS_CERT) {
return fabricClient.newOrderer(`grpcs://${addr}`, {pem: util.loadPemFromFile(ordererRootTLSFile)});
}
}
module.exports = new ChannelManager();