-
Notifications
You must be signed in to change notification settings - Fork 948
/
SyncService.ts
768 lines (719 loc) · 20.5 KB
/
SyncService.ts
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
/*
* SPDX-License-Identifier: Apache-2.0
*/
import * as convertHex from 'convert-hex';
import fabprotos from 'fabric-protos';
import includes from 'lodash/includes';
import { helper } from '../../../common/helper';
import { ExplorerError } from '../../../common/ExplorerError';
import { explorerError } from '../../../common/ExplorerMessage';
import * as FabricConst from '../../../platform/fabric/utils/FabricConst';
import * as FabricUtils from '../../../platform/fabric/utils/FabricUtils';
const logger = helper.getLogger('SyncServices');
const fabric_const = FabricConst.fabric.const;
const blocksInProcess = [];
// Transaction validation code
const _validation_codes = {};
for (const key in fabprotos.protos.TxValidationCode) {
const new_key = fabprotos.protos.TxValidationCode[key];
_validation_codes[new_key] = key;
}
/**
*
*
* @class SyncServices
*/
export class SyncServices {
persistence: any;
platform: any;
synchInProcess: string[];
/**
* Creates an instance of SyncServices.
* @param {*} platform
* @param {*} persistence
* @memberof SyncServices
*/
constructor(platform, persistence) {
this.platform = platform;
this.persistence = persistence;
this.synchInProcess = [];
}
/**
*
*
* @memberof SyncServices
*/
/*eslint-disable */
async initialize() {}
/* eslint-enable */
/**
*
*
* @param {*} client
* @returns
* @memberof SyncServices
*/
async synchNetworkConfigToDB(client) {
const channels = client.getChannels();
const channels_query = await client.fabricGateway.queryChannels();
if (!channels_query) {
logger.error('Not found any channels');
return false;
}
for (const channel of channels_query.channels) {
const channel_name = channel.channel_id;
if (!includes(channels, channel_name)) {
await client.initializeNewChannel(channel_name);
}
}
for (const channel_name of channels) {
logger.info(
'SyncServices.synchNetworkConfigToDB client ',
client.getNetworkId(),
' channel_name ',
channel_name
);
const block = await client.getGenesisBlock(channel_name);
const channel_genesis_hash = await FabricUtils.generateBlockHash(
block.header
);
client.setChannelGenHash(channel_name, channel_genesis_hash);
const res = await this.insertNewChannel(
client,
channel_name,
block,
channel_genesis_hash
);
if (res) {
await this.insertFromDiscoveryResults(
client,
channel_name,
channel_genesis_hash
);
} else {
return false;
}
}
return true;
}
// Insert new channel to DB
/**
*
*
* @param {*} client
* @param {*} channel
* @param {*} block
* @param {*} channel_genesis_hash
* @returns
* @memberof SyncServices
*/
async insertNewChannel(client, channel_name, block, channel_genesis_hash) {
const network_id = client.getNetworkId();
const channelInfo = await this.persistence
.getCrudService()
.getChannel(network_id, channel_name, channel_genesis_hash);
if (!channelInfo) {
const count = await this.persistence
.getCrudService()
.existChannel(network_id, channel_name);
if (count.count === '0') {
if (block.data && block.data.data.length > 0 && block.data.data[0]) {
const createdt = await FabricUtils.getBlockTimeStamp(
block.data.data[0].payload.header.channel_header.timestamp
);
const channel_row = {
name: channel_name,
createdt,
blocks: 0,
trans: 0,
channel_hash: '',
channel_version: block.data.data[0].payload.header.channel_header.version,
channel_genesis_hash
};
await this.persistence
.getCrudService()
.saveChannel(network_id, channel_row);
}
} else {
const notify = {
notify_type: fabric_const.NOTITY_TYPE_EXISTCHANNEL,
network_id: network_id,
channel_name
};
this.platform.send(notify);
throw new ExplorerError(explorerError.ERROR_2013, channel_name);
// Return false;
}
}
return true;
}
/**
*
*
* @param {*} client
* @param {*} channel
* @param {*} channel_genesis_hash
* @memberof SyncServices
*/
async insertFromDiscoveryResults(client, channel_name, channel_genesis_hash) {
const discoveryResults = await client.initializeChannelFromDiscover(
channel_name
);
// Insert peer
if (discoveryResults && discoveryResults.peers_by_org) {
for (const org_name in discoveryResults.peers_by_org) {
const org = discoveryResults.peers_by_org[org_name];
for (const peer of org.peers) {
await this.insertNewPeer(peer, channel_genesis_hash, client);
}
}
}
// Insert orderer
if (discoveryResults && discoveryResults.orderers) {
for (const org_name in discoveryResults.orderers) {
const org = discoveryResults.orderers[org_name];
for (const orderer of org.endpoints) {
orderer.org_name = org_name;
await this.insertNewOrderers(orderer, channel_genesis_hash, client);
}
}
}
// Insert chaincode
await this.insertNewChannelChaincode(
client,
channel_genesis_hash,
discoveryResults
);
}
/**
*
*
* @param {*} peer
* @param {*} channel_genesis_hash
* @param {*} client
* @memberof SyncServices
*/
async insertNewPeer(peer, channel_genesis_hash, client) {
let eventurl = '';
let requesturl = peer.endpoint;
const network_id = client.getNetworkId();
const host_port = peer.endpoint.split(':');
const peers = client.getNetworkConfig().peers;
if (peers && peers[host_port[0]] && peers[host_port[0]].url) {
requesturl = peers[host_port[0]].url;
}
if (peers && peers[host_port[0]] && peers[host_port[0]].eventUrl) {
eventurl = peers[host_port[0]].eventUrl;
}
const peer_row = {
mspid: peer.mspid,
requests: requesturl.replace(/^grpcs*:\/\//, ''),
events: eventurl,
server_hostname: host_port[0],
channel_genesis_hash,
peer_type: 'PEER'
};
await this.persistence.getCrudService().savePeer(network_id, peer_row);
const channel_peer_row = {
peerid: host_port[0],
channelid: channel_genesis_hash
};
await this.persistence
.getCrudService()
.savePeerChannelRef(network_id, channel_peer_row);
}
/**
*
*
* @param {*} orderer
* @param {*} channel_genesis_hash
* @param {*} client
* @memberof SyncServices
*/
async insertNewOrderers(orderer, channel_genesis_hash, client) {
const network_id = client.getNetworkId();
const discoveryProtocol = client.fabricGateway.getDiscoveryProtocol();
const requesturl = `${discoveryProtocol}://${orderer.host}:${orderer.port}`;
logger.debug(
'insertNewOrderers discoveryProtocol ',
discoveryProtocol,
' requesturl ',
requesturl
);
const orderer_row = {
mspid: orderer.org_name,
requests: requesturl.replace(/^grpcs*:\/\//, ''),
server_hostname: orderer.host,
channel_genesis_hash,
peer_type: 'ORDERER'
};
await this.persistence.getCrudService().savePeer(network_id, orderer_row);
const channel_orderer_row = {
peerid: orderer.host,
channelid: channel_genesis_hash
};
await this.persistence
.getCrudService()
.savePeerChannelRef(network_id, channel_orderer_row);
}
/**
*
*
* @param {*} client
* @param {*} channel
* @param {*} channel_genesis_hash
* @param {*} discoveryResults
* @memberof SyncServices
*/
async insertNewChannelChaincode(
client,
channel_genesis_hash,
discoveryResults
) {
const network_id = client.getNetworkId();
const channel_name = client.getChannelNameByHash(channel_genesis_hash);
const chaincodes = await client.fabricGateway.queryInstantiatedChaincodes(
channel_name
);
for (const chaincode of chaincodes.chaincodes) {
let path = '-';
if (chaincode.path !== undefined) {
path = chaincode.path;
}
const chaincode_row = {
name: chaincode.name,
version: chaincode.version,
path: path,
txcount: 0,
createdt: new Date(),
channel_genesis_hash
};
await this.persistence
.getCrudService()
.saveChaincode(network_id, chaincode_row);
if (discoveryResults && discoveryResults.peers_by_org) {
for (const org_name in discoveryResults.peers_by_org) {
const org = discoveryResults.peers_by_org[org_name];
for (const peer of org.peers) {
for (const c_code of peer.chaincodes) {
if (
c_code.name === chaincode.name &&
c_code.version === chaincode.version
) {
await this.insertNewChaincodePeerRef(
client,
c_code,
peer.endpoint,
channel_genesis_hash
);
}
}
}
}
}
}
}
/**
*
*
* @param {*} chaincode
* @param {*} endpoint
* @param {*} channel_genesis_hash
* @memberof SyncServices
*/
async insertNewChaincodePeerRef(
client,
chaincode,
endpoint,
channel_genesis_hash
) {
const network_id = client.getNetworkId();
const host_port = endpoint.split(':');
const chaincode_peer_row = {
chaincodeid: chaincode.name,
cc_version: chaincode.version,
peerid: host_port[0],
channelid: channel_genesis_hash
};
await this.persistence
.getCrudService()
.saveChaincodPeerRef(network_id, chaincode_peer_row);
}
async synchBlocks(client, channel_name) {
const network_id = client.getNetworkId();
const synch_key = `${network_id}_${channel_name}`;
if (this.synchInProcess.includes(synch_key)) {
logger.info(`Block synch in process for >> ${network_id}_${channel_name}`);
return;
}
this.synchInProcess.push(synch_key);
// Get channel information from ledger
const channelInfo = await client.fabricGateway.queryChainInfo(channel_name);
const channel_genesis_hash = client.getChannelGenHash(channel_name);
const blockHeight = parseInt(channelInfo.height.low) - 1;
// Query missing blocks from DB
const results = await this.persistence
.getMetricService()
.findMissingBlockNumber(network_id, channel_genesis_hash, blockHeight);
if (results) {
for (const result of results) {
// Get block by number
const block = await client.fabricGateway.queryBlock(
channel_name,
result.missing_id
);
if (block) {
await this.processBlockEvent(client, block);
}
}
} else {
logger.debug('Missing blocks not found for %s', channel_name);
}
const index = this.synchInProcess.indexOf(synch_key);
this.synchInProcess.splice(index, 1);
}
/**
*
*
* @param {*} client
* @param {*} block
* @returns
* @memberof SyncServices
*/
async processBlockEvent(client, block) {
const network_id = client.getNetworkId();
// Get the first transaction
const first_tx = block.data.data[0];
// The 'header' object contains metadata of the transaction
const header = first_tx.payload.header;
const channel_name = header.channel_header.channel_id;
const blockPro_key = `${channel_name}_${block.header.number.toString()}`;
if (blocksInProcess.includes(blockPro_key)) {
return 'Block already in processing';
}
blocksInProcess.push(blockPro_key);
logger.debug('New Block >>>>>>> %j', block.header.number);
let channel_genesis_hash = client.getChannelGenHash(channel_name);
// Checking block is channel CONFIG block
/* eslint-disable */
if (!channel_genesis_hash) {
// get discovery and insert channel details to db and create new channel object in client context
setTimeout(
async (cli, chName, blk) => {
await cli.initializeNewChannel(chName);
channel_genesis_hash = cli.getChannelGenHash(chName);
// inserting new channel details to DB
await this.insertNewChannel(cli, chName, blk, channel_genesis_hash);
await this.insertFromDiscoveryResults(cli, chName, channel_genesis_hash);
const notify = {
notify_type: fabric_const.NOTITY_TYPE_NEWCHANNEL,
network_id,
channel_name: chName
};
this.platform.send(notify);
},
10000,
client,
channel_name,
block
);
} else if (
header.channel_header.typeString === fabric_const.BLOCK_TYPE_CONFIG
) {
setTimeout(
async (cli, chName, chGenHash) => {
// get discovery and insert new peer, orders details to db
await cli.initializeChannelFromDiscover(chName);
await this.insertFromDiscoveryResults(cli, chName, chGenHash);
const notify = {
notify_type: fabric_const.NOTITY_TYPE_UPDATECHANNEL,
network_id,
channel_name: chName
};
this.platform.send(notify);
},
10000,
client,
channel_name,
channel_genesis_hash
);
}
/* eslint-enable */
const createdt = await FabricUtils.getBlockTimeStamp(
header.channel_header.timestamp
);
const blockhash = await FabricUtils.generateBlockHash(block.header);
if (channel_genesis_hash) {
const block_row = {
blocknum: block.header.number.toString(),
datahash: block.header.data_hash.toString('hex'),
prehash: block.header.previous_hash.toString('hex'),
txcount: block.data.data.length,
createdt,
prev_blockhash: '',
blockhash,
channel_genesis_hash,
blksize: jsonObjSize(block)
};
const txLen = block.data.data.length;
for (let txIndex = 0; txIndex < txLen; txIndex++) {
const txObj = block.data.data[txIndex];
const txid = txObj.payload.header.channel_header.tx_id;
let validation_code = '';
let endorser_signature = '';
let payload_proposal_hash = '';
let endorser_id_bytes = '';
let chaincode_proposal_input = '';
let chaincode = '';
let rwset;
let readSet;
let writeSet;
let chaincodeID;
let status;
let mspId = [];
this.convertFormatOfValue(
'value',
client.fabricGateway.fabricConfig.getRWSetEncoding(),
txObj
);
if (txid && txid !== '') {
const validation_codes =
block.metadata.metadata[
fabprotos.common.BlockMetadataIndex.TRANSACTIONS_FILTER
];
const val_code = validation_codes[txIndex];
validation_code = convertValidationCode(val_code);
}
let envelope_signature = txObj.signature;
if (envelope_signature !== undefined) {
envelope_signature = convertHex.bytesToHex(envelope_signature);
}
let payload_extension = txObj.payload.header.channel_header.extension;
if (payload_extension !== undefined) {
payload_extension = convertHex.bytesToHex(payload_extension);
}
let creator_nonce = txObj.payload.header.signature_header.nonce;
if (creator_nonce !== undefined) {
creator_nonce = convertHex.bytesToHex(creator_nonce);
}
/* eslint-disable */
const creator_id_bytes =
txObj.payload.header.signature_header.creator.id_bytes;
if (txObj.payload.data.actions !== undefined) {
chaincode =
txObj.payload.data.actions[0].payload.action.proposal_response_payload
.extension.chaincode_id.name;
chaincodeID = new Uint8Array(
txObj.payload.data.actions[0].payload.action.proposal_response_payload.extension
);
status =
txObj.payload.data.actions[0].payload.action.proposal_response_payload
.extension.response.status;
mspId = txObj.payload.data.actions[0].payload.action.endorsements.map(
endorsement => endorsement.endorser.mspid
);
rwset =
txObj.payload.data.actions[0].payload.action.proposal_response_payload
.extension.results.ns_rwset;
readSet = rwset.map(rw => ({
chaincode: rw.namespace,
set: rw.rwset.reads
}));
writeSet = rwset.map(rw => ({
chaincode: rw.namespace,
set: rw.rwset.writes
}));
chaincode_proposal_input =
txObj.payload.data.actions[0].payload.chaincode_proposal_payload.input
.chaincode_spec.input.args;
if (chaincode_proposal_input !== undefined) {
let inputs = '';
for (const input of chaincode_proposal_input) {
inputs =
(inputs === '' ? inputs : `${inputs},`) + convertHex.bytesToHex(input);
}
chaincode_proposal_input = inputs;
}
endorser_signature =
txObj.payload.data.actions[0].payload.action.endorsements[0].signature;
if (endorser_signature !== undefined) {
endorser_signature = convertHex.bytesToHex(endorser_signature);
}
payload_proposal_hash = txObj.payload.data.actions[0].payload.action.proposal_response_payload.proposal_hash.toString(
'hex'
);
endorser_id_bytes =
txObj.payload.data.actions[0].payload.action.endorsements[0].endorser
.IdBytes;
}
const read_set = JSON.stringify(readSet, null, 2);
const write_set = JSON.stringify(writeSet, null, 2);
const chaincode_id = String.fromCharCode.apply(null, chaincodeID);
// checking new chaincode is deployed
if (
header.channel_header.typeString ===
fabric_const.BLOCK_TYPE_ENDORSER_TRANSACTION &&
chaincode === fabric_const.CHAINCODE_LSCC
) {
setTimeout(
async (cli, chName, chGenHash) => {
// get discovery and insert chaincode details to db
await this.insertFromDiscoveryResults(cli, chName, chGenHash);
const notify = {
notify_type: fabric_const.NOTITY_TYPE_CHAINCODE,
network_id,
channel_name: chName
};
this.platform.send(notify);
},
10000,
client,
channel_name,
channel_genesis_hash
);
}
/* eslint-enable */
const transaction_row = {
blockid: block.header.number.toString(),
txhash: txObj.payload.header.channel_header.tx_id,
createdt: txObj.payload.header.channel_header.timestamp,
chaincodename: chaincode,
chaincode_id,
status,
creator_msp_id: txObj.payload.header.signature_header.creator.mspid,
endorser_msp_id: mspId,
type: txObj.payload.header.channel_header.typeString,
read_set,
write_set,
channel_genesis_hash,
validation_code,
envelope_signature,
payload_extension,
creator_nonce,
chaincode_proposal_input,
endorser_signature,
creator_id_bytes,
payload_proposal_hash,
endorser_id_bytes
};
// Insert transaction
const res = await this.persistence
.getCrudService()
.saveTransaction(network_id, transaction_row);
logger.debug('saveTransaction ', res);
}
// Insert block
logger.info('block_row.blocknum ', block_row.blocknum);
const successSaveBlock = await this.persistence
.getCrudService()
.saveBlock(network_id, block_row);
logger.debug('result of SaveBlock ', successSaveBlock);
if (successSaveBlock) {
// Push last block
const notify = {
notify_type: fabric_const.NOTITY_TYPE_BLOCK,
network_id,
channel_name,
title: `Block ${block.header.number.toString()} added to Channel: ${channel_name}`,
type: 'block',
message: `Block ${block.header.number.toString()} established with ${
block.data.data.length
} tx`,
time: createdt,
txcount: block.data.data.length,
datahash: block.header.data_hash.toString('hex'),
blksize: block_row.blksize
};
this.platform.send(notify);
}
} else {
logger.error('Failed to process the block %j', block);
}
const index = blocksInProcess.indexOf(blockPro_key);
blocksInProcess.splice(index, 1);
}
convertFormatOfValue(prop, encoding, obj) {
if (Array.isArray(obj)) {
for (let idx = 0; idx < obj.length; idx++) {
this.convertFormatOfValue(prop, encoding, obj[idx]);
}
} else if (!Buffer.isBuffer(obj) && typeof obj === 'object') {
// Each element of array of Buffer is excluded by the 1st condition
Object.keys(obj).forEach(key => {
if (key === prop && Buffer.isBuffer(obj[key])) {
obj[key] = obj[key].toString(encoding);
} else if (obj[key]) {
this.convertFormatOfValue(prop, encoding, obj[key]);
}
});
}
}
/**
*
*
*
* @returns
* @memberof SyncServices
*/
getPlatform() {
return this.platform;
}
/**
*
*
* @returns
* @memberof SyncServices
*/
getPersistence() {
return this.persistence;
}
}
// Transaction validation code
function convertValidationCode(code) {
if (typeof code === 'string') {
return code;
}
return _validation_codes[code];
}
// Calculate data size of json object
function jsonObjSize(json) {
let bytes = 0;
function sizeOf(obj) {
if (obj !== null && obj !== undefined) {
switch (typeof obj) {
case 'number': {
bytes += 8;
break;
}
case 'string': {
bytes += obj.length;
break;
}
case 'boolean': {
bytes += 4;
break;
}
case 'object': {
const objClass = Object.prototype.toString.call(obj).slice(8, -1);
if (objClass === 'Object' || objClass === 'Array') {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
sizeOf(obj[key]);
}
} else {
bytes += obj.length;
}
break;
}
default:
logger.debug(typeof obj);
break;
}
}
return bytes;
}
function formatByteSize(rawByte) {
return (rawByte / 1024).toFixed(0);
}
return formatByteSize(sizeOf(json));
}