-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
292 lines (247 loc) · 8.18 KB
/
app.js
File metadata and controls
292 lines (247 loc) · 8.18 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
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
const { prompt } = require('inquirer');
const Web3 = require('web3');
const program = require('commander');
const shell = require('shelljs');
const fs = require('fs')
const Wallet = require('ethereumjs-wallet');
const EthUtil = require('ethereumjs-util');
const spawn = require('child_process').spawn;
const web3 = new Web3();
const outputDir = './output'
// Executes a command
const executeCommand = async (cmd) => {
if (shell.exec(cmd, { silent: true }).code !== 0) {
return Promise.reject();
} else {
return Promise.resolve();
}
}
// Starts a background process and detaches from parent
const startBGProcess = (cmd, args, logPath) => {
const out = logPath ? fs.openSync(logPath, 'a') : 'ignore';
const err = logPath ? fs.openSync(logPath, 'a') : 'ignore';
spawn( cmd, args, { detached: true, stdio: [ 'ignore', out, err ] }).unref()
}
// Creates and returns genesis for Clique PoA
const generateGenesis = (blockTime, authorityAccounts, prefundAccounts) => {
let genesis = {
config: {
chainId: 1515,
homesteadBlock: 0,
eip150Block: 0,
eip150Hash: '0x0000000000000000000000000000000000000000000000000000000000000000',
eip155Block: 0,
eip158Block: 0,
byzantiumBlock: 0,
constantinopleBlock: 0,
petersburgBlock: 0,
clique: {
period: blockTime,
epoch: 30000
}
},
nonce: '0x0',
timestamp: '0x5d298ced',
gasLimit: '0x47b760',
difficulty: '0x1',
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
coinbase: '0x0000000000000000000000000000000000000000',
alloc: {},
number: '0x0',
gasUsed: '0x0',
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000'
};
prefundAccounts.forEach(account => {
genesis.alloc[account] = {
balance: web3.utils.toHex(web3.utils.toWei('1000', 'ether'))
}
})
let concatenatedAuthorityAccounts = '';
authorityAccounts.forEach(account => concatenatedAuthorityAccounts += account.substring(2))
genesis.extraData = `0x0000000000000000000000000000000000000000000000000000000000000000${concatenatedAuthorityAccounts}0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`
return JSON.stringify(genesis, null, 2)
};
// Deletes the node's files and processes
let resetNetwork = _ => {
shell.rm('-rf', outputDir);
shell.mkdir('-p', outputDir);
shell.cd(outputDir);
shell.exec('pkill bootnode && pkill geth');
}
// Kills the running nodes
let stopNetwork = _ => {
shell.cd(outputDir);
shell.exec('pkill bootnode && pkill geth');
}
// Initializes a new network
let initNetwork = async (authorities, peers, password, blockTime) => {
try {
const totalNodes = authorities + peers;
// Create directories for nodes
for(let count = 1; count <= totalNodes; count++) {
shell.mkdir('-p', `node${count}`);
}
let authorityAccounts = [];
// Create password file for unlocking accounts
fs.writeFileSync('password.txt', password, 'utf8');
//Generate accounts for authorities to seal blocks
for(let count = 1; count <= authorities; count++) {
await executeCommand(`geth --datadir node${count} account new --password password.txt`)
const files = fs.readdirSync(`node${count}/keystore/`);
authorityAccounts.push('0x' + JSON.parse(fs.readFileSync(`node${count}/keystore/${files[0]}`, { encoding: 'utf8' })).address);
}
//Generate genesis file
let genesis = generateGenesis(blockTime, authorityAccounts, authorityAccounts);
fs.writeFileSync('genesis.json', genesis, 'utf8');
//Initialize the nodes with genesis file
for(let count = 1; count <= totalNodes; count++) {
await executeCommand(`geth --datadir node${count}/ init genesis.json`)
}
//Generate bootnode key
await executeCommand(`bootnode -genkey boot.key`)
return Promise.resolve();
} catch (e) {
return Promise.reject(e)
}
}
// Returns bootnode's key
let getBootnodePvtKey = _ => {
const bootnodeKey = fs.readFileSync('boot.key');
return bootnodeKey;
}
// Gets public key of any secp256k1 private key
let getPubKey = (pvtKey) => {
const privateKeyBuffer = EthUtil.toBuffer('0x' + pvtKey);
const wallet = Wallet.fromPrivateKey(privateKeyBuffer);
return wallet.getPublicKeyString().slice(2);
}
// Starts a initailized network
let startNetwork = (bootnodePubkey, authorities, peers) => {
//Start bootnode
startBGProcess('bootnode', ['-nodekey', 'boot.key', '-addr', ':30210'])
let port = 30301;
let rpcPort = 8545;
let totalNodes = authorities + peers;
//Start all the nodes
for(let count = 1; count <= totalNodes; count++) {
let args = [
'--datadir',
`node${count}/`,
`--syncmode`,
`full`,
'--port',
`${port}`,
'--rpc',
'--rpcaddr',
`localhost`,
'--rpcport',
`${rpcPort}`,
'--rpcapi',
`personal,db,eth,net,web3,txpool,miner`,
'--bootnodes',
`enode://${bootnodePubkey}@127.0.0.1:30210`,
'--networkid',
'1515',
'--gasprice',
`1`,
'--verbosity',
'2'
]
if(count <= authorities) {
args = args.concat([
'--unlock',
'0',
'--password',
'password.txt',
'--mine',
'--allow-insecure-unlock'
])
}
startBGProcess('geth', args, `./node${count}/console.log`)
count == 1 ? console.log(`Successfully started the network with ${authorities} authority and ${peers} peer nodes. Here are commands to connect to running nodes: \n`) : null;
console.log(`Node ${count} (${count <= authorities ? 'Authority' : 'Peer'}): \n IPC Attach: geth attach ${outputDir}/node${count}/geth.ipc \n RPC Endpoint: localhost:${rpcPort}`)
port++;
rpcPort++;
}
}
// Saves state so that restart can be supported.
let saveState = (authorities, peers) => {
fs.writeFileSync('network-config.json', JSON.stringify({authorities, peers}), 'utf8');
}
// Reads the saved state
let readState = _ => {
return JSON.parse(fs.readFileSync('network-config.json'));
}
const newNetworkQuestions = [
{
type : 'number',
name : 'authorities',
message : 'Enter total authority nodes ...',
default: 2,
validate: (authorities) => {
if (authorities > 0) {
return true;
} else {
return "Error: atleast 1 authority node is required"
}
}
},
{
type : 'number',
name : 'peers',
message : 'Enter total peer nodes ...',
default: 1,
validate: (peers) => {
if (peers < 0) {
return "Error: peer count should be greater than equal to 0"
} else {
return true
}
}
},
{
type : 'string',
name : 'password',
message : 'Enter password for ethereum accounts ...',
default: 'Password@123'
},
{
type : 'string',
name : 'blockTime',
message : 'Enter block time ...',
default: 5,
validate: (blockTime) => {
if (blockTime > 0) {
return true;
} else {
return "Error: should be more than 0"
}
}
}
];
program.command('new').alias('n').description('Create new Network').action(() => {
prompt(newNetworkQuestions).then(async answers => {
resetNetwork();
answers.authorities = parseInt(answers.authorities)
answers.peers = parseInt(answers.peers)
answers.blockTime = parseInt(answers.blockTime)
await initNetwork(answers.authorities, answers.peers, answers.password, answers.blockTime)
startNetwork(getPubKey(getBootnodePvtKey()), answers.authorities, answers.peers);
saveState(answers.authorities, answers.peers);
});
});
program .command('delete') .alias('d') .description('Delete Network').action(_ => {
resetNetwork();
console.log('Network deleted successfully.')
});
program .command('stop') .alias('s') .description('Stop Network').action(_ => {
stopNetwork();
console.log('Network stopped successfully.');
});
program .command('restart') .alias('r') .description('Restart Network').action(_ => {
stopNetwork();
let answers = readState();
startNetwork(getPubKey(getBootnodePvtKey()), answers.authorities, answers.peers);
console.log('Network restarted successfully');
});
program.parse(process.argv);