forked from waffle-iron/QuorumNetworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
317 lines (296 loc) · 8.92 KB
/
util.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
var exec = require('child_process').exec;
var ps = require('ps-node')
var fs = require('fs');
var async = require('async')
var ports = require('./config.js').ports
function killallGethConstellationNode(cb){
var cmd = 'killall -9';
cmd += ' geth';
cmd += ' constellation-node';
var child = exec(cmd, function(){
cb(null, null);
});
child.stderr.on('data', function(error){
console.log('ERROR:', error);
cb(error, null);
});
}
function clearDirectories(result, cb){
var cmd = 'rm -rf';
for(var i in result.folders){
var folder = result.folders[i];
cmd += ' '+folder;
}
var child = exec(cmd, function(){
cb(null, result);
});
child.stderr.on('data', function(error){
console.log('ERROR:', error);
cb(error, null);
});
}
function createDirectories(result, cb){
var cmd = 'mkdir';
for(var i in result.folders){
var folder = result.folders[i];
cmd += ' '+folder;
}
var child = exec(cmd, function(){
cb(null, result);
});
child.stderr.on('data', function(error){
console.log('ERROR:', error);
cb(error, null);
});
}
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2){
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
// TODO: Add failure after a number of retries
function waitForIPCPath(path, cb){
if (fs.existsSync(path)) {
cb()
} else {
setTimeout(function(){
waitForIPCPath(path, cb)
}, 1000)
}
}
function waitForIPCConnection(web3IPC, cb){
let web3IPCConnection = web3IPC.currentProvider.connection
if(web3IPCConnection.connecting == false){
cb()
} else {
setTimeout(function(){
waitForIPCConnection(web3IPC, cb)
}, 1000)
}
}
// TODO: add error handler here for web3 connections so that program doesn't exit on error
function createWeb3Connection(result, cb){
console.log('[* Info] If you see a single message starting with \"IPC Connection Error\", you can safely ignore it')
let host = result.web3IPCHost;
waitForIPCPath(host, function(){
// Web3 IPC
let Web3IPC = require('web3_ipc');
let options = {
host: host,
ipc: true,
personal: true,
admin: true,
debug: false
};
let web3IPC = Web3IPC.create(options);
let web3IPCConnection = web3IPC.currentProvider.connection
waitForIPCConnection(web3IPC, function(){
console.log('[*] IPC Connection open, node started')
result.web3IPC = web3IPC;
// Web3 RPC
let httpProvider = result.web3RPCProvider;
let Web3RPC = require('web3');
let web3RPC = new Web3RPC(new Web3RPC.providers.HttpProvider(httpProvider));
result.web3RPC = web3RPC;
// Web3 RPC Quorum
let Web3RPCQuorum = require('web3quorum');
let web3RPCQuorum = new Web3RPCQuorum(new Web3RPCQuorum.providers.HttpProvider(httpProvider));
result.web3RPCQuorum = web3RPCQuorum;
cb(null, result);
})
})
}
function connectToPeer(result, cb){
var enode = result.enode;
result.web3IPC.admin.addPeer(enode, function(err, res){
if(err){console.log('ERROR:', err);}
cb(null, result);
});
}
function getNewGethAccount(result, cb){
var options = {encoding: 'utf8', timeout: 10*1000};
var child = exec('geth --datadir Blockchain account new', options);
child.stdout.on('data', function(data){
if(data.indexOf('Your new account') >= 0){
child.stdin.write('\n');
} else if(data.indexOf('Repeat') >= 0){
child.stdin.write('\n');
} else if(data.indexOf('Address') == 0){
var index = data.indexOf('{');
var address = '0x'+data.substring(index+1, data.length-2);
if(result.addressList == undefined){
result.addressList = [];
}
result.addressList.push(address);
cb(null, result);
}
});
child.stderr.on('data', function(error){
if(error.indexOf('No etherbase set and no accounts found as default') < 0){
console.log('ERROR:', error);
cb(error, null);
}
});
}
function instanceAlreadyRunningMessage(processName){
console.log('\n--- NOTE: There is an instance of '+processName+' already running.'+
' Please kill this instance by selecting option 5 before continuing\n')
}
function checkPreviousCleanExit(cb){
async.parallel({
geth: function(callback){
ps.lookup({
command: 'geth',
psargs: 'ef'
},
function(err, resultList){
callback(err, resultList)
})
},
constellation: function(callback){
ps.lookup({
command: 'constellation-node',
psargs: 'ef'
},
function(err, resultList){
callback(err, resultList)
})
}
}, function(err, result){
if(result && result.geth && result.geth.length > 0){
instanceAlreadyRunningMessage('geth')
}
if(result && result.constellation && result.constellation.length > 0){
instanceAlreadyRunningMessage('constellation')
}
cb(err, true)
})
}
function createQuorumConfig(result, cb){
console.log('[*] Creating genesis config...');
var options = {encoding: 'utf8', timeout: 100*1000};
let voterList = ''
for(let bv of result.blockVoters){
voterList += '"'+bv+'",'
}
voterList = voterList.slice(0, -1)
let makerList = ''
for(let bm of result.blockMakers){
makerList += '"'+bm+'",'
}
makerList = makerList.slice(0, -1)
var config = '{'
+'"threshold": '+result.threshold+','
+'"voters": ['
+ voterList
+'],'
+'"makers": ['
+ makerList
+']'
+'}'
fs.writeFile('quorum-config.json', config, function(err, res){
cb(err, result);
});
}
function createGenesisBlockConfig(result, cb){
var options = {encoding: 'utf8', timeout: 100*1000};
var child = exec('quorum-genesis', options, function(error, stderr, stdout){
result.communicationNetwork.genesisBlockConfigReady = true;
cb(null, result);
});
child.stderr.on('data', function(error){
console.log('ERROR:', error);
cb(error, null);
});
}
function isWeb3RPCConnectionAlive(web3RPC){
let isAlive = false
try{
let accounts = web3RPC.eth.accounts
if(accounts){
isAlive = true
}
} catch(err){ }
return isAlive
}
function generateNodeKey(result, cb){
var options = {encoding: 'utf8', timeout: 10*1000};
console.log('Generating node key')
var child = exec('bootnode -genkey Blockchain/geth/nodekey', options)
child.stderr.on('data', function(error){
console.log('ERROR:', error)
})
setTimeout(function(){
cb(null, result)
}, 1000)
}
// TODO: ports shouldn't be hard coded!!!
function displayCommunicationEnode(result, cb){
if(!result){
return cb({error: 'parameter not defined, could not get ip address'}, null)
}
var options = {encoding: 'utf8', timeout: 10*1000};
var child = exec('bootnode -nodekey CommunicationNode/geth/nodekey -writeaddress', options)
child.stdout.on('data', function(data){
data = data.slice(0, -1)
let enode = 'enode://'+data+'@'+result.localIpAddress+':'
+ports.communicationNode
console.log('\n', enode+'\n')
result.nodePubKey = data
result.enodeList = [enode]
cb(null, result)
})
child.stderr.on('data', function(error){
console.log('ERROR:', error)
cb(error, null)
})
}
function displayEnode(result, cb){
let options = {encoding: 'utf8', timeout: 10*1000};
let child = exec('bootnode -nodekey Blockchain/geth/nodekey -writeaddress', options)
child.stdout.on('data', function(data){
data = data.slice(0, -1)
let enode = 'enode://'+data+'@'+result.localIpAddress+':'+ports.gethNode+'?raftport='+ports.raftHttp
console.log('\nenode:', enode+'\n')
result.nodePubKey = data
result.enodeList = [enode] // TODO: investigate why this is a list
cb(null, result)
})
child.stderr.on('data', function(error){
console.log('ERROR:', error)
cb(error, null)
})
}
function unlockAllAccounts(result, cb){
console.log('[INFO] Unlocking all accounts ...');
async.each(result.web3RPC.eth.accounts, function(account, callback){
result.web3IPC.personal.unlockAccount(account, '', 999999, function(err, res){
callback(err, res)
})
}, function(err){
if(err){
console.log('ERROR:', err)
} else {
console.log('[INFO] All accounts unlocked')
}
cb(null, result)
})
}
exports.Hex2a = hex2a;
exports.ClearDirectories = clearDirectories;
exports.CreateDirectories = createDirectories;
exports.CreateWeb3Connection = createWeb3Connection;
exports.ConnectToPeer = connectToPeer;
exports.KillallGethConstellationNode = killallGethConstellationNode;
exports.GetNewGethAccount = getNewGethAccount;
exports.CheckPreviousCleanExit = checkPreviousCleanExit
exports.CreateQuorumConfig = createQuorumConfig
exports.CreateGenesisBlockConfig = createGenesisBlockConfig
exports.IsWeb3RPCConnectionAlive = isWeb3RPCConnectionAlive
exports.GenerateNodeKey = generateNodeKey
exports.DisplayEnode = displayEnode
exports.DisplayCommunicationEnode = displayCommunicationEnode
exports.UnlockAllAccounts = unlockAllAccounts