-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·621 lines (576 loc) · 19.5 KB
/
index.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
#!/usr/bin/env node
//CLI Elements and Libraries
var chalk = require('chalk');
var clear = require('clear');
var figlet = require('figlet');
var inquirer = require('inquirer');
var Table = require('cli-table');
//Hyperledger Fabric Code And Connectors
var hyper = require('./blockchainManager');
var jsond = require('./package');
var index = require('.');
// ___ ___
// ___ / /\ / /\
// /__/| / /::\ / /::\
// ___ ___ | |:| / /:/\:\ / /:/\:\
// /__/\ / /\ | |:| / /:/~/:/ / /:/~/::\
// \ \:\ / /:/ __|__|:| /__/:/ /:/___ /__/:/ /:/\:\
// \ \:\ /:/ /__/::::\ \ \:\/:::::/ \ \:\/:/__\/
// \ \:\/:/ ~\~~\:\ \ \::/~~~~ \ \::/
// \ \::/ \ \:\ \ \:\ \ \:\
// \__\/ \__\/ \ \:\ \ \:\
// \__\/ \__\/
var yargs = require('yargs')
.command('cli', 'Start the CLI Lyra Application')
.command('author', 'Show the projects authors')
.command('assets', 'Lists all registered assets in the Blockchain')
.command('participants', 'Lists all registered participants on the Blockchain')
.command('initialize', 'Initializes the Participants and Wallets on the network', {
amount: {
description: 'Amount of clients and wallets to make',
require: true,
alias: 'a'
},
top: {
description: 'Most amount of money a client can have',
require: true,
alias: 't'
},
bottom: {
description: 'Least amount of money a client can have',
require: true,
alias: 'b'
}
})
.command('cassets', 'Lists all current wallets on the Ledger')
.command('passets', 'Lists all current participants on the Ledger')
.command('transfer', 'Makes a single transaction over the network', {
from: {
description: "Address from a client who's sending money",
require: true,
alias: 'f'
},
to: {
description: "Address from a client who's receiving money",
require: true,
alias: 't'
},
amount: {
description: "Amount of money to be sent",
require: true,
alias: 'a'
}
})
.command('schedule', 'Make a Transaction Schedule', {
transactions: {
description: 'Amount of transactions to build the schedule',
require: true,
alias: 't'
}
})
.command('cannon', 'Launches a bunch of transactions as fast as possible', {
transactions: {
description: 'Amount of transactions to launch into the ledger',
require: true,
alias: 't'
},
replication: {
description : 'Replication of transactions into MongoDB',
require: false,
default : 'n',
alias :'r'
}
})
.command('ledger', 'Checks if ledger is synced', {
transactions: {
description: 'Amount of transactions to launch into the ledger',
require: true,
alias: 't'
}
})
.help()
.argv;
switch (yargs._[0]) {
case 'cli':
author();
mainMenu();
break;
case 'author':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
author();
process.exit(0);
break;
case 'assets':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.registeredAssets()
.then(() => {
process.exit(0);
})
.catch(() => {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case 'participants':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.registeredParticipants()
.then(() => {
process.exit(0);
})
.catch(() => {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case 'initialize':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.batchAccount(yargs.amount, yargs.bottom, yargs.top)
.then(() => {
process.exit(0);
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case 'cassets':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.assetsOnLedger()
.then(() => {
process.exit(0);
})
.catch(() => {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case 'passets':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.participantsOnLedger()
.then(() => {
process.exit(0);
})
.catch(() => {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case 'transfer':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.transfer(yargs.from, yargs.to, yargs.amount)
.then(() => {
process.exit(0);
})
.catch(() => {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
})
break;
case 'schedule':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.getTransactionSchedule(yargs.transactions)
.then((result) => {
let table = new Table({
head: ['From', 'To', 'Funds']
});
for (const key of Object.keys(result)) {
let tableLine = [];
tableLine.push(result[key].from);
tableLine.push(result[key].to);
tableLine.push(result[key].funds);
table.push(tableLine);
}
console.log(table.toString());
process.exit(0);
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case 'cannon':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.transactionCannon(yargs.transactions,yargs.replication)
.then((result) => {
return hyper.isLedgerStateCorrect(result);
})
.then((result) => {
process.exit(0);
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
})
break;
case 'ledger':
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
hyper.getTransactionSchedule(yargs.transactions)
.then((schedule) => {
return hyper.isLedgerStateCorrect(schedule);
})
.then((result) => {
process.exit(0);
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
default:
console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017'));
console.log(chalk.bold.red('No commands were issued from the terminal. Please launch it again with --help'));
process.exit(0);
break;
}
/**@description Shows current authors on the system */
function author() {
clear();
console.log(
chalk.cyan(
figlet.textSync('Lyra', {
horizontalLayout: 'full',
verticalLayout: 'default'
})
)
);
console.log(chalk.cyan.bold(jsond.version));
console.log(chalk.cyan.bold('Aabo Technologies © 2017'));
/** Displays the characteristics of the Program */
console.log("\n");
console.log(chalk.cyan.bold('Welcome to the fast Blockchain Simulator'));
/** Displays the authors of the Program */
console.log(chalk.magenta.bold('Made by:'));
console.log(chalk.green.bold('--Andres Bustamante Diaz'));
console.log(chalk.white.bold('--Enrique Navarro Torres-Arpi'));
console.log(chalk.magenta.bold('--Fernando Martin Garcia Del Angel'));
console.log(chalk.blue.bold('--Hector Carlos Flores Reynoso'));
console.log("\n");
}
/**@description Shows the menu with the main options
* @returns {Nothing}
*/
function mainMenu() {
/** Displays the main menu */
console.log(
chalk.green(
figlet.textSync('Main Menu', {
horizontalLayout: 'full',
verticalLayout: 'default'
})
)
);
var questions = [{
type: "list",
name: "initial",
message: "Choose an Option from the main menu",
choices: [
new inquirer.Separator(),
"Access Debug Menu",
"Start the testing mode",
new inquirer.Separator(),
"Exit"
]
}];
inquirer.prompt(questions).then(function (answers) {
switch (answers.initial) {
case "Access Debug Menu":
debugMenu();
break;
case "Start the testing mode":
testingMenu();
break;
case "Exit":
process.exit(0);
break;
}
});
}
/**@description Shows the debug menu with the debug options
* @returns {Nothing}
*/
function debugMenu() {
console.log(
chalk.yellow(
figlet.textSync('Debug Menu', {
horizontalLayout: 'full',
verticalLayout: 'default'
})
)
);
var questions = [{
type: "list",
name: "initial",
message: "Choose an option from the debug menu",
choices: [
new inquirer.Separator(),
"Shows the project's authors",
"Lists all registered assets in the Blockchain",
"Lists all registered participants on the Blockchain",
"Create a schedule for the transactions",
"Lists all current wallets on the Ledger",
"Lists all current participants on the Ledger",
"Make a single transaction over the network",
new inquirer.Separator(),
"Go back to the main menu"
]
}];
inquirer.prompt(questions).then(function (answers) {
switch (answers.initial) {
case "Create a schedule for the transactions":
makeSchedule();
break;
case "Shows the project's authors":
author();
console.log('\n');
debugMenu();
break;
case "Lists all registered assets in the Blockchain":
hyper.registeredAssets()
.then(() => {
debugMenu();
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case "Lists all registered participants on the Blockchain":
hyper.registeredParticipants()
.then(() => {
debugMenu();
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case "Lists all current wallets on the Ledger":
hyper.assetsOnLedger()
.then(() => {
debugMenu();
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case "Lists all current participants on the Ledger":
hyper.participantsOnLedger()
.then(() => {
debugMenu();
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
break;
case "Make a single transaction over the network":
var questions = [{
type: "input",
name: "from",
message: "Input the wallet address to send balance from"
},{
type: "input",
name: 'to',
message: "Input the wallet address to receive balance"
},{
type: 'input',
name: 'money',
message: 'Input the amount of money to send'
}];
inquirer.prompt(questions).then(function (answers) {
hyper.transfer(answers.from,answers.to,answers.money)
.then(()=>{
debugMenu();
})
.catch(function(error){
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
});
break;
case "Go back to the main menu":
clear();
mainMenu();
break;
}
});
}
/**@description Shows current testing options
* @version 1.0
*/
function testingMenu() {
clear();
console.log(
chalk.yellow(
figlet.textSync('Testing Menu', {
horizontalLayout: 'full',
verticalLayout: 'default'
})
)
);
var questions = [{
type: "list",
name: "initial",
message: "Choose an option from the debug menu",
choices: [
new inquirer.Separator(),
"Create batch accounts",
"Start the transaction cannon",
new inquirer.Separator(),
"Go back to the main menu"
]
}];
inquirer.prompt(questions).then(function (answers) {
switch (answers.initial) {
case 'Create batch accounts':
batchCreation('test');
break;
case 'Start the transaction cannon':
startTheCannon();
break;
case 'Go back to the main menu':
clear();
mainMenu();
break;
}
});
}
/**
* @description Makes a Sample schedule for testing purposes
* @returns {Table} table including the transactions made by the schedule
*/
function makeSchedule() {
var questions = [{
type: 'input',
name: 'transactions',
message: 'Amount of transactions to create the sample schedule',
default: 1,
}];
inquirer.prompt(questions).then(function (answers) {
hyper.getTransactionSchedule(answers.transactions)
.then((result) => {
let table = new Table({
head: ['From', 'To', 'Funds']
});
for (const key of Object.keys(result)) {
let tableLine = [];
tableLine.push(result[key].from);
tableLine.push(result[key].to);
tableLine.push(result[key].funds);
table.push(tableLine);
}
console.log(table.toString());
})
.then(() => {
debugMenu();
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
});
}
/**@description Starts the Transaction Cannon execution
* @returns {Nothing}
*/
function startTheCannon() {
var questions = [{
type: 'input',
name: 'transactions',
message: 'Amount of transactions to launch into the ledger',
default: 1,
},{
type: 'confirm',
name: 'replica',
message: 'Do you want the transactions to be replicated?',
default : false
}];
inquirer.prompt(questions).then(function (answers) {
let rep;
if(answers.replica){
rep = 'y';
} else{
rep = 'n';
}
hyper.transactionCannon(answers.transactions,rep)
.then((schedule) => {
return hyper.isLedgerStateCorrect(schedule);
})
.then(() => {
testingMenu();
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
});
}
/**@description Creates a batch amount of accounts and wallets on the system
* @param {String} whereto is where it will go after executing
* @returns {Nothing}
*/
function batchCreation(whereTo) {
var questions = [{
type: 'input',
name: 'clientNumber',
message: 'How many users are we going to create?',
default: 1,
},
{
type: 'input',
name: 'top',
message: 'Which will be the top amount of balance the client will have?',
default: 100000
},
{
type: 'input',
name: 'bottom',
message: 'Which will be the bottom amount of balance the client will have?',
default: 0
}
];
inquirer.prompt(questions).then(function (answers) {
hyper.batchAccount(answers.clientNumber, answers.bottom, answers.top)
.then(() => {
if ('debug') {
debugMenu();
} else {
testingMenu();
}
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
});
}
/**@description Makes a bunch of transactions */
function makeTransaction() {
var questions = [{
type: 'input',
name: "from",
message: 'Which Address from?'
},
{
type: 'input',
name: 'to',
message: 'Which Address to?'
},
{
type: 'input',
name: 'funds',
message: 'How much?'
}
];
inquirer.prompt(questions).then(function (answers) {
hyper.transfer(answers.from, answers.to, answers.funds)
.then(() => {
debugMenu();
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
});
}