-
Notifications
You must be signed in to change notification settings - Fork 28
/
fabric-starter-client.js
841 lines (735 loc) · 33.2 KB
/
fabric-starter-client.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
const fs = require('fs');
const _ = require('lodash');
const cfg = require('./config.js');
const logger = cfg.log4js.getLogger('FabricStarterClient');
const Client = require('fabric-client');
const unzip = require('unzipper');
const path = require('path');
const urlParseLax = require('url-parse-lax');
const chmodPlus = require('chmod-plus');
const fabricCLI = require('./fabric-cli');
const util = require('./util');
const localDns = require('./util/local-dns');
const certsManager = require('./certs-manager');
const channelManager = require('./channel-manager');
//const networkConfigFile = '../crypto-config/network.json'; // or .yaml
//const networkConfig = require('../crypto-config/network.json');
const networkConfigProvider = require('./network');
const asLocalhost = (process.env.DISCOVER_AS_LOCALHOST === 'true');
const IS_ADMIN = true;
logger.debug(`invokeTimeout=${cfg.CHAINCODE_PROCESSING_TIMEOUT} asLocalhost=${asLocalhost}`);
class FabricStarterClient {
constructor(networkConfig, txEventQueue) {
FabricStarterClient.setDefaultConfigSettings(cfg.CRYPTO_SUIT_CONFIG);
this.networkConfig = networkConfig || networkConfigProvider(cfg.cas);
logger.info('constructing with network config:', JSON.stringify(this.networkConfig));
this.client = Client.loadFromConfig(this.networkConfig); // or networkConfigFile
this.peer = this.client.getPeersForOrg()[0];
// this.org = this.networkConfig.client.organization; //todo:?
this.channelsInitializationMap = new Map();
this.registerQueue = new Map();
this.clients = new Map()
this.txEventQueue = txEventQueue
}
static setDefaultConfigSettings(config) {
_.forEach(config, (value, key) => {
Client.setConfigSetting(key, value);
})
}
async init() { //todo: remove after removing from dns.js
// // await this.client.initCredentialStores();
// await this.ordererClient.initCredentialStores();
// this.fabricCaClient = cfg.AUTH_MODE === 'CA' ? this.ordererClient.getCertificateAuthority() : undefined;
// try {
// this.ordererClient.setAdminSigningIdentity(
// util.loadPemFromFile(certsManager.getPrivateKeyFilePath()),
// util.loadPemFromFile(certsManager.getSignCertPath()),
// cfg.ORDERER_MSPID
// );
// } catch (err) {
// logger.debug("Not orderer host")
// }
}
async login(username, password) {
if (cfg.AUTH_MODE === 'CA') {
await this.checkClientInitialized();
this.clearUsersPreviousLoginCache(username);
this.user = await this.client.setUserContext({username: username, password: password}, true);
} else if (cfg.AUTH_MODE === 'ADMIN') {
if (cfg.ENROLL_ID !== username || cfg.enrollSecret !== password) {
throw Error("Invalid credentials");
}
this.user = await this.createUserWithAdminRights(username, password);
} else {
throw Error("AUTH_MODE is not defined.");
}
}
clearUsersPreviousLoginCache(username) {
this.client.getStateStore() && this.client.getStateStore().setValue(username, '')
}
async checkClientInitialized() {
if (!this.client.getStateStore()) {
await this.client.initCredentialStores();
}
}
async createUserWithAdminRights(username) {
let mspId = cfg.org;// this.org;
return await this.client.createUser({
username: username,
mspid: mspId,
cryptoContent: {
privateKey: certsManager.getPrivateKeyFilePath(mspId),
signedCert: certsManager.getSignCertPath(mspId)
},
skipPersistence: true
});
}
logoutUser(userName) {
const userCachePath = _.get(this, "networkConfig.client.credentialStore.path");
if (userCachePath) {
const userFile = path.join(userCachePath, userName);
try {
fs.unlinkSync(userFile);
} catch (err) {
logger.debug(`Cannot remove credential store: ${userFile}`, err.Error || err);
}
}
}
async register(username, password, affiliation, role, attrs = []) {
if (cfg.AUTH_MODE === 'CA') {
await this.checkClientInitialized();
let fabricCaClient = this.client.getCertificateAuthority()
const registrar = fabricCaClient.getRegistrar()[0];
const admin = await this.client.setUserContext({
username: registrar.enrollId, //TODO: used to be enrollId, now unexpectedly ENROLL_ID
password: registrar.enrollSecret
});
return await fabricCaClient.register({
enrollmentID: username,
enrollmentSecret: password,
affiliation: affiliation || cfg.org,
maxEnrollments: -1,
role,
attrs
}, admin);
}
}
async loginOrRegister(username, password, affiliation) {
if (this.registerQueue[username]) {
return this.registerQueue[username];
}
this.registerQueue[username] = new Promise((resolve, reject) => {
return this.login(username, password).then(resolve)
.catch((err) => {
logger.error("Login error:", err);
return this.register(username, password, affiliation)
.then(() => this.login(username, password));
})
.then(() => this.registerQueue[username] = null)
.then(resolve)
.catch(err => {
reject(err);
});
});
return this.registerQueue[username];
}
async enroll(enrollmentId, password, profile) {
let fabricCaClient = this.client.getCertificateAuthority()
return await fabricCaClient.enroll({enrollmentID: enrollmentId, enrollmentSecret: password, profile})
}
getSecret() {
const signingIdentity = this.client._getSigningIdentity(true);
const signedBytes = signingIdentity.sign(cfg.org);
return String.fromCharCode.apply(null, signedBytes);
}
async queryChannels() {
const channelQueryResponse = await this.client.queryChannels(this.peer, true);
return channelQueryResponse.getChannels();
}
async queryPeers(orgName, peer) {
orgName = orgName || cfg.org;
peer = peer || this.peer;
const peerQueryResponse = await this.client.queryPeers({target: peer, useAdmin: true});
let peers = _.get(peerQueryResponse, `local_peers.${orgName}.peers`);
return _.map(peers, p => this.createPeerFromUrl(p.endpoint));
}
async queryInstalledChaincodes() {
const chaincodeQueryResponse = await this.client.queryInstalledChaincodes(this.peer, true);
return chaincodeQueryResponse.getChaincodes();
}
async createChannel(channelId) {
try {
logger.info(`Creating channel ${channelId}`);
// await fabricCLI.downloadOrdererMSP();
const tx_id = this.client.newTransactionID(true);
let orderer = this.client.getOrderer(cfg.ORDERER_ADDR); //this.createOrderer();
let channelReq = {
txId: tx_id,
name: channelId,
orderer: orderer
};
let channelTxContent = await fabricCLI.generateChannelConfigTxContent(channelId);
let config_update = this.client.extractChannelConfig(channelTxContent);
channelReq.config = config_update;
channelReq.signatures = [await this.client.signChannelConfig(config_update)];
// let res = await this.client.createChannel(channelReq);
fabricCLI.createChannelByCli(channelId);
// if (!res || res.status != "SUCCESS") {
// logger.error(res);
// throw new Error(res.info);
// }
} finally {
this.chmodCryptoFolder();
}
}
async joinChannel(channelId) {
logger.info(`Joining channel ${channelId}`);
// await fabricCLI.downloadOrdererMSP();
const tx2_id = this.client.newTransactionID(true);
let peers = [this.peer];//await this.queryPeers();
let channel = await this.constructChannel(channelId, peers);
let genesis_block = await channel.getGenesisBlock();//{txId: tx2_id});
let gen_tx_id = this.client.newTransactionID(true);
let j_request = {
targets: peers,
block: genesis_block,
txId: gen_tx_id
};
logger.debug(`Joining channel request [${channelId}]:`, j_request);
let result = await channel.joinChannel(j_request);
logger.debug(`Joined channel ${channelId}:`, result);
return result;
}
async addOrgToChannel(channelId, orgObj, certFiles) {
await this.checkOrgDns(orgObj);
try {
await util.checkRemotePort(cfg.addressFromTemplate(orgObj.peerName || 'peer0', orgObj.orgId, orgObj.domain), orgObj.peer0Port,
{from: `addOrgToChannel(${channelId}, ${orgObj})`}); //TODO: , throws: true
let currentChannelConfigFile = await fabricCLI.fetchChannelConfig(channelId);
let configUpdateRes = await fabricCLI.prepareOrgConfigStruct(orgObj, 'NewOrg.json', {NEWORG_PEER0_PORT: orgObj.peer0Port || cfg.DEFAULT_PEER0PORT}, certFiles);
let res = await channelManager.applyConfigToChannel(channelId, currentChannelConfigFile, configUpdateRes, this.client);
this.chmodCryptoFolder();
return res;
} catch (e) {
logger.error(e)
throw e;
} finally {
this.invalidateChannelsCache(channelId);
}
}
async addOrgToConsortium(orgObj, consortiumName, certFiles) {
await this.checkOrgDns(orgObj);
let currentChannelConfigFile = await fabricCLI.fetchChannelConfig(cfg.systemChannelId, certsManager.getOrdererMSPEnv());
let configUpdateRes = await fabricCLI.prepareOrgConfigStruct(orgObj, 'Consortium.json', {CONSORTIUM_NAME: consortiumName || cfg.DEFAULT_CONSORTIUM}, certFiles);
try {
let ordererClient = await this.initOrdererClient();
return channelManager.applyConfigToChannel(cfg.systemChannelId, currentChannelConfigFile, configUpdateRes, ordererClient, IS_ADMIN);
} catch (err) {
throw new Error("There is no Orderer on this node. Consortium cannot be updated.")
}
}
async initOrdererClient() {
try {
const ordererClient = Client.loadFromConfig(networkConfigProvider(cfg.cas)); //this.networkConfig);
await ordererClient.initCredentialStores();
ordererClient.setAdminSigningIdentity(
util.loadPemFromFile(certsManager.getPrivateKeyFilePath()),
util.loadPemFromFile(certsManager.getSignCertPath()),
cfg.ordererMspId
);
return ordererClient
} catch (err) {
logger.info("No orderer on host", err)
}
}
async getConsortiumMemberList(consortiumName = 'SampleConsortium') {
logger.debug("About to get consortium members list")
let result = [];
return new Promise(async (resolve, reject) => {
try {
// let channel = await (this.client.getChannel(cfg.systemChannelId, false) || this.constructChannel(cfg.systemChannelId));
// let sysChannelConfig = await channel.getChannelConfigFromOrderer();
let channelConfigBlock = await fabricCLI.fetchChannelConfig(cfg.systemChannelId, certsManager.getOrdererMSPEnv());
let channelGroupConfig = await fabricCLI.translateChannelConfig(channelConfigBlock);
logger.debug(channelGroupConfig);
// let consortium = _.get(sysChannelConfig, "config.channel_group.groups.map.Consortiums");
// let participants = _.get(consortium, 'value.groups.map.SampleConsortium.value.groups.map');
let consortium = _.get(channelGroupConfig, `channel_group.groups.Consortiums.groups.${consortiumName}`);
let participants = _.get(consortium, 'groups');
// return util.filterOrderersOut(participants);
result = _.filter(_.keys(participants), name => {
return !(_.startsWith(name, "Orderer") || _.startsWith(name, "orderer"));
});
return resolve(result);
} catch (err) {
logger.debug("Not enough permissions to access Consortium", err);
reject(err)
}
})
}
async checkOrgDns(orgObj) {
let chaincodeList = await this.queryInstantiatedChaincodes(cfg.DNS_CHANNEL);
// if (!chaincodeList || !chaincodeList.chaincodes.find(i => i.name === "dns"))
// return;
const dns = await this.query(cfg.DNS_CHANNEL, cfg.DNS_CHAINCODE, "get", '["dns"]', {targets: []});
try {
// let dnsRecordsList = dns && dns.length && JSON.parse(dns[0]);
const orgId = _.get(orgObj, "orgId");
const orgIp = _.get(orgObj, "orgIp");
if (orgIp) {
await this.invoke(cfg.DNS_CHANNEL, cfg.DNS_CHAINCODE, "registerOrg", [JSON.stringify(orgObj)], {targets: []}, true)
.then(() => util.sleep(cfg.DNS_UPDATE_TIMEOUT));
await localDns.updateLocalDnsStorageFromChaincode(this)
}
} catch (e) {
logger.warn("Error on dns info:", dns, e);
}
}
async constructChannel(channelId, optionalPeer) {
let channel = this.client.getChannel(channelId, false);
if (!channel) {
channel = this.client.newChannel(channelId);
channel.addOrderer(this.client.getOrderer(cfg.ORDERER_ADDR)); //this.createOrderer());
try {
if (optionalPeer) { //TODO: seems unnecessary code
optionalPeer = optionalPeer || await this.queryPeers();
if (_.isArray(optionalPeer)) {
optionalPeer = _.find(optionalPeer, p => _.startsWith(p.getName(), "peer0")) || optionalPeer[0];
}
channel.addPeer(optionalPeer);
}
} catch (e) {
logger.warn(`Error adding peer ${optionalPeer} to channel ${channelId}`, e);
}
}
return channel;
}
async getChannel(channelId) {
return await this.getChannelWithInitialization(channelId);
}
async getChannelWithInitialization(channelId) {
let inProcessPromise = this.channelsInitializationMap.get(channelId);
if (!inProcessPromise) {
inProcessPromise = new Promise(async (resolve, reject) => {
try {
logger.debug(`Initialise channel: ${channelId}`);
const channel = this.client.getChannel(channelId, false) || await this.constructChannel(channelId);
await channel.initialize({
discover: cfg.USE_SERVICE_DISCOVERY,
asLocalhost: asLocalhost,
target: this.peer
}); //TODO: is target needed
await channel.queryInfo(this.peer, true);
resolve(channel);
} catch (e) {
logger.error(e);
reject(e);
} finally {
this.channelsInitializationMap.set(channelId, null);
}
});
this.channelsInitializationMap.set(channelId, inProcessPromise);
}
return inProcessPromise;
}
getChannelEventHub(channel, options) {
//const channelEventHub = channel.getChannelEventHub(this.peer.getName());
const channelEventHub = channel.newChannelEventHub(this.peer.getName());
// const channelEventHub = channel.getChannelEventHubsForOrg()[0];
channelEventHub.connect(options, (err, data)=>{
err && logger.error(`Error at connection to ChannelEventHub: ${channel}`, err)
});
return channelEventHub;
}
async installChaincodeOld(chaincodeId, chaincodePath, version, language, baseDir) {
const peer = this.peer;
const client = this.client;
let fsClient = this;
return new Promise((resolve, reject) => {
fs.createReadStream(chaincodePath).pipe(unzip.Extract({path: language === 'golang' ? '/opt/gopath/src' : baseDir}))
.on('close', async function () {
try {
fs.unlinkSync(chaincodePath);
} catch (e) {
logger.warn("Deleting temp file failed: ", e)
}
let fullChaincodePath = path.resolve(__dirname, baseDir, chaincodeId);
/*
const chaincodePackageFile = fabricCLI.packageChaincodeWithInstantiationPolicy(chaincodeId, fullChaincodePath, version, language)
const proposal = {
targets: peer,
chaincodePackage: fs.readFileSync(chaincodePackageFile)
}
*/
let proposal = {
targets: peer,
chaincodeId: chaincodeId,
chaincodePath: language === 'golang' ? chaincodeId : fullChaincodePath,
chaincodeVersion: version || '1.0',
chaincodeType: language || 'node',
};
try {
const result = await client.installChaincode(proposal);
fsClient.errorCheck(result);
let msg = `Chaincode ${chaincodeId}:${version} successfully installed`;
logger.info(msg);
resolve(msg);
} catch (e) {
return reject(e);
}
});
});
}
async installChaincode(chaincodeId, chaincodePath, version, language) {
let that = this;
return new Promise(async (resolve, reject) => {
// let chaincodePath = language === 'golang' ? chaincodeId : path.resolve(/*__dirname,*/ chaincodePath, chaincodeId);
/*
const chaincodePackageFile = fabricCLI.packageChaincodeWithInstantiationPolicy(chaincodeId, fullChaincodePath, version, language)
const proposal = {
targets: peer,
chaincodePackage: fs.readFileSync(chaincodePackageFile)
}
*/
let proposal = {
targets: that.peer,
chaincodeId: chaincodeId,
chaincodePath: /*language === 'golang' ? chaincodeId :*/ chaincodePath,
chaincodeVersion: version || '1.0',
chaincodeType: language || 'node',
};
try {
const result = await that.client.installChaincode(proposal);
that.errorCheck(result);
let msg = `Chaincode ${chaincodeId}:${version} successfully installed`;
logger.info(msg);
resolve(msg);
} catch (e) {
return reject(e);
}
});
}
async instantiateChaincode(channelId, chaincodeId, type, fcn, args, version, targets, waitForTransactionEvent, policy, collections) {
const channel = await this.getChannel(channelId);
const tx_id = this.client.newTransactionID(true);
let collectionsConfigPath;
if (collections)
collectionsConfigPath = path.resolve(__dirname, collections);
let endorsmentPolicy;
if (policy)
endorsmentPolicy = JSON.parse(policy);
const proposal = {
chaincodeId: chaincodeId,
chaincodeType: type || 'node',
fcn: fcn || 'init',
args: args || [],
chaincodeVersion: version || '1.0',
txId: tx_id,
'endorsement-policy': endorsmentPolicy,
'collections-config': collectionsConfigPath
};
let badPeers;
if (targets) {
const targetsList = this.createTargetsList(channel, targets);
const foundPeers = targetsList.peers;
badPeers = targetsList.badPeers;
logger.trace('badPeers', badPeers);
proposal.targets = foundPeers;
}
logger.info('Sent instantiate proposal');
logger.trace(proposal);
const results = await channel.sendInstantiateProposal(proposal, cfg.CHAINCODE_PROCESSING_TIMEOUT);
this.errorCheck(results);
const transactionRequest = {
txId: tx_id,
proposalResponses: results[0],
proposal: results[1],
};
const promise = waitForTransactionEvent ? this.waitForTransactionEvent(tx_id, channel) : Promise.resolve(tx_id);
const broadcastResponse = await channel.sendTransaction(transactionRequest);
logger.trace('broadcastResponse', broadcastResponse);
return promise.then(function (res) {
res.badPeers = badPeers;
return res;
});
}
async upgradeChaincode(channelId, chaincodeId, type, fcn, args, version, targets, waitForTransactionEvent, policy, collections) {
const channel = await this.getChannel(channelId);
const tx_id = this.client.newTransactionID(true);
let collectionsConfigPath;
if (collections)
collectionsConfigPath = path.resolve(__dirname, collections);
let endorsmentPolicy;
if (policy)
endorsmentPolicy = JSON.parse(policy);
const proposal = {
chaincodeId: chaincodeId,
chaincodeType: type || 'node',
fcn: fcn || 'init',
args: args || [],
chaincodeVersion: version || '1.0',
txId: tx_id,
'endorsement-policy': endorsmentPolicy,
'collections-config': collectionsConfigPath
};
let badPeers;
if (targets) {
const targetsList = this.createTargetsList(channel, targets);
const foundPeers = targetsList.peers;
badPeers = targetsList.badPeers;
logger.trace('badPeers', badPeers);
proposal.targets = foundPeers;
}
logger.info('Sent upgrade proposal');
logger.trace(proposal);
const results = await channel.sendUpgradeProposal(proposal, cfg.CHAINCODE_PROCESSING_TIMEOUT);
this.errorCheck(results);
const transactionRequest = {
txId: tx_id,
proposalResponses: results[0],
proposal: results[1],
};
const promise = waitForTransactionEvent ? this.waitForTransactionEvent(tx_id, channel) : Promise.resolve(tx_id);
const broadcastResponse = await channel.sendTransaction(transactionRequest);
logger.trace('broadcastResponse', broadcastResponse);
return promise.then(function (res) {
res.badPeers = badPeers;
return res;
});
}
async invoke(channelId, chaincodeId, fcn, args, targets, waitForTransactionEvent, transientMap) {
const channel = await this.getChannel(channelId);
let fsClient = this;
const proposal = {
chaincodeId: chaincodeId,
fcn: fcn,
args: args
};
if (transientMap) {
proposal.transientMap =
Object.assign(...
Object.keys(transientMap).map((key) => {
return {[key]: Buffer.from(JSON.stringify(transientMap[key]))}
}))
}
let badPeers;
if (targets && (targets.targets || targets.peers)) {
const targetsList = this.createTargetsList(channel, targets);
const foundPeers = targetsList.peers;
badPeers = targetsList.badPeers;
logger.trace('badPeers', badPeers);
proposal.targets = foundPeers;
} else {
proposal.targets = [this.peer];
}
const logArgs = _.map(proposal.args, a => _.toString(a).substring(0, 250) + (_.size(a) > 250 ? ' ...' : ''))
logger.debug("Proposal", ({...proposal, args: logArgs}))
return await util.retryOperation(cfg.INVOKE_RETRY_COUNT, cfg.CHANNEL_LISTENER_UPDATE_TIMEOUT, async function () {
const txId = fsClient.client.newTransactionID(/*true*/);
proposal.txId = txId;
// logger.trace('invoke proposal', proposal.chaincodeId, proposal.fcn, proposal.args,);
let proposalResponse;
try {
proposalResponse = await channel.sendTransactionProposal(proposal);
fsClient.errorCheck(proposalResponse);
} catch (e) {
throw new Error(e);
}
const transactionRequest = {
// tx_id: tx_id,
proposalResponses: proposalResponse[0],
proposal: proposalResponse[1],
};
const promise = waitForTransactionEvent ? fsClient.waitForTransactionEvent(txId, channel) : Promise.resolve({txId: txId});
const broadcastResponse = await channel.sendTransaction(transactionRequest);
logger.trace('broadcastResponse', broadcastResponse);
return promise.then(function (res) {
res.PeersNotFound = badPeers;
res.chaincodeResult = _.get(proposalResponse, "[0].response.payload");
return res;
});
});
}
errorCheck(results) {
logger.trace('proposalResponse', results);
results.map(r => {
const checkError = _.toString(r);
if (_.startsWith(checkError, 'Error')) {
throw ({message: checkError, status: _.get(r, '[0].status') || _.get(r, 'status')});
}
});
}
async waitForTransactionEvent(tx_id, channel) {
const timeout = cfg.CHAINCODE_PROCESSING_TIMEOUT;
const id = tx_id.getTransactionID();
let timeoutHandle;
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(() => {
const msg = `timed out waiting for transaction ${id} after ${timeout}`;
logger.error(msg);
reject(new Error(msg));
// resolve({txid:id, msg: msg});
}, timeout);
});
let channelEventHub;
const eventPromise = new Promise((resolve, reject) => {
logger.trace(`registerTxEvent ${id}`);
if (!cfg.DISABLE_TX_ID_LISTENER) { //TODO: refactor
channelEventHub = this.getChannelEventHub(channel);
channelEventHub.registerTxEvent(id, (txid, status, blockNumber) => {
logger.debug(`committed transaction ${txid} as ${status} in block ${blockNumber}`);
resolve({txid: txid, status: status, blockNumber: blockNumber});
}, (e) => {
logger.error(`registerTxEvent ${e}`);
reject(new Error(e));
});
} else {
this.txEventQueue.waitForTransaction(id,resolve)
}
});
const racePromise = Promise.race([eventPromise, timeoutPromise]);
racePromise.catch(() => {
clearTimeout(timeoutHandle);
channelEventHub && channelEventHub.disconnect();
}).then(() => {
clearTimeout(timeoutHandle);
channelEventHub && channelEventHub.disconnect();
});
return racePromise;
}
_blockNumber(block){ //TODO: move to Block object
return block.number || _.get(block, "header.number");
}
_transactions(block) {
return block.filtered_transactions || _.map(_.get(block, 'data.data'), d=>{
const {tx_id, ...rest} = _.get(d,'payload.header.channel_header');
const {status, ...rest1} = _.get(d, 'payload.data.actions[0].payload.action.proposal_response_payload.extension.response')
return {txid: tx_id, status: status}
})
}
async query(channelId, chaincodeId, fcn, args, targets) {
const channel = await this.getChannel(channelId);
const proposal = {
chaincodeId: chaincodeId, fcn: fcn
};
logger.trace('query args', args);
if (args) {
proposal.args = JSON.parse(args);
} else {
proposal.args = [];
}
logger.trace('query targets', targets);
if (targets && (targets.targets || targets.peers)) {
const targetsList = this.createTargetsList(channel, targets);
const foundPeers = targetsList.peers;
const badPeers = targetsList.badPeers;
logger.trace('badPeers', badPeers);
proposal.targets = foundPeers;
}
// else {
// proposal.targets = [this.peer]; //TODO: define option for single target
// }
logger.trace('query proposal', proposal);
const responses = await channel.queryByChaincode(proposal);
this.errorCheck(responses);
return responses.map(r => {
return r.toString('utf8');
});
}
createTargetsList(channel, targets) {
let peers = [];
let badPeers = [];
_.each(_.compact(_.concat([], targets.targets, targets.peers)), function (value) {
let peer = _.find(channel.getChannelPeers(), p => p._name === value);
if (peer) {
peers.push(peer);
} else {
logger.error(`Peer ${value} not found`);
badPeers.push(value);
}
});
if (_.isEmpty(peers)) {
logger.trace("Using default peer");
peers.push(this.peer);
} else
logger.trace("Using specified peer(s)");
return {
peers,
badPeers
};
}
chmodCryptoFolder() {
//chmodPlus.directory(666, cfg.CRYPTO_CONFIG_DIR, {recursive: true});
// logger.debug(`Permissions for ${cfg.CRYPTO_CONFIG_DIR} folder are set to 666`);
}
invalidateChannelsCache(channelId) {
this.client._channels = new Map(); //TODO: workaround until sdk supports cache invalidating
}
async getOrganizations(channelId, filter) {
const channel = await this.getChannel(channelId);
const organizations = channel.getOrganizations();
return filter ? util.filterOrderersOut(organizations) : organizations;
}
async queryInstantiatedChaincodes(channelId) {
try {
const channel = await this.getChannel(channelId);
return await channel.queryInstantiatedChaincodes();
} catch (e) {
return null;
}
}
async queryInfo(channelId) {
const channel = await this.getChannel(channelId);
return await channel.queryInfo(this.peer, true);
}
async queryBlock(channelId, number, admin = false) {
const channel = await this.getChannel(channelId);
return await channel.queryBlock(number, this.peer, admin);
}
async queryTransaction(channelId, id) {
const channel = await this.getChannel(channelId);
return await channel.queryTransaction(id, this.peer, /*, true*/);
}
getPeersForOrg(mspid) {
return this.client.getPeersForOrg(mspid);
}
getMspid() {
return this.client.getMspid();
}
getNetworkConfig() {
return this.networkConfig;
}
async getPeersForOrgOnChannel(channelId) {
let peers = [];
const channel = await this.getChannel(channelId);
_.forEach(channel.getChannelPeers(), function (value) {
peers.push(value._name)
});
return peers;
}
async registerBlockEvent(channelId, onEvent, onError, eventHubConnectOptions) {
const channel = await this.getChannel(channelId);
const channelEventHub = this.getChannelEventHub(channel, eventHubConnectOptions);
return channelEventHub.registerBlockEvent(onEvent, onError);
}
async disconnectChannelEventHub(channelId) {
const channel = await this.getChannel(channelId);
const channelEventHub = this.getChannelEventHub(channel);
return channelEventHub.disconnect();
}
createPeerFromUrl(peerEndpoint) {
let connectionOptions = this.defaultConnectionOptions(peerEndpoint);
return this.client.newPeer(`grpcs://${peerEndpoint}`, connectionOptions);
}
defaultConnectionOptions(peerUrl, org, domain) {
let parsedUrl = urlParseLax(peerUrl);
let mspSubPath = parsedUrl.hostname;
let connectionOptions = {
'ssl-target-name-override': peerUrl,
//'ssl-target-name-override': 'localhost',
pem: util.loadPemFromFile(`${cfg.ORG_CRYPTO_DIR}/peers/${mspSubPath}/msp/tlscacerts/tlsca.${org || cfg.org}.${domain || cfg.domain}-cert.pem`)
};
return connectionOptions;
}
}
module.exports = FabricStarterClient;