pool: stricter standards for requests.

This commit is contained in:
Christopher Jeffrey 2017-01-20 20:51:36 -08:00
parent 7a5f4a1928
commit b60e0e20a4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 44 additions and 41 deletions

View File

@ -76,12 +76,13 @@ function Peer(pool) {
EventEmitter.call(this); EventEmitter.call(this);
this.pool = pool; this.pool = pool;
this.options = this.pool.options; this.options = pool.options;
this.network = this.pool.network; this.network = this.options.network;
this.logger = this.pool.logger; this.logger = this.options.logger;
this.chain = this.pool.chain; this.chain = this.options.chain;
this.mempool = this.pool.mempool; this.mempool = this.options.mempool;
this.locker = new Lock(); this.locker = new Lock();
this.next = null; this.next = null;
this.prev = null; this.prev = null;
@ -215,6 +216,16 @@ Peer.INV_INTERVAL = 5000;
Peer.RESPONSE_TIMEOUT = 30000; Peer.RESPONSE_TIMEOUT = 30000;
/**
* Required time for loader to
* respond with block/merkleblock
* during initial sync.
* @const {Number}
* @default
*/
Peer.BLOCK_TIMEOUT = 60000;
/** /**
* Getter to retrieve host. * Getter to retrieve host.
* @function * @function
@ -1163,7 +1174,7 @@ Peer.prototype.maybeTimeout = function maybeTimeout() {
if (this.merkleBlock) { if (this.merkleBlock) {
assert(this.merkleTime !== -1); assert(this.merkleTime !== -1);
if (now > this.merkleTime + 60000) { if (now > this.merkleTime + Peer.BLOCK_TIMEOUT) {
this.error('Peer is stalling (merkleblock).'); this.error('Peer is stalling (merkleblock).');
this.destroy(); this.destroy();
return; return;
@ -1176,7 +1187,7 @@ Peer.prototype.maybeTimeout = function maybeTimeout() {
if (!this.isLoader()) if (!this.isLoader())
return; return;
if (now > this.lastBlock + 60000) { if (now > this.lastBlock + Peer.BLOCK_TIMEOUT) {
this.error('Peer is stalling (block).'); this.error('Peer is stalling (block).');
this.destroy(); this.destroy();
} }
@ -2212,7 +2223,7 @@ Peer.prototype.increaseBan = function increaseBan(score) {
*/ */
Peer.prototype.ban = function ban() { Peer.prototype.ban = function ban() {
this.pool.ban(this.address); this.emit('ban');
}; };
/** /**

View File

@ -14,7 +14,6 @@ var util = require('../utils/util');
var IP = require('../utils/ip'); var IP = require('../utils/ip');
var co = require('../utils/co'); var co = require('../utils/co');
var common = require('./common'); var common = require('./common');
var errors = require('../protocol/errors');
var NetAddress = require('../primitives/netaddress'); var NetAddress = require('../primitives/netaddress');
var Address = require('../primitives/address'); var Address = require('../primitives/address');
var BIP150 = require('./bip150'); var BIP150 = require('./bip150');
@ -33,7 +32,6 @@ var InvItem = require('../primitives/invitem');
var Map = require('../utils/map'); var Map = require('../utils/map');
var packets = require('./packets'); var packets = require('./packets');
var invTypes = InvItem.types; var invTypes = InvItem.types;
var VerifyError = errors.VerifyError;
/** /**
* A pool of peers for handling all network activity. * A pool of peers for handling all network activity.
@ -487,7 +485,7 @@ Pool.prototype.addLoader = function addLoader() {
peer = this.createPeer(addr); peer = this.createPeer(addr);
this.logger.info('Setting loader peer (%s).', peer.hostname); this.logger.info('Adding loader peer (%s).', peer.hostname);
this.peers.add(peer); this.peers.add(peer);
@ -625,17 +623,19 @@ Pool.prototype.requestHeaders = co(function* requestHeaders(peer) {
Pool.prototype.getNextTip = function getNextTip(height) { Pool.prototype.getNextTip = function getNextTip(height) {
var i, next; var i, next;
if (!this.options.useCheckpoints) { if (this.options.useCheckpoints) {
if (this.chain.isFull()) for (i = 0; i < this.checkpoints.length; i++) {
return; next = this.checkpoints[i];
return new BlockNode(null, height + 20000); if (next.height > height)
return next;
}
return;
} }
for (i = 0; i < this.checkpoints.length; i++) { if (this.chain.isFull())
next = this.checkpoints[i]; return;
if (next.height > height)
return next; return new BlockNode(null, height + 20000);
}
}; };
/** /**
@ -842,6 +842,10 @@ Pool.prototype.bindPeer = function bindPeer(peer) {
self.handleClose(peer, connected); self.handleClose(peer, connected);
}); });
peer.on('ban', function() {
self.ban(peer.address);
});
peer.on('error', function(err) { peer.on('error', function(err) {
self.emit('error', err, peer); self.emit('error', err, peer);
}); });
@ -1690,10 +1694,10 @@ Pool.prototype._addBlock = co(function* addBlock(peer, block) {
return; return;
if (!this.fulfill(peer, hash)) { if (!this.fulfill(peer, hash)) {
peer.invFilter.add(block.hash());
this.logger.warning( this.logger.warning(
'Received unrequested block: %s (%s).', 'Received unrequested block: %s (%s).',
block.rhash(), peer.hostname); block.rhash(), peer.hostname);
peer.destroy();
return; return;
} }
@ -1823,7 +1827,7 @@ Pool.prototype.handleTX = co(function* handleTX(peer, packet) {
Pool.prototype._handleTX = co(function* handleTX(peer, packet) { Pool.prototype._handleTX = co(function* handleTX(peer, packet) {
var tx = packet.tx; var tx = packet.tx;
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var requested, missing; var missing;
if (peer.merkleBlock) { if (peer.merkleBlock) {
assert(peer.merkleMatches > 0); assert(peer.merkleMatches > 0);
@ -1839,30 +1843,17 @@ Pool.prototype._handleTX = co(function* handleTX(peer, packet) {
} }
} }
requested = this.fulfill(peer, hash); if (!this.fulfill(peer, hash)) {
if (!requested) {
this.logger.warning( this.logger.warning(
'Peer sent unrequested tx: %s (%s).', 'Peer sent unrequested tx: %s (%s).',
tx.txid(), peer.hostname); tx.txid(), peer.hostname);
peer.destroy();
peer.invFilter.add(tx.hash());
}
if (!this.mempool) {
if (!requested)
this.txFilter.add(tx.hash());
this.emit('tx', tx, peer);
return; return;
} }
if (!requested) { if (!this.mempool) {
if (this.mempool.hasReject(tx.hash())) { this.emit('tx', tx, peer);
throw new VerifyError(tx, return;
'alreadyknown',
'txn-already-in-mempool',
0);
}
} }
try { try {
@ -1993,7 +1984,8 @@ Pool.prototype.handleFilterClear = co(function* handleFilterClear(peer, packet)
*/ */
Pool.prototype.handleMerkleBlock = co(function* handleMerkleBlock(peer, packet) { Pool.prototype.handleMerkleBlock = co(function* handleMerkleBlock(peer, packet) {
var unlock = yield this.locker.lock(); var hash = packet.block.hash('hex');
var unlock = yield this.locker.lock(hash);
try { try {
return yield this._handleMerkleBlock(peer, packet); return yield this._handleMerkleBlock(peer, packet);
} finally { } finally {