Transport/Pool: Mark internal functions
This commit is contained in:
parent
b403cc3770
commit
709cda2618
|
@ -34,9 +34,9 @@ function Pool(network) {
|
|||
var self = this;
|
||||
|
||||
this.network = Networks.get(network) || Networks.defaultNetwork;
|
||||
this.connectedPeers = {};
|
||||
this.addrs = [];
|
||||
this.keepalive = false;
|
||||
this._connectedPeers = {};
|
||||
this._addrs = [];
|
||||
|
||||
this.on('peeraddr', function peerAddrEvent(peer, message) {
|
||||
var addrs = message.addresses;
|
||||
|
@ -47,28 +47,28 @@ function Pool(network) {
|
|||
if (addr.time <= 100000000 || addr.time > (now() + 10 * 60)) {
|
||||
addr.time = now() - 5 * 24 * 60 * 60;
|
||||
}
|
||||
this.addAddr(addr);
|
||||
this._addAddr(addr);
|
||||
}
|
||||
});
|
||||
|
||||
this.on('seed', function seedEvent(ips) {
|
||||
ips.forEach(function(ip) {
|
||||
self.addAddr({
|
||||
self._addAddr({
|
||||
ip: {
|
||||
v4: ip
|
||||
}
|
||||
});
|
||||
});
|
||||
if (self.keepalive) {
|
||||
self.fillConnections();
|
||||
self._fillConnections();
|
||||
}
|
||||
});
|
||||
|
||||
this.on('peerdisconnect', function peerDisconnectEvent(peer, addr) {
|
||||
self.deprioritizeAddr(addr);
|
||||
self.removeConnectedPeer(addr);
|
||||
self._deprioritizeAddr(addr);
|
||||
self._removeConnectedPeer(addr);
|
||||
if (self.keepalive) {
|
||||
self.fillConnections();
|
||||
self._fillConnections();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -94,10 +94,10 @@ Pool.PeerEvents = ['version', 'inv', 'getdata', 'ping', 'ping', 'addr',
|
|||
Pool.prototype.connect = function connect() {
|
||||
this.keepalive = true;
|
||||
var self = this;
|
||||
if (self.addrs.length === 0) {
|
||||
self.addAddrsFromSeeds();
|
||||
if (self._addrs.length === 0) {
|
||||
self._addAddrsFromSeeds();
|
||||
} else {
|
||||
self.fillConnections();
|
||||
self._fillConnections();
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
@ -108,51 +108,31 @@ Pool.prototype.connect = function connect() {
|
|||
*/
|
||||
Pool.prototype.disconnect = function disconnect() {
|
||||
this.keepalive = false;
|
||||
for (var i in this.connectedPeers) {
|
||||
this.connectedPeers[i].disconnect();
|
||||
for (var i in this._connectedPeers) {
|
||||
this._connectedPeers[i].disconnect();
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {Boolean} If the pool has peers (addrs) available to connect.
|
||||
*/
|
||||
Pool.prototype.isAvailable = function isAvailable() {
|
||||
if (this.addrs.length > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {Boolean} If there are peers connected.
|
||||
*/
|
||||
Pool.prototype.isConnected = function isConnected() {
|
||||
if (this.numberConnected() > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {Number} The number of peers currently connected.
|
||||
*/
|
||||
Pool.prototype.numberConnected = function numberConnected() {
|
||||
return Object.keys(this.connectedPeers).length;
|
||||
return Object.keys(this._connectedPeers).length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Will fill the conneted peers to the maximum amount.
|
||||
*/
|
||||
Pool.prototype.fillConnections = function fillConnections() {
|
||||
var length = this.addrs.length;
|
||||
Pool.prototype._fillConnections = function _fillConnections() {
|
||||
var length = this._addrs.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (this.numberConnected() >= Pool.MaxConnectedPeers) {
|
||||
break;
|
||||
}
|
||||
var addr = this.addrs[i];
|
||||
var addr = this._addrs[i];
|
||||
if (!addr.retryTime || now() > addr.retryTime) {
|
||||
this.connectPeer(addr);
|
||||
this._connectPeer(addr);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
@ -162,11 +142,11 @@ Pool.prototype.fillConnections = function fillConnections() {
|
|||
* Will remove a peer from the list of connected peers.
|
||||
* @param {Object} addr - An addr from the list of addrs
|
||||
*/
|
||||
Pool.prototype.removeConnectedPeer = function removeConnectedPeer(addr) {
|
||||
if (this.connectedPeers[addr.hash].status !== Peer.STATUS.DISCONNECTED) {
|
||||
this.connectedPeers[addr.hash].disconnect();
|
||||
Pool.prototype._removeConnectedPeer = function _removeConnectedPeer(addr) {
|
||||
if (this._connectedPeers[addr.hash].status !== Peer.STATUS.DISCONNECTED) {
|
||||
this._connectedPeers[addr.hash].disconnect();
|
||||
} else {
|
||||
delete this.connectedPeers[addr.hash];
|
||||
delete this._connectedPeers[addr.hash];
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
@ -175,7 +155,7 @@ Pool.prototype.removeConnectedPeer = function removeConnectedPeer(addr) {
|
|||
* Will connect a peer and add to the list of connected peers.
|
||||
* @param {Object} addr - An addr from the list of addrs
|
||||
*/
|
||||
Pool.prototype.connectPeer = function connectPeer(addr) {
|
||||
Pool.prototype._connectPeer = function _connectPeer(addr) {
|
||||
var self = this;
|
||||
|
||||
function addConnectedPeer(addr) {
|
||||
|
@ -194,10 +174,10 @@ Pool.prototype.connectPeer = function connectPeer(addr) {
|
|||
});
|
||||
});
|
||||
peer.connect();
|
||||
self.connectedPeers[addr.hash] = peer;
|
||||
self._connectedPeers[addr.hash] = peer;
|
||||
}
|
||||
|
||||
if (!this.connectedPeers[addr.hash]) {
|
||||
if (!this._connectedPeers[addr.hash]) {
|
||||
addConnectedPeer(addr);
|
||||
}
|
||||
|
||||
|
@ -209,15 +189,15 @@ Pool.prototype.connectPeer = function connectPeer(addr) {
|
|||
* of the array, and setting a retryTime
|
||||
* @param {Object} addr - An addr from the list of addrs
|
||||
*/
|
||||
Pool.prototype.deprioritizeAddr = function deprioritizeAddr(addr) {
|
||||
for (var i = 0; i < this.addrs.length; i++) {
|
||||
if (this.addrs[i].hash === addr.hash) {
|
||||
var middle = this.addrs[i];
|
||||
Pool.prototype._deprioritizeAddr = function _deprioritizeAddr(addr) {
|
||||
for (var i = 0; i < this._addrs.length; i++) {
|
||||
if (this._addrs[i].hash === addr.hash) {
|
||||
var middle = this._addrs[i];
|
||||
middle.retryTime = now() + Pool.RetrySeconds;
|
||||
var beginning = this.addrs.splice(0, i);
|
||||
var end = this.addrs.splice(i + 1, this.addrs.length);
|
||||
var beginning = this._addrs.splice(0, i);
|
||||
var end = this._addrs.splice(i + 1, this._addrs.length);
|
||||
var combined = beginning.concat(end);
|
||||
this.addrs = combined.concat([middle]);
|
||||
this._addrs = combined.concat([middle]);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
@ -227,20 +207,20 @@ Pool.prototype.deprioritizeAddr = function deprioritizeAddr(addr) {
|
|||
* Will add an addr to the beginning of the addrs array
|
||||
* @param {Object}
|
||||
*/
|
||||
Pool.prototype.addAddr = function addAddr(addr) {
|
||||
Pool.prototype._addAddr = function _addAddr(addr) {
|
||||
|
||||
// make a unique key
|
||||
addr.hash = sha256(new Buffer(addr.ip.v6 + addr.ip.v4 + addr.port)).toString('hex');
|
||||
|
||||
var length = this.addrs.length;
|
||||
var length = this._addrs.length;
|
||||
var exists = false;
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (this.addrs[i].hash === addr.hash) {
|
||||
if (this._addrs[i].hash === addr.hash) {
|
||||
exists = true;
|
||||
}
|
||||
}
|
||||
if (!exists) {
|
||||
this.addrs.unshift(addr);
|
||||
this._addrs.unshift(addr);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
@ -250,7 +230,7 @@ Pool.prototype.addAddr = function addAddr(addr) {
|
|||
* @param {String} seed - A domain name to resolve known peers
|
||||
* @param {Function} done
|
||||
*/
|
||||
Pool.prototype.addAddrsFromSeed = function addAddrsFromSeed(seed) {
|
||||
Pool.prototype._addAddrsFromSeed = function _addAddrsFromSeed(seed) {
|
||||
var self = this;
|
||||
dns.resolve(seed, function(err, ips) {
|
||||
if (err) {
|
||||
|
@ -271,11 +251,11 @@ Pool.prototype.addAddrsFromSeed = function addAddrsFromSeed(seed) {
|
|||
* Will add addrs to the list of addrs from network DNS seeds
|
||||
* @param {Function} done
|
||||
*/
|
||||
Pool.prototype.addAddrsFromSeeds = function addAddrsFromSeeds() {
|
||||
Pool.prototype._addAddrsFromSeeds = function _addAddrsFromSeeds() {
|
||||
var self = this;
|
||||
var seeds = this.network.dnsSeeds;
|
||||
seeds.forEach(function(seed) {
|
||||
self.addAddrsFromSeed(seed);
|
||||
self._addAddrsFromSeed(seed);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
@ -287,7 +267,7 @@ Pool.prototype.inspect = function inspect() {
|
|||
return '<Pool network: ' +
|
||||
this.network + ', connected: ' +
|
||||
this.numberConnected() + ', available: ' +
|
||||
this.addrs.length + '>';
|
||||
this._addrs.length + '>';
|
||||
};
|
||||
|
||||
module.exports = Pool;
|
||||
module.exports = Pool;
|
||||
|
|
|
@ -39,16 +39,16 @@ if (typeof(window) === 'undefined'){
|
|||
var pool = new Pool(Networks.livenet);
|
||||
pool.connect();
|
||||
pool.disconnect();
|
||||
pool.addrs.length.should.equal(3);
|
||||
pool._addrs.length.should.equal(3);
|
||||
stub.restore();
|
||||
});
|
||||
|
||||
it('should not discover peers via dns', function() {
|
||||
var pool = new Pool();
|
||||
pool.addAddr({ip: '10.10.10.1'});
|
||||
pool._addAddr({ip: {v4: '10.10.10.1'}});
|
||||
pool.connect();
|
||||
pool.disconnect();
|
||||
pool.addrs.length.should.equal(1);
|
||||
pool._addrs.length.should.equal(1);
|
||||
});
|
||||
|
||||
it('should add new addrs as they are announced over the network', function(done) {
|
||||
|
@ -68,20 +68,20 @@ if (typeof(window) === 'undefined'){
|
|||
|
||||
var pool = new Pool();
|
||||
|
||||
pool.addAddr({ip: {v4: 'localhost'}});
|
||||
pool._addAddr({ip: {v4: 'localhost'}});
|
||||
|
||||
// listen for the event
|
||||
pool.on('peeraddr', function(peer, message) {
|
||||
pool.addrs.length.should.equal(502);
|
||||
pool._addrs.length.should.equal(502);
|
||||
|
||||
// restore stubs
|
||||
peerConnectStub.restore();
|
||||
peerMessageStub.restore();
|
||||
|
||||
for (var i = 0; i < pool.addrs.length; i++) {
|
||||
should.exist(pool.addrs[i].hash);
|
||||
should.exist(pool.addrs[i].ip);
|
||||
should.exist(pool.addrs[i].ip.v4);
|
||||
for (var i = 0; i < pool._addrs.length; i++) {
|
||||
should.exist(pool._addrs[i].hash);
|
||||
should.exist(pool._addrs[i].ip);
|
||||
should.exist(pool._addrs[i].ip.v4);
|
||||
}
|
||||
|
||||
// done
|
||||
|
|
Loading…
Reference in New Issue