net: avoid requesting duplicate blocks. avoid reconnecting to banned peers.
This commit is contained in:
parent
c3c3e41e68
commit
fd2f158169
@ -1583,7 +1583,8 @@ Chain.prototype.removeInvalid = function removeInvalid(hash) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the chain to see if it has a block, orphan, or pending block.
|
* Test the chain to see if it contains
|
||||||
|
* a block, or has recently seen a block.
|
||||||
* @param {Hash} hash
|
* @param {Hash} hash
|
||||||
* @returns {Promise} - Returns Boolean.
|
* @returns {Promise} - Returns Boolean.
|
||||||
*/
|
*/
|
||||||
@ -1595,9 +1596,31 @@ Chain.prototype.has = co(function* has(hash) {
|
|||||||
if (this.locker.has(hash))
|
if (this.locker.has(hash))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
if (this.invalid.has(hash))
|
||||||
|
return true;
|
||||||
|
|
||||||
return yield this.hasEntry(hash);
|
return yield this.hasEntry(hash);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the chain to see if it has recently seen a block.
|
||||||
|
* @param {Hash} hash
|
||||||
|
* @returns {Boolean}
|
||||||
|
*/
|
||||||
|
|
||||||
|
Chain.prototype.seen = function seen(hash) {
|
||||||
|
if (this.hasOrphan(hash))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (this.locker.has(hash))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (this.invalid.has(hash))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return this.db.hasCache(hash);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a block entry by timestamp.
|
* Find a block entry by timestamp.
|
||||||
* @param {Number} ts - Timestamp.
|
* @param {Number} ts - Timestamp.
|
||||||
|
|||||||
@ -879,17 +879,15 @@ Pool.prototype.handleOpen = function handleOpen(peer) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype.handleClose = co(function* handleClose(peer, connected) {
|
Pool.prototype.handleClose = co(function* handleClose(peer, connected) {
|
||||||
var loader = peer.isLoader();
|
var outbound = peer.outbound;
|
||||||
|
|
||||||
this.removePeer(peer);
|
this.removePeer(peer);
|
||||||
|
|
||||||
if (!this.loaded)
|
if (!this.loaded)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!loader) {
|
if (!outbound)
|
||||||
this.refill();
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
this.refill();
|
this.refill();
|
||||||
});
|
});
|
||||||
@ -1427,10 +1425,13 @@ Pool.prototype.getHost = function getHost(unique) {
|
|||||||
if (!addr.hasServices(this.reqServices))
|
if (!addr.hasServices(this.reqServices))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (now - entry.lastAttempt < 600 && i < 30)
|
if (i < 30 && now - entry.lastAttempt < 600)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (addr.port !== this.network.port && i < 50)
|
if (i < 50 && addr.port !== this.network.port)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (i < 95 && this.hosts.isBanned(addr.host))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
return entry.addr;
|
return entry.addr;
|
||||||
@ -1820,22 +1821,29 @@ Pool.prototype.sendBlockRequests = function sendBlockRequests(peer) {
|
|||||||
for (i = 0; i < queue.length; i++) {
|
for (i = 0; i < queue.length; i++) {
|
||||||
hash = queue[i];
|
hash = queue[i];
|
||||||
|
|
||||||
|
if (hashes.length === size)
|
||||||
|
break;
|
||||||
|
|
||||||
assert(this.queueMap.has(hash));
|
assert(this.queueMap.has(hash));
|
||||||
|
|
||||||
this.queueMap.remove(hash);
|
this.queueMap.remove(hash);
|
||||||
|
|
||||||
assert(!this.requestMap.has(hash));
|
assert(!this.requestMap.has(hash));
|
||||||
|
|
||||||
|
// Do a second check to make sure
|
||||||
|
// we don't have this in the chain.
|
||||||
|
// Avoids a potential race condition
|
||||||
|
// with the `chain.has()` call.
|
||||||
|
if (this.chain.seen(hash))
|
||||||
|
continue;
|
||||||
|
|
||||||
this.requestMap.insert(hash);
|
this.requestMap.insert(hash);
|
||||||
peer.requestMap.insert(hash);
|
peer.requestMap.insert(hash);
|
||||||
|
|
||||||
hashes.push(hash);
|
hashes.push(hash);
|
||||||
|
|
||||||
if (hashes.length === size)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
peer.blockQueue = queue.slice(hashes.length);
|
peer.blockQueue = queue.slice(i);
|
||||||
|
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
'Requesting %d/%d blocks from peer with getdata (%s).',
|
'Requesting %d/%d blocks from peer with getdata (%s).',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user