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