-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (113 loc) · 3.69 KB
/
index.js
File metadata and controls
135 lines (113 loc) · 3.69 KB
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
const readline = require('readline');
const BFTNode = require('./src/consensus/BFTNode');
const SCPNode = require('./src/consensus/SCPNode');
const CommandHandler = require('./src/cli/CommandHandler');
class ConsensusEngine {
constructor() {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.port = parseInt(process.argv[2]) || 6000;
this.protocol = null;
this.node = null;
this.commandHandler = null;
this.setupEventHandlers();
this.showProtocolMenu();
}
setupEventHandlers() {
this.rl.on('line', (input) => {
if (!this.protocol) {
this.handleProtocolSelection(input.trim());
} else {
const shouldContinue = this.commandHandler.handleCommand(input.trim());
if (!shouldContinue) {
this.shutdown();
return;
}
this.promptUser();
}
});
this.rl.on('close', () => {
this.shutdown();
});
process.on('SIGINT', () => {
console.log('\nReceived SIGINT. Shutting down gracefully...');
this.shutdown();
});
process.on('uncaughtException', (error) => {
console.error('\nUncaught Exception:', error.message);
this.shutdown();
});
process.on('unhandledRejection', (reason, promise) => {
console.error('\nUnhandled Rejection:', reason);
this.shutdown();
});
}
showProtocolMenu() {
console.log('\n===========================================================');
console.log(' CONSENSUS ENGINE - PROTOCOL SELECTION');
console.log('===========================================================\n');
console.log('Please select a consensus protocol to use:\n');
console.log(' 1. BFT (Byzantine Fault Tolerance)');
console.log(' - Traditional BFT consensus with 2/3 majority voting');
console.log(' - Fixed threshold consensus');
console.log(' - Best for: Learning classic BFT concepts\n');
console.log(' 2. SCP (Stellar Consensus Protocol)');
console.log(' - Federated Byzantine Agreement (FBA)');
console.log(' - Quorum slice-based consensus');
console.log(' - Flexible trust relationships');
console.log(' - Best for: Learning modern consensus with flexible trust\n');
console.log('Enter your choice (1 or 2):');
this.rl.setPrompt('> ');
this.rl.prompt();
}
handleProtocolSelection(input) {
const choice = input.trim();
if (choice === '1') {
this.protocol = 'BFT';
this.initializeNode('BFT');
} else if (choice === '2') {
this.protocol = 'SCP';
this.initializeNode('SCP');
} else {
console.log('Invalid choice. Please enter 1 for BFT or 2 for SCP.');
this.rl.prompt();
}
}
initializeNode(protocolType) {
console.log(`\nInitializing ${protocolType} consensus node...\n`);
if (protocolType === 'BFT') {
this.node = new BFTNode(this.port);
} else if (protocolType === 'SCP') {
this.node = new SCPNode(this.port);
}
this.commandHandler = new CommandHandler(this.node, protocolType);
this.start();
}
start() {
this.commandHandler.displayWelcome();
this.node.start(() => {
this.promptUser();
});
}
promptUser() {
this.rl.setPrompt(`${this.node.nodeId}> `);
this.rl.prompt();
}
shutdown() {
const protocolName = this.protocol || 'Consensus';
console.log(`\nGoodbye! ${protocolName} Engine shutting down...`);
if (this.rl) {
this.rl.close();
}
if (this.node) {
this.node.shutdown();
}
setTimeout(() => {
process.exit(0);
}, 1000);
}
}
console.log('Starting Consensus Engine...');
new ConsensusEngine();