forked from waffle-iron/QuorumNetworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joinExistingRaftNetwork.js
152 lines (139 loc) · 4.41 KB
/
joinExistingRaftNetwork.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
let async = require('async')
let exec = require('child_process').exec
let prompt = require('prompt')
let fs = require('fs')
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
let setup = require('./config.js').setup
prompt.start()
function displayGethAccount(result, cb){
console.log('Account:', result.addressList[0])
cb(null, result)
}
function startRaftNode(result, cb){
let options = {encoding: 'utf8', timeout: 100*1000}
let cmd = './startRaftNode.sh'
cmd += ' '+ports.gethNodeRPC
cmd += ' '+ports.gethNode
cmd += ' '+ports.raftHttp
cmd += ' '+result.communicationNetwork.raftID
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 handleExistingFiles(result, cb){
if(result.keepExistingFiles == false){
let seqFunction = async.seq(
util.ClearDirectories,
util.CreateDirectories,
util.GetNewGethAccount,
util.GenerateNodeKey,
util.DisplayEnode,
constellation.CreateNewKeys,
constellation.CreateConfig
)
seqFunction(result, function(err, res){
if (err) { return console.log('ERROR', err) }
cb(null, res)
})
} else {
cb(null, result)
}
}
function handleNetworkConfiguration(result, cb){
if(result.keepExistingFiles == false){
let seqFunction = async.seq(
whisper.RequestExistingNetworkMembership,
whisper.GetGenesisBlockConfig,
whisper.GetStaticNodesFile
)
seqFunction(result, function(err, res){
if (err) { return console.log('ERROR', err) }
cb(null, res)
})
} else {
fs.readFile('Blockchain/raftID', function(err, data){
result.communicationNetwork.raftID = data
cb(null, result)
})
}
}
function joinRaftNetwork(config, cb){
console.log('[*] Starting new network...')
let nodeConfig = {
localIpAddress: config.localIpAddress,
remoteIpAddress : config.remoteIpAddress,
keepExistingFiles: config.keepExistingFiles,
folders: ['Blockchain', 'Blockchain/geth', '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',
},
"web3IPCHost": './Blockchain/geth.ipc',
"web3RPCProvider": 'http://localhost:'+ports.gethNodeRPC
}
let seqFunction = async.seq(
handleExistingFiles,
whisper.JoinCommunicationNetwork,
handleNetworkConfiguration,
startRaftNode,
util.CreateWeb3Connection,
whisper.AddEnodeResponseHandler,
peerHandler.ListenForNewEnodes,
fundingHandler.MonitorAccountBalances
)
seqFunction(nodeConfig, function(err, res){
if (err) { return console.log('ERROR', err) }
console.log('[*] New network started')
cb(err, res)
})
}
function getRemoteIpAddress(cb){
if(setup.automatedSetup === true){
cb(setup.remoteIpAddress)
} else {
console.log('In order to join the network, please enter the ip address of the coordinating node')
prompt.get(['ipAddress'], function (err, network) {
cb(network.ipAddress)
})
}
}
function handleJoiningRaftNetwork(options, cb){
config = {}
config.localIpAddress = options.localIpAddress
config.keepExistingFiles = options.keepExistingFiles
getRemoteIpAddress(function(remoteIpAddress){
config.remoteIpAddress = remoteIpAddress
joinRaftNetwork(config, function(err, result){
if (err) { return console.log('ERROR', err) }
let networks = {
raftNetwork: Object.assign({}, result),
communicationNetwork: config.communicationNetwork
}
cb(err, networks)
})
})
}
exports.HandleJoiningRaftNetwork = handleJoiningRaftNetwork