forked from waffle-iron/QuorumNetworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rejoinNetwork.js
250 lines (233 loc) · 8.21 KB
/
rejoinNetwork.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
let async = require('async')
let exec = require('child_process').exec
let prompt = require('prompt')
let whisper = require('./Communication/whisperNetwork.js')
let util = require('./util.js')
let constellation = require('./constellation.js')
let statistics = require('./networkStatistics.js')
let peerHandler = require('./peerHandler.js')
let fundingHandler = require('./fundingHandler.js')
let ports = require('./config.js').ports
prompt.start()
function rejoinQuorumNetwork(config, cb){
console.log('[*] Rejoining existing quorum network...');
let startNode = null
if(config.joinOption == 0){
startNode = startQuorumBMAndBVNode
} else if(config.joinOption == 1) {
startNode = startQuorumBMNode
} else if(config.joinOption == 2){
startNode = startQuorumBVNode
} else {
startNode = startQuorumParticipantNode
}
//whisper.AddEtherResponseHandler,
let seqFunction = async.seq(
getConfiguration,
startNode,
util.CreateWeb3Connection,
peerHandler.ListenForNewEnodes,
whisper.AddEnodeRequestHandler,
whisper.AddEnodeResponseHandler,
peerHandler.ListenForNewEnodes,
fundingHandler.MonitorAccountBalances,
statistics.Setup
)
let result = {
joinOption: config.joinOption,
localIpAddress: config.localIpAddress,
folders: ['Blockchain', 'Constellation'],
constellationKeySetup: [
{folderName: 'Constellation', fileName: 'node'},
{folderName: 'Constellation', fileName: 'nodeArch'}
],
constellationConfigSetup: {
configName: 'constellation.config',
folderName: 'Constellation',
localIpAddress : config.localIpAddress,
localPort : ports.constellation,
remoteIpAddress : config.remoteIpAddress,
remotePort : ports.constellation,
publicKeyFileName: 'node.pub',
privateKeyFileName: 'node.key',
publicArchKeyFileName: 'nodeArch.pub',
privateArchKeyFileName: 'nodeArch.key',
},
communicationNetwork: config.communicationNetwork,
"web3IPCHost": './Blockchain/geth.ipc',
"web3RPCProvider": 'http://localhost:'+ports.gethNodeRPC
}
seqFunction(result, function(err, res){
if (err) { return console.log('ERROR', err) }
console.log('[*] Network joined')
cb(err, res)
})
}
function startQuorumBMAndBVNode(result, cb){
console.log('[*] Started node as BM + BV')
let options = {encoding: 'utf8', timeout: 100*1000}
let cmd = './startQuorumBMAndBVNode.sh'
cmd += ' '+result.blockVoters[0]
cmd += ' '+result.blockMakers[0]
cmd += ' '+result.minBlockTime
cmd += ' '+result.maxBlockTime
cmd += ' '+ports.gethNodeRPC
cmd += ' '+ports.gethNode
let child = exec(cmd, options)
child.stdout.on('data', function(data){
cb(null, result)
})
child.stderr.on('data', function(error){
console.log('ERROR:', error)
cb(error, null)
})
}
function startQuorumBMNode(result, cb){
let options = {encoding: 'utf8', timeout: 100*1000}
let cmd = './startQuorumBMNode.sh'
cmd += ' '+result.blockMakers[0]
cmd += ' '+result.minBlockTime
cmd += ' '+result.maxBlockTime
cmd += ' '+ports.gethNodeRPC
cmd += ' '+ports.gethNode
let child = exec(cmd, options)
child.stdout.on('data', function(data){
cb(null, result)
})
child.stderr.on('data', function(error){
console.log('ERROR:', error)
cb(error, null)
})
}
function startQuorumBVNode(result, cb){
let options = {encoding: 'utf8', timeout: 100*1000}
let cmd = './startQuorumBVNode.sh'
cmd += ' '+result.blockVoters[0]
cmd += ' '+result.minBlockTime
cmd += ' '+result.maxBlockTime
cmd += ' '+ports.gethNodeRPC
cmd += ' '+ports.gethNode
let child = exec(cmd, options)
child.stdout.on('data', function(data){
cb(null, result)
})
child.stderr.on('data', function(error){
console.log('ERROR:', error)
cb(error, null)
})
}
function startQuorumParticipantNode(result, cb){
console.log('Starting quorum participant node...');
var options = {encoding: 'utf8', timeout: 100*1000};
var cmd = './startQuorumParticipantNode.sh';
cmd += ' '+ports.gethNodeRPC
cmd += ' '+ports.gethNode
var child = exec(cmd, options);
child.stdout.on('data', function(data){
console.log('Started quorum participant node');
cb(null, result);
});
child.stderr.on('data', function(error){
console.log('ERROR:', error);
cb(error, null);
});
}
function getAddress(cb){
console.log('Please enter either: '
+'\n1) an address (starting with 0x) '
+'\n2) a 1 to generate an address to use'
+'\n3) a 0 to not participate in this role')
let address = ''
prompt.get(['address'], function (err, answer) {
if(answer.address.indexOf('1') === 0){
console.log('generating new address...')
util.GetNewGethAccount({}, function(err, res){
address = res.addressList[0]
cb(err, address)
})
} else if(answer.address.indexOf('0x') === 0){
address = answer.address
cb(err, address)
} else{
address = null
cb(err, address)
}
})
}
function getConfiguration(result, cb){
console.log('Getting config for:', result.joinOption)
if(result.joinOption == 0 || result.joinOption == 1 || result.joinOption == 2){
console.log('Please enter the configuration for this network:')
prompt.get(['minimumTimeBetweenBlocks', 'maximumTimeBetweenBlocks']
, function (err, config) {
if(err){console.log('ERROR:', err)}
result.minBlockTime = config.minimumTimeBetweenBlocks
result.maxBlockTime = config.maximumTimeBetweenBlocks
if(result.joinOption == 0){
console.log('\nSelect which block maker to use')
getAddress(function(err, blockMaker){
if(err){console.log('ERROR:', err)}
result.blockMakers = [blockMaker]
console.log('Please use '+blockMaker + ' as your block maker address')
console.log('\nSelect which block voter to use')
getAddress(function(err, blockVoter){
if(err){console.log('ERROR:', err)}
result.blockVoters = [blockVoter]
console.log('Please use '+blockVoter + ' as your block voter address')
cb(err, result)
})
})
} else if(result.joinOption == 1) {
console.log('\nSelect which block maker to use')
getAddress(function(err, blockMaker){
if(err){console.log('ERROR:', err)}
result.blockMakers = [blockMaker]
console.log('Please use '+blockMaker + ' as your block maker address')
cb(err, result)
})
} else if(result.joinOption == 2){
console.log('\nSelect which block voter to use')
getAddress(function(err, blockVoter){
if(err){console.log('ERROR:', err)}
result.blockVoters = [blockVoter]
console.log('Please use '+blockVoter + ' as your block voter address')
cb(err, result)
})
}
})
} else {
cb(null, result)
}
}
function handleRejoiningQuorumNetwork(localIpAddress, cb){
config = {}
config.localIpAddress = localIpAddress
console.log('In order to rejoin an existing network, '
+ 'please enter the ip address of one of the managing nodes')
prompt.get(['ipAddress'], function (err, network) {
config.remoteIpAddress = network.ipAddress
console.log('Please select an option:')
console.log('0) Join the network as a block maker and block voter')
console.log('1) Join the network as a block maker')
console.log('2) Join the network as a block voter')
console.log('3) Join the network as a participant')
prompt.get(['option'], function (err, answer) {
config.joinOption = answer.option
whisper.JoinCommunicationNetwork(config, function(err, result){
if (err) { return console('ERROR:', err) }
config.communicationNetwork = Object.assign({}, result)
rejoinQuorumNetwork(config, function(err, result){
if (err) { return console.log('ERROR:', err) }
let networks = {
quorumNetwork: Object.assign({}, result),
communicationNetwork: config.communicationNetwork
}
networks.quorumNetwork.localIpAddress = localIpAddress
networks.communicationNetwork.localIpAddress = localIpAddress
cb(err, networks)
})
})
})
})
}
exports.HandleRejoiningQuorumNetwork = handleRejoiningQuorumNetwork