-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchainManager.js
631 lines (584 loc) · 25.3 KB
/
blockchainManager.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
'use strict';
// ______ __ ______ ______ __ __ ______ __ __ ______ __ __ __
// /\ == \/\ \ /\ __ \/\ ___\/\ \/ / /\ ___\/\ \_\ \/\ __ \/\ \/\ "-.\ \
// \ \ __<\ \ \___\ \ \/\ \ \ \___\ \ _"-\ \ \___\ \ __ \ \ __ \ \ \ \ \-. \
// \ \_____\ \_____\ \_____\ \_____\ \_\ \_\ \_____\ \_\ \_\ \_\ \_\ \_\ \_\\"\_\
// \/_____/\/_____/\/_____/\/_____/\/_/\/_/\/_____/\/_/\/_/\/_/\/_/\/_/\/_/ \/_/
// __ __ ______ __ __ ______ ______ ______ ______
// /\ "-./ \/\ __ \/\ "-.\ \/\ __ \/\ ___\/\ ___\/\ == \
// \ \ \-./\ \ \ __ \ \ \-. \ \ __ \ \ \__ \ \ __\\ \ __<
// \ \_\ \ \_\ \_\ \_\ \_\\"\_\ \_\ \_\ \_____\ \_____\ \_\ \_\
// \/_/ \/_/\/_/\/_/\/_/ \/_/\/_/\/_/\/_____/\/_____/\/_/ /_/
// ------- Basic Libraries for this package -------
const Table = require('cli-table');
const prettyoutput = require('prettyoutput');
var chalk = require('chalk');
var figlet = require('figlet');
var now = require('performance-now');
const ora = require('ora');
var crypto = require('crypto');
// ------- Basic Libraries for this persistance modules -------
const mongo = require('./mongoManager');
// ------- Basic Libraries for this Hyperledger Fabric -------
var hfc = require('fabric-client');
require('./config.js');
var helper = require('./app/helper.js');
var channels = require('./app/create-channel.js');
var join = require('./app/join-channel.js');
var install = require('./app/install-chaincode.js');
var instantiate = require('./app/instantiate-chaincode.js');
var invoke = require('./app/invoke-transaction.js');
var query = require('./app/query.js');
var host = process.env.HOST || hfc.getConfigSetting('host');
var port = process.env.PORT || hfc.getConfigSetting('port');
class BlockchainManager {
/**@name profilingTime
* @author Aabo Technologies © 2017 - Server's team
* @description Profiles the time that a process took and shows some statistics
* @param {Number} timeStart when the process started
* @param {Number} timeEnd when the process ended
* @param {Number} simTrax is the amount of transactions that were executed
* @param {String} whereFrom where the method is called from
* @returns {Number} executedTime is the overall execution time
* @returns {Number} opTime is the time it took for every operation to run
* @returns {Number} opsPerDay is the amount of operations that could be done in one day
*/
static profilingTime(timeStart, timeEnd, simTrax, whereFrom) {
const METHOD = 'profilingTime';
let executedTime = (timeEnd - timeStart);
let opTime = (executedTime / simTrax);
let opsPerDay = (86400000 / opTime).toFixed(0);
let exp = (opsPerDay.toString().length) - 1;
console.log(chalk.bold.yellow('EXECUTION TIME:'), chalk.white(executedTime), 'ms');
console.log(chalk.bold.yellow('OPERATION TIME:'), chalk.white(opTime), 'ms');
console.log(chalk.bold.yellow('OPS PER DAY:'), chalk.white(opsPerDay), 'or:', chalk.bold.yellow('1E' + exp));
if (whereFrom === 'tx') {
if (exp < 9) {
console.log(
chalk.yellow(
figlet.textSync('Not yet...', {
horizontalLayout: 'full',
verticalLayout: 'default'
})
)
);
} else {
console.log(
chalk.green(
figlet.textSync('DONE~!', {
horizontalLayout: 'full',
verticalLayout: 'default'
})
)
);
}
}
}
/**
* @name transactionCannon
* @author Aabo Technologies © 2017 - Server's team
* @description Launches the 'transferFunds' method multiple times using promises
* @param {Array} peer, is an array of peers to execute the transaction
* @param {String} chaincodeName, is the name of the chaincode to target
* @param {String} channelName, is the name of the channel to target
* @param {String} username, is the username to sign the transaction
* @param {String} orgName, is the name of the organization
*/
static transactionCannon(peer, chaincodeName, channelName, amount, username, orgName) {
let start;
let end;
return this.createSchedule(peer, chaincodeName, channelName, amount, username, orgName)
.then((schedule) => {
let cannonBalls = [];
for (let i = 0; i < schedule.length; i++) {
let args = [];
args.push(schedule[i].from);
args.push(schedule[i].to);
args.push(schedule[i].funds.toString());
cannonBalls.push(this.invokeTransactions(peer, chaincodeName, chaincodeName, 'transferFunds', args, username, orgName));
}
start = now();
return Promise.all(cannonBalls);
})
.then(() => {
end = now();
this.profilingTime(start, end, amount, 'tx');
return this.getWalletByRange(peer, chaincodeName, channelName, '', '', username, orgName);
})
.then((ledger) => {
return this.syncChecker(ledger);
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name syncLedger
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} ledger, an array of accounts taken from the ledger
*/
static syncChecker(ledger) {
let wallet;
return mongo.getAllAst()
.then((result) => {
wallet = result;
return mongo.getAllTx();
})
.then((transactions) => {
for (let i = 0; i < transactions.length; i++) {
for (let x = 0; x < wallet.length; x++) {
if (wallet[x].id == transactions[i].from) {
wallet[x].balance = +wallet[x].balance - +transactions[i].funds;
}
if (wallet[x].id == transactions[i].to) {
wallet[x].balance = +wallet[x].balance + +transactions[i].funds;
}
}
}
})
.then(() => {
let err = [];
for (let i = 0; i < ledger.length; i++) {
for (let x = 0; x < wallet.length; x++) {
if (ledger[i].Key == wallet[x].id && ledger[i].Record.balance != wallet[x].balance) {
err.push(ledger[i].Record.address);
}
}
}
if (err.length != 0) {
console.log(chalk.red.bold('The ledger is not synced'));
console.log(chalk.yellow.bold('==== ADDRESSES NOT SYNCED ===='));
for (let i = 0; i < err.length; i++) {
console.log(err[i]);
}
console.log(chalk.yellow.bold('==== ADDRESSES NOT SYNCED ===='));
} else {
console.log(chalk.green.bold('The ledger is synced'));
}
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name createSchedule
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , an array of peers where to execute the transaction
* @param {String} chaincodeName , the name of the chaincode to invoke
* @param {String} channelName , the name of the channel to target
* @param {Number} amount , the amount of transactions to schedule
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static createSchedule(peer, chaincodeName, channelName, amount, username, orgName) {
return this.getWalletByRange(peer, chaincodeName, channelName, '', '', username, orgName)
.then((result) => {
let schedule = [];
for (let i = 0; i < amount; i++) {
var tran = {
from: result[2 * (i)].Key,
to: result[(2 * (i)) + 1].Key,
funds: Math.floor(Math.random() * (1000 - 10) + 10)
};
mongo.saveTx(tran);
schedule.push(tran);
}
return schedule;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name createWallets
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , an array of peers where to execute the transaction
* @param {String} chaincodeName , the name of the chaincode to invoke
* @param {String} channelName , the name of the channel to target
* @param {Number} amount , amount of wallets to create on the ledger
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static createWallets(peer, chaincodeName, channelName, amount, username, orgName) {
let start;
let end;
let all_promise = [];
//Create the wallets
for (let i = 0; i < amount; i++) {
let args = [];
args.push(crypto.randomBytes(20).toString('hex'));
args.push((i * 1000).toString());
mongo.saveAst(args);
all_promise.push(this.invokeTransactions(peer, chaincodeName, channelName, 'initWallet', args, username, orgName));
}
//Launch the accounts
start = now();
return Promise.all(all_promise)
.then(() => {
end = now();
this.profilingTime(start, end, amount, 'acc');
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getWallet
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , an array of peers where to execute the transaction
* @param {String} chaincodeName , the name of the chaincode to invoke
* @param {String} channelName , the name of the channel to target
* @param {String} id , the wallet address to target
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getWallet(peer, chaincodeName, channelName, id, username, orgName) {
var args = [];
args.push(id);
return this.queryChaincode(peer, chaincodeName, channelName, 'readWallet', args, username, orgName)
.then((result) => {
return result;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getWalletByRange
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , an array of peers where to execute the transaction
* @param {String} chaincodeName , the name of the chaincode to invoke
* @param {String} channelName , the name of the channel to target
* @param {String} start , the lower bound to get addresses from
* @param {String} end , the upper bound to get addresses from
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getWalletByRange(peer, chaincodeName, channelName, start, end, username, orgName) {
var args = [];
args.push(start);
args.push(end);
return this.queryChaincode(peer, chaincodeName, channelName, 'getWalletsByRange', args, username, orgName)
.then((result) => {
return result;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name transfer
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , an array of peers where to execute the transaction
* @param {String} chaincodeName , the name of the chaincode to invoke
* @param {String} channelName , the name of the channel to target
* @param {String} from , the address of the wallet that is sending money
* @param {String} to , the address of the wallet that is receiving money
* @param {Number} balance , the amount of money that is being transfered
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static transfer(peer, chaincodeName, channelName, from, to, balance, username, orgName) {
let start;
let end;
var args = [];
args.push(from);
args.push(to);
args.push(balance.toString());
var tran = {
from: from,
to: to,
funds: balance
};
mongo.saveTx(tran);
start = now();
return this.invokeTransactions(peer, chaincodeName,
channelName, 'transferFunds', args, username, orgName)
.then((message) => {
end = now();
this.profilingTime(start, end, 1, 'tx');
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
})
}
/**
* @name enrollUser
* @author Aabo Technologies © 2017 - Server's division
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static enrollUser(username, orgName) {
return helper.getRegisteredUsers(username, orgName, true)
.then((response) => {
return response;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name createChannel
* @author Aabo Technologies © 2017 - Server's division
* @param {String} channelName , the name of the channel to enroll
* @param {String} channelConfigPath , the path of configuration
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static createChannel(channelName, channelConfigPath, username, orgName) {
return channels.createChannel(channelName, channelConfigPath, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name joinChannel
* @author Aabo Technologies © 2017 - Server's division
* @param {String} channelName , name of the channel to join
* @param {Array} peers , array of peers to join
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static joinChannel(channelName, peers, username, orgName) {
return join.joinChannel(channelName, peers, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
})
}
/**
* @name installChaincode
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peers , array of peers to install a Chaincode
* @param {String} chaincodeName , name of the chaincode to install
* @param {Path} chaincodePath , file path
* @param {String} chaincodeVersion , version of the chaincode
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static installChaincode(peers, chaincodeName, chaincodePath, chaincodeVersion, username, orgName) {
return install.installChaincode(peers, chaincodeName, chaincodePath, chaincodeVersion, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name instantiateChaincode
* @author Aabo Technologies © 2017 - Server's division
* @param {String} channelName , name of the channel
* @param {String} chaincodeName , name of the chaincode
* @param {String} chaincodeVersion , chaincode version
* @param {String} fcn , function to invoke
* @param {Array} args , arguments to pass
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static instantiateChaincode(channelName, chaincodeName, chaincodeVersion, fcn, args, username, orgName) {
return instantiate.instantiateChaincode(channelName, chaincodeName, chaincodeVersion, fcn, args, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name invokeTransactions
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peers , array of peers to invoke the transaction
* @param {String} chaincodeName , name of the chaincode
* @param {String} channelName , name of the channel
* @param {String} fcn , function to invoke
* @param {Array} args , arguments to pass
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static invokeTransactions(peers, chaincodeName, channelName, fcn, args, username, orgName) {
return invoke.invokeChaincode(peers, channelName, chaincodeName, fcn, args, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name queryChaincode
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , array of peers to query
* @param {String} chaincodeName , name of the chaincode
* @param {String} channelName , name of the channel
* @param {String} fcn , function to query
* @param {Array} args , arguments
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static queryChaincode(peer, chaincodeName, channelName, fcn, args, username, orgName) {
return query.queryChaincode(peer, channelName, chaincodeName, args, fcn, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getBlockByBlockNumber
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , peers to query the block
* @param {String} blockId , Block id
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getBlockByBlockNumber(peer, blockId, username, orgName) {
return query.getBlockByNumber(peer, blockId, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getTransactionByTransactionID
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , array of peers to get the transaction
* @param {String} txId , transaction ID
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getTransactionByTransactionID(peer, txId, username, orgName) {
return query.getTransactionByID(peer, txId, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getBlockByHash
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , array of peers to query
* @param {String} hash , hash of the block
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getBlockByHash(peer, hash, username, orgName) {
return query.getBlockByHash(peer, hash, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getChannelInfo
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , array of peers to check
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getChannelInfo(peer, username, orgName) {
return query.getChainInfo(peer, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getInstalledChaincodes
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , array of peers
* @param {String} installType , type of installation
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getInstalledChaincodes(peer, installType, username, orgName) {
return query.getInstalledChaincodes(peer, installType, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getChannels
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer, peers to query
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getChannels(peer, username, orgName) {
return query.getChannels(peer, username, orgName)
.then((message) => {
return message;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name walletRegistration
* @author Aabo Technologies © 2017 - Server's division
* @param {*} peer , array of
* @param {*} chaincodeName
* @param {*} channelName
* @param {*} id
* @param {*} balance
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static walletRegistration(peer, chaincodeName, channelName, id, balance, username, orgName) {
let start;
let end;
let args = [];
args.push(id);
args.push(balance.toString());
mongo.saveAst(args);
start = now();
return this.invokeTransactions(peer, chaincodeName, channelName, 'initWallet', args, username, orgName)
.then(() => {
end = now();
this.profilingTime(start, end, 1, 'acc');
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @name getWalletHistory
* @author Aabo Technologies © 2017 - Server's division
* @param {Array} peer , array of peers to query
* @param {String} chaincodeName , name of the chaincode
* @param {String} channelName , name of the channel
* @param {String} id , the wallet's address
* @param {String} username , the user that will sign transactions
* @param {String} orgName , name of the organization to target
*/
static getWalletHistory(peer, chaincodeName, channelName, id, username, orgName) {
var args = [];
args.push(id);
return this.queryChaincode(peer, chaincodeName, channelName, 'getHistoryForWallet', id, username, orgName)
.then((result) => {
return result;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
}
module.exports = BlockchainManager;