forked from waffle-iron/QuorumNetworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
255 lines (244 loc) · 8.71 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
var prompt = require('prompt')
var util = require('./util.js')
var statistics = require('./networkStatistics.js')
var newNetworkSetup = require('./newNetworkSetup.js')
var joinNewNetwork = require('./joinNewNetwork.js')
var rejoinNetork = require('./rejoinNetwork.js')
var newRaftNetwork = require('./newRaftNetwork.js')
var joinRaftNetwork = require('./joinRaftNetwork.js')
var joinExistingRaftNetwork = require('./joinExistingRaftNetwork.js')
var ipAddresses = require('./ipAddress.js')
var config = require('./config.js')
prompt.start();
// TODO: These global vars should be refactored
var quorumNetwork = null
var raftNetwork = null
var communicationNetwork = null
var localIpAddress = null
var remoteIpAddress = null
var checkForOtherProcesses = false
var networkStatisticsEnabled = false;
var consensus = null //quorumChain or raft
function handleConsensusChoice(){
console.log('Please select an option:\n1) Raft\n2) QuorumChain\n5) Kill all geth and constellation')
prompt.get(['option'], function(err, answer){
if(answer.option == 1){
consensus = 'raft'
mainLoop()
} else if (answer.option == 2){
consensus = 'quorumChain'
mainLoop()
} else if(answer.option == 5){
util.KillallGethConstellationNode(function(err, result){
if (err) { return onErr(err); }
quorumNetwork = null;
raftNetwork = null
communicationNetwork = null;
mainLoop()
})
}
})
}
function handleQuorumConsensus(){
console.log('Please select an option below:');
console.log('1) Start a new Quorum network [WARNING: this clears everything]');
console.log('2) Join an existing Quorum network, first time joining this network. [WARNING: this clears everything]');
console.log('3) Reconnect to the previously connected network');
if(networkStatisticsEnabled){
console.log('4) Display network statistics');
} else {
console.log('4) Enable network statistics');
}
console.log('5) killall geth constellation-node');
console.log('6) Display communication network connection details')
console.log('0) Quit');
prompt.get(['option'], function (err, result) {
if (err) { return onErr(err); }
if(result.option == 1){
newNetworkSetup.HandleStartingNewQuorumNetwork(localIpAddress, function(err, networks){
quorumNetwork = networks.quorumNetwork
communicationNetwork = networks.communicationNetwork
mainLoop()
});
} else if(result.option == 2){
joinNewNetwork.HandleJoiningNewQuorumNetwork(localIpAddress, function(err, networks){
quorumNetwork = networks.quorumNetwork
communicationNetwork = networks.communicationNetwork
mainLoop();
});
} else if(result.option == 3){
rejoinNetork.HandleRejoiningQuorumNetwork(localIpAddress, function(err, networks){
quorumNetwork = networks.quorumNetwork
communicationNetwork = networks.communicationNetwork
mainLoop();
});
} else if(networkStatisticsEnabled == false && result.option == 4){
statistics.Enable();
networkStatisticsEnabled = true;
mainLoop();
} else if(networkStatisticsEnabled == true && result.option == 4){
statistics.PrintBlockStatistics();
mainLoop();
} else if(result.option == 5){
util.KillallGethConstellationNode(function(err, result){
if (err) { return onErr(err); }
quorumNetwork = null;
communicationNetwork = null;
mainLoop();
});
} else if(result.option == 6){
console.log('Enode details:')
util.DisplayCommunicationEnode(communicationNetwork, function(err, result){
if(err){console.log('ERROR:', err)}
mainLoop()
})
} else if(result.option == 0){
console.log('Quiting');
process.exit(0);
return;
} else {
mainLoop();
}
});
}
function handleNetworkMembership(cb){
console.log('Please select an option below:');
console.log('1) Allow anyone to connect');
console.log('2) [TODO] Allow only people with pre-auth tokens to connect');
prompt.get(['option'], function(err, result){
if(result.option == 1){
cb({
networkMembership: 'allowAll'
})
} else {
console.log('This option is still TODO, defaulting to option 1');
cb({
//networkMembership: 'allowOnlyPreAuth'
networkMembership: 'allowAll'
})
}
})
}
function keepExistingFiles(cb){
console.log('Please select an option below:');
console.log('1) Clear all files/configuration and start from scratch[WARNING: this clears everything]')
console.log('2) Keep old files/configuration intact and start the node + whisper services')
prompt.get(['option'], function(err, result){
let keepFiles = false;
if(result.option == 1){
keepFiles = false
} else {
keepFiles = true
}
cb({
keepExistingFiles: keepFiles
})
})
}
function handleRaftConsensus(){
console.log('Please select an option below:');
console.log('----- Option 1 and 2 are for the initial setup of a raft network -----')
console.log('1) Start a node as the setup coordinator [Ideally there should only be one coordinator]')
console.log('2) Start a node as a non-coordinator')
console.log('----- Option 3 is for joining a raft network post initial setup -----')
console.log('3) Join a raft network if you were not part of the initial setup')
console.log('4) TODO: Start whisper services and attach to already running node')
console.log('5) killall geth constellation-node');
console.log('0) Quit');
prompt.get(['option'], function(err, result){
if(result.option == 1){
handleNetworkMembership(function(res){
keepExistingFiles(function(setup){
let options = {
localIpAddress: localIpAddress,
networkMembership: res.networkMembership,
keepExistingFiles: setup.keepExistingFiles
};
newRaftNetwork.HandleStartingNewRaftNetwork(options, function(err, networks){
raftNetwork = networks.raftNetwork
communicationNetwork = networks.communicationNetwork
mainLoop()
})
})
})
} else if(result.option == 2){
keepExistingFiles(function(setup){
let options = {
localIpAddress: localIpAddress,
keepExistingFiles: setup.keepExistingFiles
};
joinRaftNetwork.HandleJoiningRaftNetwork(options, function(err, networks){
raftNetwork = networks.raftNetwork
communicationNetwork = networks.communicationNetwork
mainLoop()
})
})
} else if(result.option == 3){
keepExistingFiles(function(setup){
let options = {
localIpAddress: localIpAddress,
keepExistingFiles: setup.keepExistingFiles
};
joinExistingRaftNetwork.HandleJoiningRaftNetwork(options, function(err, networks){
raftNetwork = networks.raftNetwork
communicationNetwork = networks.communicationNetwork
mainLoop()
})
})
} else if(result.option == 4){
console.log('This is stil on the TODO list')
mainLoop()
} else if(result.option == 5){
util.KillallGethConstellationNode(function(err, result){
if (err) { return onErr(err) }
raftNetwork = null
communicationNetwork = null
mainLoop()
})
} else if(result.option == 0){
console.log('Quiting')
process.exit(0)
return
} else {
mainLoop()
}
})
}
function mainLoop(){
if(localIpAddress && checkForOtherProcesses == false) {
util.CheckPreviousCleanExit(function(err, done){
if(err) {console.log('ERROR:', err)}
checkForOtherProcesses = done
mainLoop()
})
} else if(localIpAddress && checkForOtherProcesses && consensus === 'quorumChain'){
handleQuorumConsensus()
} else if(localIpAddress && checkForOtherProcesses && consensus === 'raft'){
handleRaftConsensus()
} else if(localIpAddress && checkForOtherProcesses && consensus == null){
handleConsensusChoice()
} else {
console.log('Trying to get public ip address, please wait a few seconds...')
ipAddresses.WhatIsMyIp(function(ip){
console.log('Welcome! \n\n'
+'Please enter the IP address other nodes will use to connect to this node. \n\n'
+'Also, please enter a publicly identifyable string for this node to use.\n\n')
let schema = [{
name: 'localIpAddress',
default: ip.publicIp
}, {
name: 'nodeName' // TODO: Add schema to remove unwanted characters etc.
}]
prompt.get(schema, function (err, answer) {
localIpAddress = answer.localIpAddress
config.identity.nodeName = answer.nodeName
mainLoop()
})
})
}
}
function onErr(err) {
console.log(err);
return 1;
}
mainLoop();