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