forked from ebfull/simbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeermgr.js
executable file
·313 lines (257 loc) · 8.22 KB
/
peermgr.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
/*
The PeerMgr is the inter-client behavior middleware for establishing connections
with other peers, thus forming a network.
(This peer manager is not intended to accurately mimic bitcoin yet.)
Current behavior: every 1 second it checks to see if it has 'maxpeers'. If not, it attempts
either to connect to an archived addr, or get addresses from other nodes it has contacted.
Initially, the only archived addr is the bootstrap node, 0. Once the node has received
maxpeers, it stops ticking.
*/
function PeerState(id, lastmessage) {
this.id = id;
this.lastmessage = lastmessage;
this.active = false;
this.locked = true;
this.msgqueue = [];
}
function PeerMgr(self) {
// attach to nodestate 'peers' property
// self.peers = this;
self.peermgr = this;
this.freeze = false;
this.ticking = false;
this.peers = {}; // current established or attempted connections
this.numpeers = 0; // the number of peers we have
this.maxpeers = 8; // the max number of peers we can have
if (self.id != 0)
this.nodearchive = [new PeerState(0, self.now())]; // a node archived, initialized with a bootstrap node
else
this.nodearchive = []
var peerTick = function() {
if (!this.freeze && (this.numpeers < this.maxpeers)) {
// we need more peers
// let's try connecting to a peer in our nodearchive, if there are any
if (this.nodearchive.length) {
var p = this.nodearchive.shift();
this.nodearchive.push(p);
// try connecting to this node, fails if we're already trying/connected
this.connect(p);
}
// if we have (active) connections, ask them for new peers
if (this.numpeers) {
var randomPeer = this.peers[Object.keys(this.peers)[Math.floor(Math.random() * Object.keys(this.peers).length)]]
if (randomPeer.active) {
this.getpeers(randomPeer.id)
}
}
if (self.now() > 1000 * 1000) // after 1000 seconds, let's tick every 5 seconds instead
return 5000;
} else {
self.peermgr.ticking = false;
return false; // no more ticking necessary
}
}
var startTicking = function() {
if (!self.peermgr.ticking) {
self.peermgr.ticking = true;
self.tick(1000, peerTick, self.peermgr);
}
}
this.numActive = function() {
var n = 0;
for (var p in this.peers) {
if (this.peers[p].active) {
n++;
}
}
return n;
}
this.send = function(to, name, msg) {
if (this.peers[to].active) {
if (this.peers[to].locked) {
this.peers[to].msgqueue.push({name:name,obj:msg})
} else {
self.send(this.peers[to].id, "__peermsg", {name:name,obj:msg})
}
}
}
this.each = function(cb) {
for (var p in this.peers) {
if (this.peers[p].active)
cb.call(self, p)
}
}
// sends a message to all active peers
this.broadcast = function(name, msg) {
for (var p in this.peers) {
this.send(p, name, msg)
}
}
// request peers from a remote node
this.getpeers = function(p) {
self.send(p, 'getpeers', {});
}
// send a portion of our archived node list
this.sendpeers = function(p) {
var someNodes = this.nodearchive.slice(0, 15)
if (someNodes.length == 0) {
var randomPeer = this.peers[Object.keys(this.peers)[Math.floor(Math.random() * Object.keys(this.peers).length)]]
if (randomPeer.active)
someNodes = [randomPeer]
}
self.send(p, 'peerlist', someNodes);
}
// connect to a remote node (if we haven't already tried)
this.connect = function(p) {
if (self.id == p.id)
return; // can't connect to ourselves!
if (typeof this.peers[p.id] == "undefined") {
this.peers[p.id] = p;
this.numpeers += 1;
self.send(p.id, 'connect', {})
}
}
// disconnect from a remote node
this.disconnect = function(p) {
if (typeof this.peers[p] != "undefined") {
var peer = this.peers[p];
delete this.peers[p];
//if (peer.active) {
self.send(p, 'disconnect', {})
self.disconnect(p);
startTicking();
//}
this.nodearchive.push(peer);
this.numpeers -= 1;
self.handle(p, "peermgr:disconnect", p)
}
}
// accept a remote node's connection
this.accept = function(p) {
p.locked = true; // wait for an ack
self.send(p.id, 'accept', {})
}
// reject a remote node's connection
this.reject = function(p) {
var someNodes = this.nodearchive.slice(0, 15)
if (someNodes.length == 0) {
var randomPeer = this.peers[Object.keys(this.peers)[Math.floor(Math.random() * Object.keys(this.peers).length)]]
if (randomPeer.active)
someNodes = [this.randomPeer]
}
self.send(p.id, 'reject', someNodes)
}
this.onAck = function(from, o) {
if (typeof this.peers[from] != "undefined") {
this.peers[from].locked = false;
this.peers[from].lastmessage = self.now()
if (this.peers[from].msgqueue.length > 0) {
var domsg = this.peers[from].msgqueue.shift();
this.peers[from].locked = true;
self.send(this.peers[from].id, "__peermsg", {name:domsg.name,obj:domsg.obj})
}
}
}
// processes a received message from another peer
this.onReceive = function(from, o) {
if (typeof this.peers[from] != "undefined" && this.peers[from].active) {
self.send(from, 'ack', {})
self.handle(from, o.name, o.obj)
this.peers[from].lastmessage = self.now();
}
}
// receive a peerlist message
this.onPeerlist = function(from, obj) {
// are we connected to this peer?
if (this.peers[from] != "undefined") {
// add these peers to our nodearchive
// if we don't have them already
for (var i=0;i<obj.length;i++) {
var candidate = obj[i];
// remove redundant existing nodearchive objects
for (var k=0;k<this.nodearchive.length;k++) {
if (this.nodearchive[k].id == candidate.id) {
this.nodearchive.splice(k, 1);
}
}
this.nodearchive.push(new PeerState(candidate.id, self.now()))
}
}
}
// receive a getpeers message
this.onGetpeers = function(from, obj) {
// are we connected to this peer?
if (this.peers[from] != "undefined") {
this.sendpeers(from)
}
}
// receive a reject message
this.onReject = function(from, obj) {
if (typeof this.peers[from] != "undefined") {
this.onPeerlist(from, obj) // process rejection peerlist
this.disconnect(from) // cya
}
}
// receive an accept message
this.onAccept = function(from, obj) {
if (typeof this.peers[from] != "undefined") {
// remove them from our nodearchive
for (var i=0;i<this.nodearchive.length;i++) {
if (this.nodearchive[i].id == from) {
this.nodearchive.splice(i, 1)
}
}
// set connection active
this.peers[from].lastmessage = self.now();
this.peers[from].active = true;
self.send(from, 'ack', {})
// notify Network of connection
self.connect(from);
self.handle(from, "peermgr:connect", from)
}
}
// receive a disconnect message
this.onDisconnect = function(from, obj) {
this.disconnect(from)
}
this.onConnect = function(from, obj) {
// are you already trying to connect?
if (typeof this.peers[from] != "undefined")
return; // do nothing
if (this.numpeers < this.maxpeers) {
// sure, we'll accept you. we need a peer.
// are we already connected to you?
this.peers[from] = new PeerState(from, self.now())
this.numpeers += 1;
this.onAccept(from, obj);
this.accept(this.peers[from])
return;
}
// otherwise, reject this node.
// remove node from nodearchive in any redundant locations
for (var i=0;i<this.nodearchive.length;i++) {
if (this.nodearchive[i].id == from) {
this.nodearchive.splice(i, 1)
}
}
var rejectPeer = new PeerState(from, self.now());
// add back to nodearchive in the front
this.nodearchive.push(rejectPeer)
// send node a rejection message
this.reject(rejectPeer);
}
//
// attach to the node
//
// tick that runs every 1 second
startTicking();
self.on("connect", this.onConnect, this);
self.on("accept", this.onAccept, this);
self.on("reject", this.onReject, this);
self.on("disconnect", this.onDisconnect, this);
self.on("peerlist", this.onPeerlist, this);
self.on("getpeers", this.onGetpeers, this);
self.on("ack", this.onAck, this);
self.on("__peermsg", this.onReceive, this);
}
module.exports = PeerMgr;