-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmain.js
537 lines (333 loc) · 14.7 KB
/
main.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
/**
*
* Created by shouhewu on 6/8/17.
*
*/
var express = require("express");
var path = require('path');
var app = express();
var http= require('http').Server(app);
var bodyParser = require('body-parser');
require('./socket/websocketserver.js').init(http)
var timer=require('./timer/timer.js')
timer.start()
var query=require('./app/query.js');
var ledgerMgr=require('./utils/ledgerMgr.js')
var statusMertics=require('./service/metricservice.js')
var channelsRouter=require('./router/channels.js');
var keyset=require('./service/keysetService.js');
var bcexplorerservice = require('./service/bcexplorerservice');
app.use(express.static(path.join(__dirname,'explorer_client')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/apis',channelsRouter)
var sql=require('./db/mysqlservice.js')
var config = require('./config.json');
var host = process.env.HOST || config.host;
var port = process.env.PORT || config.port;
// ======================= controller ===================
app.post("/api/tx/getinfo", async function(req, res) {
let txid = req.body.txid
if( txid != '0' ){
let fabricservice = bcexplorerservice.getCurrOrgFabricservice();
let curr_channel = ledgerMgr.getCurrChannel();
let channelpeermap = ledgerMgr.getcurrchannelpeerma();
let peer = channelpeermap[curr_channel];
//let peerRequest = bcexplorerservice.getPeerRequest(peer['requests']);
//peer['requests'] = bcexplorerservice.getPeerRequest(peer['requests']);
let response_payloads = await fabricservice.getTransaction(curr_channel , bcexplorerservice.getPeerRequestInfo(peer) ,txid);
var header = response_payloads['transactionEnvelope']['payload']['header']
var data = response_payloads['transactionEnvelope']['payload']['data']
var signature = response_payloads['transactionEnvelope']['signature'].toString("hex")
let otherorgs = bcexplorerservice.getOtherOrg();
res.send({
'tx_id':header.channel_header.tx_id,
'timestamp':header.channel_header.timestamp,
'channel_id':header.channel_header.channel_id,
'type':header.channel_header.type,
})
/*query.getTransactionByID('peer1',ledgerMgr.getCurrChannel(),txid,'admin','org1').then(response_payloads=>{
var header = response_payloads['transactionEnvelope']['payload']['header']
var data = response_payloads['transactionEnvelope']['payload']['data']
var signature = response_payloads['transactionEnvelope']['signature'].toString("hex")
res.send({
'tx_id':header.channel_header.tx_id,
'timestamp':header.channel_header.timestamp,
'channel_id':header.channel_header.channel_id,
'type':header.channel_header.type,
})
})*/
}else{
res.send({ })
}
});
app.post("/api/tx/json", async function(req, res) {
let txid = req.body.number
if( txid != '0' ){
let fabricservice = bcexplorerservice.getCurrOrgFabricservice();
let curr_channel = ledgerMgr.getCurrChannel();
let channelpeermap = ledgerMgr.getcurrchannelpeerma();
let peer = channelpeermap[curr_channel];
let peerRequest = bcexplorerservice.getPeerRequest(peer['requests']);
//peer['requests'] = bcexplorerservice.getPeerRequest(peer['requests']);
let response_payloads = await fabricservice.getTransaction( curr_channel , bcexplorerservice.getPeerRequestInfo(peer) , txid );
var header = response_payloads['transactionEnvelope']['payload']['header'];
var data = response_payloads['transactionEnvelope']['payload']['data'];
var signature = response_payloads['transactionEnvelope']['signature'].toString("hex");
var blockjsonstr = JSON.stringify(response_payloads['transactionEnvelope']);
res.send(blockjsonstr);
/*query.getTransactionByID('peer1',ledgerMgr.getCurrChannel(),txid,'admin','org1').then(response_payloads=>{
var header = response_payloads['transactionEnvelope']['payload']['header']
var data = response_payloads['transactionEnvelope']['payload']['data']
var signature = response_payloads['transactionEnvelope']['signature'].toString("hex")
var blockjsonstr = JSON.stringify(response_payloads['transactionEnvelope'])
res.send(blockjsonstr)
})
*/
}else{
res.send({ })
}
});
app.post("/api/block/json", async function(req, res) {
let number=req.body.number
let fabricservice = bcexplorerservice.getCurrOrgFabricservice();
let curr_channel = ledgerMgr.getCurrChannel();
let channelpeermap = ledgerMgr.getcurrchannelpeerma();
let peer = channelpeermap[curr_channel];
let peerRequest = bcexplorerservice.getPeerRequest(peer['requests']);
//peer['requests'] = bcexplorerservice.getPeerRequest(peer['requests']);
//let response_payloads = await fabricservice.getTransaction(curr_channel , peerRequest ,txid);
let blockinfo = await fabricservice.getblockInfobyNum( curr_channel , bcexplorerservice.getPeerRequestInfo(peer) , parseInt(number) );
var blockjsonstr = JSON.stringify(blockinfo);
res.send(blockjsonstr);
/*query.getBlockByNumber('peer1',ledgerMgr.getCurrChannel(),parseInt(number),'admin','org1').then(block=>{
var blockjsonstr = JSON.stringify(block)
res.send(blockjsonstr)
})*/
});
app.post("/api/block/getinfo", async function(req, res) {
let number=req.body.number
let fabricservice = bcexplorerservice.getCurrOrgFabricservice();
let curr_channel = ledgerMgr.getCurrChannel();
let channelpeermap = ledgerMgr.getcurrchannelpeerma();
let peer = channelpeermap[curr_channel];
let peerRequest = bcexplorerservice.getPeerRequest(peer['requests']);
//let response_payloads = await fabricservice.getTransaction(curr_channel , peerRequest ,txid);
//peer['requests'] = bcexplorerservice.getPeerRequest(peer['requests']);
let blockinfo = await fabricservice.getblockInfobyNum( curr_channel , bcexplorerservice.getPeerRequestInfo(peer) , parseInt(number) );
let blockjsonstr = JSON.stringify(blockinfo);
let low = blockinfo['header']['number']['low'];
/*let previous_hash = blockinfo['header']['previous_hash'];
let data_hash = blockinfo['header']['data_hash'];
let transactions = blockinfo['header']['number']['low'];
*/
res.send({
'number':low,
'previous_hash':blockinfo['header']['previous_hash'],
'data_hash':blockinfo['header']['data_hash'],
'transactions':blockinfo['data']['data']
})
/*let number=req.body.number
query.getBlockByNumber('peer1'']',ledgerMgr.getCurrChannel(),parseInt(number),'admin','org1').then(block=>{
res.send({
'number':block.header.number.toString(),
'previous_hash':block.header.previous_hash,
'data_hash':block.header.data_hash,
'transactions':block.data.data
})
})*/
});
/*app.post("/api/block/get", function(req, res) {
let number=req.body.number
query.getBlockByNumber('peer1',ledgerMgr.getCurrChannel(),parseInt(number),'admin','org1').then(block=>{
res.send({
'number':number,
'txCount':block.data.data.length
})
})
});*/
app.post("/api/block/get", function(req, res) {
let number=req.body.number
sql.getRowByPkOne(`select blocknum ,txcount from blocks where channelname='${ledgerMgr.getCurrChannel()}' and blocknum='${number}'`).then(row=>{
if(row){
res.send({
'number':row.blocknum,
'txCount':row.txcount
})
}
})
});
//return latest status
app.post("/api/status/get", function(req, res) {
let sectionName=ledgerMgr.currSection();
if (sectionName=='channel'){
statusMertics.getStatus(ledgerMgr.getCurrChannel(),function(status){
res.send(status)
})
} else if(sectionName=='org'){
bcexplorerservice.getOrgStatus().then(status=>{
res.send(status)
});
} else if(sectionName=='peer'){
bcexplorerservice.getPeerStatus().then(status=>{
res.send(status)
});
}
});
app.post('/chaincodelist',function(req,res){
statusMertics.getTxPerChaincode(ledgerMgr.getCurrChannel(),function (data) {
res.send(data)
})
})
app.post('/changeChannel',function(req,res){
let channelName=req.body.channelName
ledgerMgr.changeChannel(channelName)
res.send({'a':ledgerMgr.getCurrChannel()})
})
app.post('/curChannel',function(req,res){
res.send({'currentChannel':ledgerMgr.getCurrChannel()})
})
app.post('/channellist',function(req,res){
ledgerMgr.getChannellist().then(channelList=>{
res.send({'channelList':channelList});
}).catch(err=>{
res.send({'channelList':[ledgerMgr.getCurrChannel()]});
})
})
app.post('/curPeer',function(req,res){
res.send({'currentPeer':ledgerMgr.getCurrpeer()['name']})
})
app.post('/peerselectlist',function(req,res){
let peerlist=bcexplorerservice.getCurrOrgPeers();
res.send({'peerlist':peerlist});
})
app.post('/changePeer',function(req,res){
let peerName=req.body.peerName
let currorg = ledgerMgr.getCurrOrg();
let peer = bcexplorerservice.getPeer(ledgerMgr.getCurrOrg(),peerName);
ledgerMgr.changeCurrPeer(peer)
res.send({'a':ledgerMgr.getCurrpeer()})
})
app.post('/showSection',function(req,res){
let sectionName=req.body.sectionName;
ledgerMgr.changeSection(sectionName);
res.send({'a':ledgerMgr.currSection()});
})
app.post('/getKeyset',function(req,res){
keyset.getKeyset().then(rows=>{
res.send(rows);
})
})
app.post('/channellist4peer', async function(req,res){
let currp = ledgerMgr.getCurrpeer();
let peername = currp['name'];
let searchSql = `select * from channel where channelname in ( select channelname from peer_ref_channel where peer_name = '${peername}' ) `;
let chaincodesmap = await sql.getSQL2Map( ` select channelname , count(distinct(name)) as nums from chaincodes where peer_name = '${peername}' group by channelname `,'channelname');
let channeltransmap = await sql.getSQL2Map( ` select channelname , count(*) as nums from transaction group by channelname `,'channelname');
let keysetmap = await sql.getSQL2Map( ` select channelname , count(*) as nums from keyset group by channelname `,'channelname');
let rows = await sql.getRowsBySQlNoCondtion(searchSql);
for( let ind = 0 ; ind < rows.length ; ind++ ){
let cc = rows[ind];
let channelname = cc['channelname'];
cc['ccnums'] = chaincodesmap.get(channelname)['nums']
cc['tranmums'] = channeltransmap.get(channelname)['nums']
cc['keynums'] = keysetmap.get(channelname)['nums']
}
res.send(rows);
})
app.post('/chaincodelist4peer',async function(req,res){
let currp = ledgerMgr.getCurrpeer();
let peername = currp['name'];
let searchsql = ` select * from chaincodes where peer_name in ( select peer_name from peer_ref_channel where peer_name = '${peername}' ) order by ccstatus desc `;
let chaincodelist = await sql.getRowsBySQlNoCondtion( searchsql );
for( let ind = 0 ; ind < chaincodelist.length ; ind++ ){
let cc = chaincodelist[ind];
if( cc['ccstatus'] == 0 )
cc['ccstatus_commit'] = 'Install';
else
cc['ccstatus_commit'] = 'Instantiated';
}
res.send(chaincodelist);
})
app.post('/peerlist', async function(req,res){
let curr_channel = ledgerMgr.getCurrChannel();
let curr_channel_peermap = ledgerMgr.getCurrchannelpeersmap()[curr_channel];
let curr_channel_peers = [];
for (let key in curr_channel_peermap) {
let peer = curr_channel_peermap[key];
curr_channel_peers.push(peer);
}
/* let orgs = await bcexplorerservice.getOrgStatus();
let peerstatus = await bcexplorerservice.getPeerStatus();
*/
//let currp = ledgerMgr.getCurrpeer();
let currpeerjoinchannel = await bcexplorerservice.getCurrPeerJoinChannels();
let currpeerconCc = await bcexplorerservice.getCurrPeerContaitCc();
res.send(curr_channel_peers);
/*keyset.getKeyset().then(rows=>{
console.info(JSON.stringify(rows))
res.send(rows);
})*/
})
app.post('/network',function(req,res){
let curr_channel = ledgerMgr.getCurrChannel();
let curr_org = ledgerMgr.getCurrOrg();
let curr_peers = bcexplorerservice.getPeers4Org(curr_org);
let curr_orderer = bcexplorerservice.orderers[0];
let other_org = bcexplorerservice.getOtherOrg();
let currorgobj = bcexplorerservice.ORGNAMEMAP[curr_org];
let currmspid = currorgobj['mspid'];
/*{id: 1, label: 'CA', font:{size:30}, shape: 'circle'},
{id: 2, label: 'Orderer' , font:{size:30}, shape: 'ellipse' },
{id: 3, label: 'Org1Msp' ,font:{size:30}, shape: 'ellipse' },*/
let nodearr = [];
let ledag = [];
let ordererdata = {id: 1, label: 'Orderer', font:{size:30}, shape: 'ellipse'};
let currmsp = {id: 2, label: `${currmspid}`, font:{size:30}, shape: 'box'};
nodearr.push(ordererdata);
nodearr.push(currmsp);
let ind = 3;
let peerind = 3;
for( let key in other_org ){
if( key == currmspid )
continue;
let orgmsp = other_org[key];
let temp = {id: peerind , label: `${key}` , font:{size:30}, shape: 'box'};
nodearr.push(temp);
peerind ++;
}
for( let index2 = peerind ; index2< curr_peers.length+peerind ; index2++ ){
let peertemp = curr_peers[index2-peerind];
let temp1 = {id: index2 , label: `${peertemp['name']}` , font:{size:30,color:'white'}, shape: 'database',color:'DarkViolet'};
nodearr.push(temp1);
}
ledag.push({from: 2, to: 1, arrows:'to'});
for ( let index5 = ind-1 ; index5<peerind-1;index5++ ){
let nodetemp = nodearr[index5];
let nodeid = nodetemp['id'];
ledag.push({from: nodeid , to: 1, arrows:'to'});
}
for( let index6 = peerind-1 ; index6<nodearr.length ; index6++){
let nodetemp = nodearr[index6];
let nodeid = nodetemp['id'];
ledag.push({from: nodeid , to: 2 , arrows:'to'});
}
let result = {
"nodearr":nodearr, "edgesarr":ledag
}
//{from: 3, to: 2, arrows:'to'},
res.send(result);
/*keyset.getKeyset().then(rows=>{
console.info(JSON.stringify(rows))
res.send(rows);
})*/
})
// ============= start server =======================
var server = http.listen(port, function() {
console.log(`Please open Internet explorer to access :http://${host}:${port}/`);
});
//注册异常处理器
process.on('unhandledRejection', function (err) {
console.error(err.stack);
});
process.on(`uncaughtException`, console.error);