This repository has been archived by the owner on Oct 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.js
145 lines (110 loc) · 3.61 KB
/
runner.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
/**
* @module runner
*/
'use strict';
const num = +process.argv.slice(2)[0] || 33;
const rb = require('crypto').randomBytes;
const fs = require('fs');
const cp = require('child_process');
const evt = require('lib/events');
const Genesis = require('lib/genesis');
const tp = require('core/transport');
const Account = require('core/account');
/**
* Initial voting power of the delegate.
*
* @type {Number}
*/
const DELEGATE_BALANCE = 100;
/**
* Path to genesis file.
*
* @type {String}
*/
const GENESIS_PATH = `data/${rb(4).toString('hex')}.json`;
const env = Object.assign({DELEGATES: num, GENESIS_PATH}, process.env);
const delegates = [];
const producers = [];
const bank = Account();
const genesis = Genesis();
genesis.addAccount(bank.address.toString('hex'), 1000000);
for (let i = 0; i < num; i++) {
const account = Account();
delegates.push(account);
genesis.addDelegate(account.address.toString('hex'), DELEGATE_BALANCE);
}
for (let i = 0; i < num; i++) {
const account = Account();
producers.push(account);
genesis.addProducer(account.address.toString('hex'), 1);
}
genesis.writeToFile(GENESIS_PATH);
const kids = []
.concat(delegates.map(spawnDelegate))
.concat(producers.map(spawnProducer));
const bankKiddo = cp.fork('clients/bank.js', ['bank'], {env: Object.assign(env, {SECRET_KEY: bank.secretKey.toString('hex')})});
const repl = require('repl').start('> ');
repl.context.kids = kids;
repl.context.tp = tp;
repl.context.start = () => tp.send(evt.START_ROUND);
repl.context.ping = (addr) => tp.send(evt.PING, addr);
tp.on(evt.PONG, (data) => console.log('Yup, dude, %s', data));
tp.delegates = new Map();
tp.on(evt.I_AM_HERE, (data, msg) => tp.delegates.set(msg.sender, msg));
console.log('DELEGATES:') || delegates.map((e) => console.log('-', e.getHexAddress()));
console.log('PRODUCERS:') || producers.map((e) => console.log('-', e.getHexAddress()));
tp.on(evt.START_ROUND, function () {
tp.once(evt.NEW_BLOCK, function ({block}) {
console.log('New block', block);
});
});
process
.on('exit', finish)
.on('SIGINT', finish)
.on('uncaughtException', finish)
.on('unhandledRejection', finish);
/**
* Spawn child delegate process. To be used with Array.prototype.map
*
* @param {Number} i Sequence number of delegate
* @return {cp.ChildProcess} Spawned child process
*/
function spawnDelegate(e, i) {
const datadir = 'data/del_' + (i + 1);
const options = {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
env: Object.assign({
SECRET_KEY: e.secretKey.toString('hex'),
DATADIR: datadir
}, env),
};
fs.mkdirSync(datadir);
const stream = fs.createWriteStream(datadir + '/out.log', {flags: 'w'});
const child = cp.fork('clients/delegate.js', [], options);
child.stdout.pipe(stream);
child.stderr.pipe(stream);
return child;
}
function spawnProducer(e, i) {
const datadir = 'data/prod_' + (i + 1);
const options = {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
env: Object.assign({
SECRET_KEY: e.secretKey.toString('hex'),
DATADIR: datadir
}, env),
};
fs.mkdirSync(datadir);
const stream = fs.createWriteStream(datadir + '/out.log', {flags: 'w'});
const child = cp.fork('clients/block-producer.js', [], options);
child.stdout.pipe(stream);
child.stderr.pipe(stream);
return child;
}
function finish(...args) {
return console.log('Cleaning up:', args)
|| bankKiddo.kill()
|| true
&& kids.map((kid) => kid.kill())
&& process.exit(0);
}