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