-
Notifications
You must be signed in to change notification settings - Fork 23
/
sim.js
executable file
·321 lines (260 loc) · 8.4 KB
/
sim.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
/*
MinCen Simulation
*/
var net = require("./network"),
peermgr = require("./peermgr"),
btc = require("./btc"),
client = new net.Client()
btc.Blockchain.Block.prototype.target_avg_between_blocks = 60 * 1000; // one minute blocks
btc.Blockchain.GenesisBlock.difficulty = 3700;
client.use(peermgr)
client.use(btc)
var hashrate = 1;
client.init(function() {
var self = this;
// mincen client specific stuff
this.mapMasters = {};
var mapMasters = this.mapMasters;
mapMasters[this.id] = {num:10, last:-1};
var shares = [];
var shareh = -1;
var master = this.id;
var signedByPrev = {};
// pick a master given the share record of each master for the last 10 blocks
var pickMaster = function() {
var best = -1;
var bestn = 0;
for (m in mapMasters) {
var _master = mapMasters[m];
if (_master.last < (self.miner.staged.h - 10)) {
delete mapMasters[m];
continue;
}
if (_master.num > bestn) {
bestn = _master.num;
best = m;
}
}
// select ourselves if we can't find one
if (best == -1) {
best = self.id;
}
// if the master changed, reset the sharelist
if (master != best) {
shares = [];
master = best;
}
}
// replace the restage function (called after a block is found, or another node finds a block)
this.miner.restage = function() {
if (self.miner.staged.h != self.miner.shareh) {
shareh = self.miner.staged.h;
shares = [];
}
self.miner.difficulty = self.miner.staged.difficulty;
if (self.miner.enabled) {
self.miner.stopMining()
self.miner.startMining()
}
}
// replace the miner:success function, handling the block we mined
self.on("miner:success", function(from, b) {
b.time = self.now();
b.transactions = self.mempool.getList();
pickMaster();
b.master = master; // delegate to the master we chose
b.winner = Math.random() < (1/16); // 1/16 chance of being the block winner
b.shares = shares.slice(0, 15); // 16 shares max
if (b.winner) {
// we won the race, let's get our block signed
self.log("WINNER (" + b.id + ") at h=" + b.h);
if (b.master == self.id) {
// sign it ourselves because we're the master
self.log("<span style='color:red'>MASTER SIGN WINNER</span> (" + b.id + ") at h=" + b.h)
self.inventory.createObj("block", b);
self.inventory.relay(b.id, true);
self.blockchain.chainstate.enter(b);
if (!(self.id in mapMasters)) {
mapMasters[self.id] = {num:0,last:0};
}
mapMasters[self.id].num++;
mapMasters[self.id].last = b.h;
signedByPrev[b.prev] = true;
} else {
// ship off an unsignedblock to get it signed by the master
self.inventory.createObj("unsignedblock", {id:"unsigned_" + b.id, b:b})
self.inventory.relay("unsigned_" + b.id, true);
}
} else {
// we found a share, let's get it in the hands of everyone else on our master
self.log("SHARE (" + b.id + ") at h=" + b.h);
if (b.master == self.id) {
// sign it ourselves because we're the master
self.log("MASTER SIGN SHARE (" + b.id + ") at h=" + b.h)
self.inventory.createObj("shareack", {id:"ack_" + b.id, share:b, master:self.id});
self.inventory.relay("ack_" + b.id, true);
} else {
// ship it off to get signed
self.inventory.createObj("share", {id:"share_" + b.id, share:b});
self.inventory.relay("share_" + b.id, true);
}
}
return false; // hook the default miner code
}, this.miner)
// handling the share inventory object being sent over the wire
self.on("obj:share", function(from, share) {
if (share.share.master == self.id) {
// sign the share we just received
// TODO: strictly require the transactions to be in our UTXO
self.log("MASTER SIGN SHARE (" + share.share.id + ") at h=" + share.share.h)
// that's us! sign it
self.inventory.createObj("shareack", {id:"ack_" + share.share.id, share:share.share, master:self.id});
self.inventory.relay("ack_" + share.share.id, true);
} else {
if (share.share.h == this.staged.h) {
// this share interests the network, propagate it so it reaches a master
self.inventory.relay(share.id, true);
}
}
}, this.miner)
// handling the shareack inventory object being sent over the wire
self.on("obj:shareack", function(from, ack) {
if (ack.share.h == this.staged.h) {
// this shareack interests the network, propagate it so it reaches everyone who delegates to this master
self.inventory.relay(ack.id, true);
var share = ack.share;
if (!(share.master in mapMasters)) {
mapMasters[share.master] = {num:0,last:0};
}
mapMasters[share.master].num++;
mapMasters[share.master].last = share.h;
// do i delegate to this master?
if (share.master == master) {
shares.push(share);
}
// pick a new master given the information, if necessary
pickMaster();
}
}, this.miner)
// handling the unsignedblock inventory object being sent over the wire
self.on("obj:unsignedblock", function(from, ub) {
if (ub.b.master == self.id) {
// that's us! sign it, if we haven't already signed another block with this prev
if (!(ub.b.prev in signedByPrev)) {
self.log("<span style='color:red'>MASTER SIGN WINNER</span> (" + ub.b.id + ") at h=" + ub.b.h);
signedByPrev[ub.b.prev] = true;
self.inventory.createObj("block", ub.b);
self.inventory.relay(ub.b.id, true);
self.blockchain.chainstate.enter(ub.b);
self.miner.restage();
}
} else {
// just relay the unsigned block
self.inventory.relay(ub.id, true);
}
}, this.miner)
////////////////////////////////////////////////
var myHashrate = (hashrate / 2) * Math.random();
hashrate -= myHashrate;
this.mine(myHashrate);
////////////////////////////////////////////////
// adjust peering relationships based on who sends us blocks!
var funpeers = {};
this.on("peermgr:connect", function(id) {
funpeers[id] = {blocks:0};
return true;
})
this.on("peermgr:disconnect", function(id) {
delete funpeers[id];
return true;
})
this.on("obj:block", function(from, b) {
funpeers[from].blocks++;
return true;
})
this.tick(100 * 1000, function() {
//if (this.peermgr.numActive() != this.peermgr.maxpeers) {
// return true;
//}
// every 100 seconds boot off a poorly performing peer
var worst_n = Infinity;
var worst = null;
var t = 0;
for (var p in funpeers) {
t += funpeers[p].blocks;
if (funpeers[p].blocks < worst_n) {
worst_n = funpeers[p].blocks;
worst = p;
}
}
if (t < 20) // we want a sample of 20 blocks
return true;
if (worst != null) {
this.peermgr.disconnect(worst);
}
for (var p in funpeers) {
funpeers[p].blocks = 0;
}
})
})
net.add(100, client)
function getTimeWindow(start, size) {
var timeWindow = []
var cur = start;
var last = -1;
while (cur && timeWindow.length < size) {
if (last >= 0) {
var timeSince = (last - cur.time)/1000; // in seconds
timeWindow.push(timeSince);
}
last = cur.time;
cur = cur._prev();
}
return timeWindow;
}
function getAverage(timeWindow) {
var average = 0;
if (timeWindow.length > 0) {
timeWindow.forEach(function(n) {
average += n;
})
average /= timeWindow.length;
}
return average;
}
var graphData = [];
var checkpoint = -1;
var shortWindow = 10;
var longWindow = 100;
if (net.visualizer) {
net.check(1000 * 1000, function() {
var sw = parseInt($("#shortwindow").val());
var lw = parseInt($("#longwindow").val());
if (sw != shortWindow || lw != longWindow) {
shortWindow = sw;
longWindow = lw;
graphData = [];
checkpoint = -1;
}
var head = net.nodes[90].blockchain.chainstate.head;
var cur = head;
var timeWindow = getTimeWindow(cur, head.h - checkpoint + longWindow);
var temp = [];
cur = head;
var last = -1;
while (cur && cur.h >= checkpoint) {
if (last >= 0) {
var timeSince = (last - cur.time)/1000; // in seconds
var avShort = getAverage(timeWindow.slice(head.h-cur.h, head.h-cur.h+shortWindow));
var avLong = getAverage(timeWindow.slice(head.h-cur.h, head.h-cur.h+longWindow));
temp.push([cur.h, cur.difficulty, avShort, avLong]);
}
last = cur.time;
cur = cur._prev();
}
graphData = temp.concat(graphData);
checkpoint = head.h;
net.visualizer.drawGraph(graphData);
})
}
net.run(Infinity)