diff --git a/etc/sample.conf b/etc/sample.conf index 6d73b57e..ab89b364 100644 --- a/etc/sample.conf +++ b/etc/sample.conf @@ -42,8 +42,8 @@ bip151: true # ignore-discovery: false # port: 8333 listen: true -max-peers: 8 -max-leeches: 30 +max-outbound: 8 +max-inbound: 30 bip150: false identity-key: 74b4147957813b62cc8987f2b711ddb31f8cb46dcbf71502033da66053c8780a auth-peers: ./authorized-peers diff --git a/lib/http/server.js b/lib/http/server.js index 7eb6dd4e..ac70f68c 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -571,6 +571,7 @@ HTTPServer.prototype._init = function _init() { this.get('/', function(req, res, send, next) { var totalTX = this.mempool ? this.mempool.totalTX : 0; var size = this.mempool ? this.mempool.getSize() : 0; + var loader = this.pool.peers.load ? 1 : 0; send(200, { version: constants.USER_VERSION, @@ -579,9 +580,9 @@ HTTPServer.prototype._init = function _init() { network: this.network.type, height: this.chain.height, tip: this.chain.tip.rhash, - peers: this.pool.peers.regular.length + (this.pool.peers.load ? 1 : 0), - pendingPeers: this.pool.peers.pending.length, - leeches: this.pool.peers.leeches.length, + outbound: this.pool.peers.outbound.length + loader, + pending: this.pool.peers.pending.length, + inbound: this.pool.peers.inbound.length, progress: this.chain.getProgress(), mempoolTX: totalTX, mempoolSize: size, diff --git a/lib/net/pool.js b/lib/net/pool.js index 36f8f20a..b0b90386 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -37,7 +37,7 @@ var request = require('../http/request'); * @param {Object} options * @param {Chain} options.chain * @param {Mempool?} options.mempool - * @param {Number?} [options.maxPeers=8] - Maximum number of peers. + * @param {Number?} [options.maxOutbound=8] - Maximum number of peers. * @param {Boolean?} options.spv - Do an SPV sync. * @param {Boolean?} options.relay - Whether to ask * for relayed transactions. @@ -107,8 +107,8 @@ function Pool(options) { this.port = this.network.port; this.server = null; - this.maxPeers = 8; - this.maxLeeches = 8; + this.maxOutbound = 8; + this.maxInbound = 8; this.connected = false; this.uid = 0; this.createServer = null; @@ -181,11 +181,11 @@ Pool.prototype._initOptions = function _initOptions() { this.address.services = this.services; this.address.setPort(this.port); - if (this.options.maxPeers != null) - this.maxPeers = this.options.maxPeers; + if (this.options.maxOutbound != null) + this.maxOutbound = this.options.maxOutbound; - if (this.options.maxLeeches != null) - this.maxLeeches = this.options.maxLeeches; + if (this.options.maxInbound != null) + this.maxInbound = this.options.maxInbound; this.createServer = this.options.createServer; this.proxyServer = this.options.proxyServer; @@ -273,6 +273,10 @@ Pool.prototype._init = function _init() { self.emit('orphan', block, height); }); + this.chain.on('reset', function() { + self.forceSync(); + }); + this.chain.on('full', function() { self.stopTimeout(); self.stopInterval(); @@ -307,7 +311,7 @@ Pool.prototype._open = co(function* _open() { else yield this.chain.open(); - this.logger.info('Pool loaded (maxpeers=%d).', this.maxPeers); + this.logger.info('Pool loaded (maxpeers=%d).', this.maxOutbound); if (this.identityKey) { key = ec.publicKeyCreate(this.identityKey, true); @@ -463,8 +467,8 @@ Pool.prototype._handleLeech = function _handleLeech(socket) { addr = NetworkAddress.fromSocket(socket, this.network); - if (this.peers.leeches.length >= this.maxLeeches) { - this.logger.debug('Ignoring leech: too many leeches (%s).', addr.hostname); + if (this.peers.inbound.length >= this.maxInbound) { + this.logger.debug('Ignoring leech: too many inbound (%s).', addr.hostname); socket.destroy(); return; } @@ -676,8 +680,28 @@ Pool.prototype.sync = function sync() { if (this.peers.load) this.peers.load.trySync(); - for (i = 0; i < this.peers.regular.length; i++) - this.peers.regular[i].trySync(); + for (i = 0; i < this.peers.outbound.length; i++) + this.peers.outbound[i].trySync(); +}; + +/** + * Force sending a sync to each peer. + * @private + */ + +Pool.prototype.forceSync = function forceSync() { + var i, peer; + + if (this.peers.load) { + this.peers.load.syncSent = false; + this.peers.load.trySync(); + } + + for (i = 0; i < this.peers.outbound.length; i++) { + peer = this.peers.outbound[i]; + peer.syncSent = false; + peer.trySync(); + } }; /** @@ -701,8 +725,8 @@ Pool.prototype.stopSync = function stopSync() { if (this.peers.load) this.peers.load.syncSent = false; - for (i = 0; i < this.peers.regular.length; i++) - this.peers.regular[i].syncSent = false; + for (i = 0; i < this.peers.outbound.length; i++) + this.peers.outbound[i].syncSent = false; }; /** @@ -995,8 +1019,8 @@ Pool.prototype.sendMempool = function sendMempool() { if (this.peers.load) this.peers.load.sendMempool(); - for (i = 0; i < this.peers.regular.length; i++) - this.peers.regular[i].sendMempool(); + for (i = 0; i < this.peers.outbound.length; i++) + this.peers.outbound[i].sendMempool(); }; /** @@ -1010,11 +1034,11 @@ Pool.prototype.sendAlert = function sendAlert(alert) { if (this.peers.load) this.peers.load.sendAlert(alert); - for (i = 0; i < this.peers.regular.length; i++) - this.peers.regular[i].sendAlert(alert); + for (i = 0; i < this.peers.outbound.length; i++) + this.peers.outbound[i].sendAlert(alert); - for (i = 0; i < this.peers.leeches.length; i++) - this.peers.leeches[i].sendAlert(alert); + for (i = 0; i < this.peers.inbound.length; i++) + this.peers.inbound[i].sendAlert(alert); }; /** @@ -1032,7 +1056,7 @@ Pool.prototype.createPeer = function createPeer(addr, socket) { if (!peer.outbound) return; - // Attempt to promote from pending->regular + // Attempt to promote from pending->outbound self.peers.promote(peer); // If we don't have an ack'd loader yet, use this peer. @@ -1398,7 +1422,7 @@ Pool.prototype.addLeech = function addLeech(addr, socket) { }; /** - * Create a regular non-loader peer. These primarily + * Create a outbound non-loader peer. These primarily * exist for transaction relaying. * @private */ @@ -1440,10 +1464,10 @@ Pool.prototype.fillPeers = function fillPeers() { var i; this.logger.debug('Refilling peers (%d/%d).', - this.peers.all.length - this.peers.leeches.length, - this.maxPeers); + this.peers.all.length - this.peers.inbound.length, + this.maxOutbound); - for (i = 0; i < this.maxPeers - 1; i++) + for (i = 0; i < this.maxOutbound - 1; i++) this.addPeer(); }; @@ -1527,8 +1551,8 @@ Pool.prototype.updateWatch = function updateWatch() { if (self.peers.load) self.peers.load.updateWatch(); - for (i = 0; i < self.peers.regular.length; i++) - self.peers.regular[i].updateWatch(); + for (i = 0; i < self.peers.outbound.length; i++) + self.peers.outbound[i].updateWatch(); }, 50); }; @@ -1674,7 +1698,6 @@ Pool.prototype.scheduleRequests = co(function* scheduleRequests(peer) { this.scheduled = true; yield this.chain.onDrain(); - yield co.wait(); this.sendRequests(peer); this.scheduled = false; @@ -1782,8 +1805,8 @@ Pool.prototype.announce = function announce(msg) { if (this.peers.load) this.peers.load.tryAnnounce(msg); - for (i = 0; i < this.peers.regular.length; i++) - this.peers.regular[i].tryAnnounce(msg); + for (i = 0; i < this.peers.outbound.length; i++) + this.peers.outbound[i].tryAnnounce(msg); }; /** @@ -1799,8 +1822,8 @@ Pool.prototype.setFeeRate = function setFeeRate(rate) { if (this.peers.load) this.peers.load.sendFeeRate(rate); - for (i = 0; i < this.peers.regular.length; i++) - this.peers.regular[i].sendFeeRate(rate); + for (i = 0; i < this.peers.outbound.length; i++) + this.peers.outbound[i].sendFeeRate(rate); }; /** @@ -1957,11 +1980,11 @@ Pool.prototype.getIP2 = co(function* getIP2() { function PeerList(pool) { this.pool = pool; // Peers that are loading blocks themselves - this.regular = []; + this.outbound = []; // Peers that are still connecting this.pending = []; // Peers that connected to us - this.leeches = []; + this.inbound = []; // Peers that are loading block ids this.load = null; // All peers @@ -1985,7 +2008,7 @@ PeerList.prototype.addPending = function addPending(peer) { }; PeerList.prototype.addLeech = function addLeech(peer) { - this.leeches.push(peer); + this.inbound.push(peer); this.all.push(peer); assert(!this.map[peer.hostname]); this.map[peer.hostname] = peer; @@ -1993,13 +2016,13 @@ PeerList.prototype.addLeech = function addLeech(peer) { PeerList.prototype.promote = function promote(peer) { if (util.binaryRemove(this.pending, peer, compare)) - util.binaryInsert(this.regular, peer, compare); + util.binaryInsert(this.outbound, peer, compare); }; PeerList.prototype.remove = function remove(peer) { util.binaryRemove(this.pending, peer, compare); - util.binaryRemove(this.regular, peer, compare); - util.binaryRemove(this.leeches, peer, compare); + util.binaryRemove(this.outbound, peer, compare); + util.binaryRemove(this.inbound, peer, compare); util.binaryRemove(this.all, peer, compare); assert(this.map[peer.hostname]); @@ -2016,7 +2039,7 @@ PeerList.prototype.demoteLoader = function demoteLoader() { assert(peer); this.load = null; if (peer.ack) - util.binaryInsert(this.regular, peer, compare); + util.binaryInsert(this.outbound, peer, compare); else util.binaryInsert(this.pending, peer, compare); }; @@ -2030,7 +2053,7 @@ PeerList.prototype.repurpose = function repurpose(peer) { this.demoteLoader(); r1 = util.binaryRemove(this.pending, peer, compare); - r2 = util.binaryRemove(this.regular, peer, compare); + r2 = util.binaryRemove(this.outbound, peer, compare); assert(r1 || r2); @@ -2038,11 +2061,11 @@ PeerList.prototype.repurpose = function repurpose(peer) { }; PeerList.prototype.isFull = function isFull() { - return this.size() >= this.pool.maxPeers - 1; + return this.size() >= this.pool.maxOutbound - 1; }; PeerList.prototype.size = function size() { - return this.regular.length + this.pending.length; + return this.outbound.length + this.pending.length; }; PeerList.prototype.get = function get(addr) { @@ -2055,7 +2078,7 @@ PeerList.prototype.destroy = function destroy() { if (this.load) this.load.destroy(); - peers = this.regular.slice(); + peers = this.outbound.slice(); for (i = 0; i < peers.length; i++) peers[i].destroy(); @@ -2065,7 +2088,7 @@ PeerList.prototype.destroy = function destroy() { for (i = 0; i < peers.length; i++) peers[i].destroy(); - peers = this.leeches.slice(); + peers = this.inbound.slice(); for (i = 0; i < peers.length; i++) peers[i].destroy(); diff --git a/lib/node/config.js b/lib/node/config.js index cc3f152f..7ea737ee 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -184,8 +184,8 @@ config.parseData = function parseData(data, prefix, dirname) { options.identityKey = key(data.identitykey); options.proxyServer = str(data.proxyserver); options.preferredSeed = str(data.preferredseed); - options.maxPeers = num(data.maxpeers); - options.maxLeeches = num(data.maxleeches); + options.maxOutbound = num(data.maxoutbound); + options.maxInbound = num(data.maxinbound); options.ignoreDiscovery = bool(data.ignorediscovery); options.port = num(data.port); options.listen = bool(data.listen); diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index f662c28f..a3124885 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -112,8 +112,8 @@ function FullNode(options) { authPeers: this.options.authPeers, knownPeers: this.options.knownPeers, identityKey: this.options.identityKey, - maxPeers: this.options.maxPeers, - maxLeeches: this.options.maxLeeches, + maxOutbound: this.options.maxOutbound, + maxInbound: this.options.maxInbound, proxyServer: this.options.proxyServer, preferredSeed: this.options.preferredSeed, ignoreDiscovery: this.options.ignoreDiscovery, diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 17f4c841..22cdf61a 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -69,7 +69,7 @@ function SPVNode(options) { authPeers: this.options.authPeers, knownPeers: this.options.knownPeers, identityKey: this.options.identityKey, - maxPeers: this.options.maxPeers, + maxOutbound: this.options.maxOutbound, ignoreDiscovery: this.options.ignoreDiscovery, headers: this.options.headers, selfish: true,