Skip to content

Commit 58c3b68

Browse files
committed
Fixed bugs in peermgr, network, inventory relaying on connect.
1 parent 028ff42 commit 58c3b68

File tree

4 files changed

+99
-11
lines changed

4 files changed

+99
-11
lines changed

btc/inventory.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function Inventory(self) {
7474
for (var p in this.tellPeer) {
7575
var invPacket = this.tellPeer[p];
7676

77-
if (Object.keys(invPacket) != 0) {
77+
if (Object.keys(invPacket).length != 0) {
7878
doneSomething = true;
7979
this.__send_inv(p, invPacket)
8080

@@ -225,17 +225,24 @@ function Inventory(self) {
225225
}
226226

227227
self.on("peermgr:connect", function(from) {
228+
var relaySet = this.objects.find({status:"relay"});
229+
228230
this.peerHas[from] = {};
229231
this.tellPeer[from] = {};
230232
this.mapAskFor[from] = {};
231233

232-
// todo: send full inventory
234+
relaySet.forEach(function(o) {
235+
this.tellPeer[from][o.id] = o.type;
236+
}, this)
237+
238+
this.addTick();
233239
}, this)
234240

235241
self.on("peermgr:disconnect", function(from) {
236242
delete this.peerHas[from]
237243
delete this.tellPeer[from]
238244
delete this.mapAlreadyAskedFor[from]
245+
delete this.mapAskFor[from];
239246
}, this)
240247

241248
self.on("inv", this.onInv, this)

network.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function NodeTickEvent(delay, f, ctx) {
100100

101101
this.run = function(network) {
102102
var newDelay;
103-
if (newDelay = f.call(ctx) !== false) {
103+
if ((newDelay = f.call(ctx)) !== false) {
104104
if (typeof newDelay == "number")
105105
this.delay = newDelay;
106106

peermgr.js

+39-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function PeerMgr(self) {
2323
// self.peers = this;
2424
self.peermgr = this;
2525

26+
this.ticking = false;
2627
this.peers = {}; // current established or attempted connections
2728
this.numpeers = 0; // the number of peers we have
2829
this.maxpeers = 8; // the max number of peers we can have
@@ -33,8 +34,6 @@ function PeerMgr(self) {
3334

3435
var peerTick = function() {
3536
if (this.numpeers < this.maxpeers) {
36-
if (self.now() > 1000 * 1000) // after 1000 seconds, let's not bother trying to form new connections and assume the network is in a steady state
37-
return false;
3837
// we need more peers
3938

4039
// let's try connecting to a peer in our nodearchive, if there are any
@@ -54,11 +53,36 @@ function PeerMgr(self) {
5453
this.getpeers(randomPeer.id)
5554
}
5655
}
56+
57+
if (self.now() > 1000 * 1000) // after 1000 seconds, let's tick every 5 seconds instead
58+
return 5000;
5759
} else {
60+
_ticking = false;
61+
self.peermgr.ticking = false;
5862
return false; // no more ticking necessary
5963
}
6064
}
6165

66+
var _ticking = false;
67+
var startTicking = function() {
68+
if (!_ticking) {
69+
_ticking = true;
70+
self.peermgr.ticking = true;
71+
72+
self.tick(1000, peerTick, self.peermgr);
73+
}
74+
}
75+
76+
this.numActive = function() {
77+
var n = 0;
78+
for (var p in this.peers) {
79+
if (this.peers[p].active) {
80+
n++;
81+
}
82+
}
83+
return n;
84+
}
85+
6286
this.send = function(to, name, msg) {
6387
if (this.peers[to].active) {
6488
if (this.peers[to].locked) {
@@ -115,8 +139,16 @@ function PeerMgr(self) {
115139
// disconnect from a remote node
116140
this.disconnect = function(p) {
117141
if (typeof this.peers[p] != "undefined") {
118-
this.nodearchive.push(this.peers[p]);
119-
delete this.peers[p]
142+
var peer = this.peers[p];
143+
delete this.peers[p];
144+
145+
//if (peer.active) {
146+
self.send(p, 'disconnect', {})
147+
self.disconnect(p);
148+
startTicking();
149+
//}
150+
151+
this.nodearchive.push(peer);
120152
this.numpeers -= 1;
121153
self.handle(p, "peermgr:disconnect", p)
122154
}
@@ -179,7 +211,7 @@ function PeerMgr(self) {
179211
}
180212
}
181213

182-
this.nodearchive.unshift(new PeerState(candidate.id, self.now()))
214+
this.nodearchive.push(new PeerState(candidate.id, self.now()))
183215
}
184216
}
185217
}
@@ -257,7 +289,7 @@ function PeerMgr(self) {
257289
var rejectPeer = new PeerState(from, self.now());
258290

259291
// add back to nodearchive in the front
260-
this.nodearchive.unshift(rejectPeer)
292+
this.nodearchive.push(rejectPeer)
261293

262294
// send node a rejection message
263295
this.reject(rejectPeer);
@@ -268,7 +300,7 @@ function PeerMgr(self) {
268300
//
269301

270302
// tick that runs every 1 second
271-
self.tick(1000, peerTick, this);
303+
startTicking();
272304

273305
self.on("connect", this.onConnect, this);
274306
self.on("accept", this.onAccept, this);

sim.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ btc.Blockchain.GenesisBlock.difficulty = 3700;
1313
client.use(peermgr)
1414
client.use(btc)
1515

16+
var hashrate = 1;
17+
1618
client.init(function() {
1719
var self = this;
1820

@@ -184,7 +186,54 @@ client.init(function() {
184186
}
185187
}, this.miner)
186188
////////////////////////////////////////////////
187-
this.mine(0.01);
189+
var myHashrate = (hashrate / 2) * Math.random();
190+
hashrate -= myHashrate;
191+
this.mine(myHashrate);
192+
////////////////////////////////////////////////
193+
// adjust peering relationships based on who sends us blocks!
194+
195+
var funpeers = {};
196+
this.on("peermgr:connect", function(id) {
197+
funpeers[id] = {blocks:0};
198+
return true;
199+
})
200+
this.on("peermgr:disconnect", function(id) {
201+
delete funpeers[id];
202+
return true;
203+
})
204+
this.on("obj:block", function(from, b) {
205+
funpeers[from].blocks++;
206+
return true;
207+
})
208+
this.tick(100 * 1000, function() {
209+
if (this.peermgr.numActive() != this.peermgr.maxpeers) {
210+
return true;
211+
}
212+
// every 100 seconds boot off a poorly performing peer
213+
var worst_n = Infinity;
214+
var worst = null;
215+
216+
var t = 0;
217+
218+
for (var p in funpeers) {
219+
t += funpeers[p].blocks;
220+
if (funpeers[p].blocks < worst_n) {
221+
worst_n = funpeers[p].blocks;
222+
worst = p;
223+
}
224+
}
225+
226+
if (t < 20) // we want a sample of 20 blocks
227+
return true;
228+
229+
if (worst != null) {
230+
this.peermgr.disconnect(worst);
231+
}
232+
233+
for (var p in funpeers) {
234+
funpeers[p].blocks = 0;
235+
}
236+
})
188237
})
189238

190239
net.add(100, client)

0 commit comments

Comments
 (0)