-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocsim.js
358 lines (329 loc) · 11.1 KB
/
pocsim.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// ComPOS Simulator
const num_nodes = 100;
const sim_time = 10000;
const peer_conns = 4; // 1 + Log10(size-of-network) ?
const fail_chance = 0.1 // chance of failure; 0 = no failure
const fail_max_time = 80 // up to 8 seconds
const block_interval = 60 // 6 seconds (10 ticks per second)
const short_interval = 50 // 5 seconds (10 ticks per second)
const attest_interval = 20 // 2 second attestation period
const catchup_timeout = 30 // 3 seconds to get a catchup-block
// The ring contains the schedule of staking nodes and is
// consensus state (we assume all nodes agree on this state)
let ring = []
// The set of nodes under simulation
let nodes = []
// Index of the next staking node to propose a block, in the ring
// let turn = 3 // prior 3 nodes will attest 1st block
// The current simulation time (one tick = 100ms)
let time = 0
// Unique block hash so nodes can compare tip
let nexthash = 286127
// Currently failing nodes
let failures = []
// Message queue
let queue = []
function init() {
for (let i=0; i<num_nodes; i++) {
nodes.push({
addr: i,
peers: [],
online: true,
height: 0,
tip: 0,
catchup: 0,
block: null,
started: 0,
offline: 0,
received: 0,
proposed: 0,
accepted: 0,
attested: 0,
staking: false,
fail_ends: 0,
due_time: 0,
turn: 3,
blocks: [],
})
}
for (let node of nodes) {
// ensure every peer has some inbound connections
const peers = [];
let n = peer_conns
while (n) {
let id = Math.floor(Math.random()*num_nodes)
if (peers.includes(id)) continue
peers.push(id)
nodes[id].peers.push(node.addr) // gossip to me
n--;
}
}
for (let node of nodes) {
// ensure every peer has some outbound connections
while (node.peers.length < peer_conns) {
let id = Math.floor(Math.random()*num_nodes)
if (node.peers.includes(id)) continue
node.peers.push(id)
}
}
// populate the ring with a random permutation of 1/4 of the nodes.
let n = Math.max(100, Math.floor(nodes.length/4));
while (n) {
let id = Math.floor(Math.random()*nodes.length)
if (ring.includes(id)) continue
ring.push(id)
nodes[id].staking = true;
n--;
}
}
function failure() {
// end prior failures
let to = 0;
for (let node of failures) {
if (node.fail_ends <= time) {
node.online = true
console.log(`${time}: [${node.addr}] I'm back online!`);
} else {
failures[to++] = node; // keep
}
}
failures.length = to;
// randomly flip one node online/offline
if (Math.random() < fail_chance) {
let id = Math.floor(Math.random()*nodes.length)
let node = nodes[id];
if (node.online) {
node.online = false
node.offline++;
node.fail_ends = time + Math.floor(Math.random()*fail_max_time)
failures.push(node)
console.log(`${time}: [${id}] I went offline :O`);
}
}
}
function prev(turn,n) {
const p = ring[(turn + ring.length - n) % ring.length];
if (p == null) throw `${turn} ${turn + ring.length - n} ${ring.length}`;
return p;
}
function think(node) {
if (!node.online) return;
// if a block was missed (clock > node.height)
// subsequent block times are shortened to catch up
// const since = Math.max(0, time - (node.height * block_interval));
// const clock = node.height + Math.floor(since / short_interval);
// const turn = ring[clock % ring.length];
// can go back in time:
// when adding node.height, the now-past short-blocks become long-blocks
// meaning the more recent short-blocks can become undone!
// do we record the time-slice assigned to each block?
// due_time += block_interval or short_interval
if (time >= node.due_time) {
// time for the next turn to start.
node.turn = (node.turn + 1) % ring.length;
const ideal_height = Math.floor(time / block_interval)
if (node.height < ideal_height) {
node.due_time = time + short_interval
console.log(`${time}: [${node.addr}] it's ${ring[node.turn]}'s turn to produce a block (ideal ${1+ideal_height}) - quickly!`);
} else {
node.due_time = time + block_interval
console.log(`${time}: [${node.addr}] it's ${ring[node.turn]}'s turn to produce a block (ideal ${1+ideal_height})`);
}
}
if (node.addr == ring[node.turn]) {
// my turn to mint a block
if (!node.block) {
let block = { block:"block", sig:node.addr, hash:nexthash++, prev:node.tip, height:node.height+1, time:time, attest:[] };
node.block = block;
node.started = time;
node.proposed++;
console.log(`${time}: [${node.addr}] I propose a block at height ${block.height}`);
// send request to 5 prior nodes to attest the block
send(prev(node.turn,7), node.addr, 'attest', block);
send(prev(node.turn,21), node.addr, 'attest', block);
send(prev(node.turn,35), node.addr, 'attest', block);
send(prev(node.turn,56), node.addr, 'attest', block);
send(prev(node.turn,77), node.addr, 'attest', block);
}
} else {
// not my turn.
if (node.block) {
console.log(`${time}: [${node.addr}] My turn is finished`);
node.block = null;
node.started = 0;
}
}
}
function ask_a_peer(node) {
node.catchup = time + catchup_timeout; // start or extend catch-up period
if (node.peers.length) {
let who = node.peers[Math.floor(Math.random()*node.peers.length)];
send(who, node.addr, 'getblock', { height:node.height+1 });
} else {
console.log(`${time}: [${node.addr}] Fie! I don't have any peers.`);
}
}
function check_behind(node, height) {
if (height > node.height) {
// I need to catch up, but can't really trust the sender.
// But first, am I already catching up?
if (time >= node.catchup) {
// OK to start catching up.
// I'll ask a peer to send me the next block, and see if that works.
console.log(`${time}: [${node.addr}] I am behind; asking a peer for help.`);
ask_a_peer(node);
}
}
}
function receive(node, name, data, from) {
if (!node.online) {
console.log(`${time}: [${node.addr}] I'm offline! Missed '${name}' from [${from}]`);
return;
}
node.received++;
// console.log(`${time}: [${node.addr}] <= [${from}] ${name}`);
switch (name) {
case 'attest': {
// another node has requested attestation
const block = data;
if (!block.sig == ring[node.turn]) {
console.log(`${time}: [${node.addr}] I refuse to attest: it's not your turn, ${block.sig}!`);
return;
}
if (time > block.time + attest_interval) {
console.log(`${time}: [${node.addr}] I refuse to attest: your block is too old: ${block.time} vs ${time}`);
return;
}
if (block.height !== node.height+1) {
console.log(`${time}: [${node.addr}] I refuse to attest: your block has the wrong height: ${block.height} (expecting ${node.height+1})`);
check_behind(node, block.height);
return;
}
if (block.prev !== node.tip) {
console.log(`${time}: [${node.addr}] I refuse to attest: your block has the wrong prev-hash: ${block.prev} (expecting ${node.tip})`);
check_behind(node, block.height);
return;
}
node.attested++;
send(from, node.addr, 'attested', { sig: node.addr });
return;
}
case 'attested': {
// another node has attested to my block
const block = node.block;
if (block != null && block.attest.length < 3) {
// I am minting a block and I still need attestations.
if (!block.attest.includes(data.sig)) {
block.attest.push(data.sig);
}
console.log(`${time}: [${node.addr}] I received your attestation, [${from}]; have=${block.attest.length} start=${node.started} time=${time}`);
// Do I have 3 attestations, within the attestation period?
if (block.attest.length == 3 && time < node.started + attest_interval) {
// Success! Gossip the new block.
// But first, I should accept my own block.
if (block.prev === node.tip) {
node.height = block.height
node.tip = block.hash
node.blocks.push(block);
console.log(`${time}: [${node.addr}] I minted a block at height ${block.height}!`);
gossip(node.peers, node.addr, 'block', block);
} else {
console.log(`${time}: [${node.addr}] Oops! My minted block is no longer at the tip: ${block.prev} vs ${node.tip}`);
}
}
} else if (block != null) {
console.log(`${time}: [${node.addr}] I don't need your attestation, [${from}]; have=${block.attest.length} start=${node.started} time=${time}`);
}
return;
}
case 'block': {
// if the block has 3 attestations and matches my height, accept the block.
const block = data;
if (block.hash != node.tip) {
// simulation: assume I verify signature, attestations, transactions vs chainstate, etc.
if (block.height !== node.height + 1) {
console.log(`${time}: [${node.addr}] I reject your block: wrong height: ${block.height} (expecting ${node.height+1})`);
check_behind(node, block.height);
} else if (block.prev !== node.tip) {
console.log(`${time}: [${node.addr}] I reject your block: wrong prev-hash: ${block.prev} (expecting ${node.tip})`);
} else if (block.attest.length !== 3) {
console.log(`${time}: [${node.addr}] I reject your block: not enough attestations`);
} else if (block.time > time) {
console.log(`${time}: [${node.addr}] I reject your block: it is too new: ${block.time} vs ${time}`);
} else {
node.height = block.height
node.tip = block.hash;
node.accepted++;
node.blocks.push(block);
console.log(`${time}: [${node.addr}] I accepted a block at height ${block.height}`);
if (time < node.catchup) {
// I'm catching up on missed blocks.
// OK I did receive a valid, newer block. I'll ask a peer again.
console.log(`${time}: [${node.addr}] I am still behind; asking a peer again.`);
ask_a_peer(node);
} else {
// forward the block to my peers.
gossip(node.peers, node.addr, 'block', block);
}
}
}
return;
}
case 'getblock': {
// send back a block if I have it.
const request = data;
if (request.height > 0 && request.height <= node.blocks.length) {
send(from, node.addr, 'block', node.blocks[request.height-1]);
} else {
console.log(`${time}: [${node.addr}] I don't have block ${request.height}`);
}
return;
}
}
}
function send(to,from,name,data) {
if (to==null) throw 1;
if (from==null) throw 1;
let delay = Math.floor(Math.random()*4) // simulates 0-300ms (0-3 ticks)
queue.push({at:time+delay,to,from,name,data})
// console.log(`${time}: [${from}] => [${to}] ${name}`);
}
function gossip(peers,from,name,data) {
for (let peer of peers) {
send(peer,from,name,data);
}
}
function tick() {
time++;
failure(); // random node failure.
// make queued messages arrive.
let to = 0;
for (let msg of queue) {
if (msg.at <= time) {
if (msg.to==null) throw 1;
if (msg.from==null) throw 1;
receive(nodes[msg.to], msg.name, msg.data, msg.from);
} else {
queue[to++] = msg; // keep
}
}
queue.length = to;
// let each node think
for (let node of nodes) {
think(node);
}
}
function run() {
for (let i=0; i<sim_time; i++) {
tick();
}
}
function results() {
for (let node of nodes) {
console.log(`[${node.addr}]: height ${node.height} offline ${node.offline} received ${node.received} proposed ${node.proposed} accepted ${node.accepted} attested ${node.attested} [${node.peers}] ${node.staking?'staking':''}`);
}
}
init()
console.log(ring.length,ring)
run()
results()