peer: add inv queue.
This commit is contained in:
parent
84f7b0196d
commit
8ca683f4f3
@ -98,6 +98,7 @@ function Peer(pool) {
|
||||
this.drainSize = 0;
|
||||
this.drainQueue = [];
|
||||
this.banScore = 0;
|
||||
this.invQueue = [];
|
||||
|
||||
this.version = null;
|
||||
this.preferHeaders = false;
|
||||
@ -125,6 +126,7 @@ function Peer(pool) {
|
||||
|
||||
this.connectTimeout = null;
|
||||
this.pingTimer = null;
|
||||
this.invTimer = null;
|
||||
this.stallTimer = null;
|
||||
|
||||
this.addrFilter = new Bloom.Rolling(5000, 0.001);
|
||||
@ -160,6 +162,7 @@ Peer.DRAIN_MAX = 5 << 20;
|
||||
Peer.DRAIN_TIMEOUT = 10000;
|
||||
Peer.STALL_INTERVAL = 5000;
|
||||
Peer.PING_INTERVAL = 30000;
|
||||
Peer.INV_INTERVAL = 5000;
|
||||
Peer.RESPONSE_TIMEOUT = 30000;
|
||||
|
||||
Peer.prototype.__defineGetter__('host', function() {
|
||||
@ -526,6 +529,11 @@ Peer.prototype.finalize = co(function* finalize() {
|
||||
self.sendPing();
|
||||
}, Peer.PING_INTERVAL);
|
||||
|
||||
// Setup the inv flusher.
|
||||
this.invTimer = setInterval(function() {
|
||||
self.flushInv();
|
||||
}, Peer.INV_INTERVAL);
|
||||
|
||||
// Ask for headers-only.
|
||||
if (this.options.headers) {
|
||||
if (this.version.version >= 70012)
|
||||
@ -719,7 +727,8 @@ Peer.prototype.announceList = function announceList() {
|
||||
*/
|
||||
|
||||
Peer.prototype.sendInv = function sendInv(items) {
|
||||
var i, item, chunk;
|
||||
var hasBlock = false;
|
||||
var i, item;
|
||||
|
||||
if (!this.ack)
|
||||
return;
|
||||
@ -732,17 +741,44 @@ Peer.prototype.sendInv = function sendInv(items) {
|
||||
|
||||
for (i = 0; i < items.length; i++) {
|
||||
item = items[i];
|
||||
this.invFilter.add(item.hash, 'hex');
|
||||
if (item.type === invTypes.BLOCK)
|
||||
hasBlock = true;
|
||||
this.invQueue.push(item);
|
||||
}
|
||||
|
||||
if (items.length === 0)
|
||||
if (this.invQueue.length >= 500 || hasBlock)
|
||||
this.flushInv();
|
||||
};
|
||||
|
||||
/**
|
||||
* Flush inv queue.
|
||||
* @private
|
||||
*/
|
||||
|
||||
Peer.prototype.flushInv = function flushInv() {
|
||||
var queue = this.invQueue.slice();
|
||||
var items = [];
|
||||
var i, item, chunk;
|
||||
|
||||
if (queue.length === 0)
|
||||
return;
|
||||
|
||||
this.logger.spam('Serving %d inv items to %s.',
|
||||
items.length, this.hostname);
|
||||
this.invQueue.length = 0;
|
||||
|
||||
for (i = 0; i < items.length; i += 50000) {
|
||||
chunk = items.slice(i, i + 50000);
|
||||
this.logger.spam('Serving %d inv items to %s.',
|
||||
queue.length, this.hostname);
|
||||
|
||||
for (i = 0; i < queue.length; i++) {
|
||||
item = queue[i];
|
||||
|
||||
if (!this.invFilter.added(item.hash, 'hex'))
|
||||
continue;
|
||||
|
||||
items.push(item);
|
||||
}
|
||||
|
||||
for (i = 0; i < items.length; i += 1000) {
|
||||
chunk = items.slice(i, i + 1000);
|
||||
this.send(new packets.InvPacket(chunk));
|
||||
}
|
||||
};
|
||||
@ -891,6 +927,11 @@ Peer.prototype.destroy = function destroy() {
|
||||
this.pingTimer = null;
|
||||
}
|
||||
|
||||
if (this.invTimer != null) {
|
||||
clearInterval(this.invTimer);
|
||||
this.invTimer = null;
|
||||
}
|
||||
|
||||
if (this.stallTimer != null) {
|
||||
clearInterval(this.stallTimer);
|
||||
this.stallTimer = null;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user