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);
this.pool = pool;
this.options = this.pool.options;
this.network = this.pool.network;
this.logger = this.pool.logger;
this.chain = this.pool.chain;
this.mempool = this.pool.mempool;
this.options = pool.options;
this.network = this.options.network;
this.logger = this.options.logger;
this.chain = this.options.chain;
this.mempool = this.options.mempool;
this.locker = new Lock();
this.next = null;
this.prev = null;
@ -215,6 +216,16 @@ Peer.INV_INTERVAL = 5000;
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.
* @function
@ -1163,7 +1174,7 @@ Peer.prototype.maybeTimeout = function maybeTimeout() {
if (this.merkleBlock) {
assert(this.merkleTime !== -1);
if (now > this.merkleTime + 60000) {
if (now > this.merkleTime + Peer.BLOCK_TIMEOUT) {
this.error('Peer is stalling (merkleblock).');
this.destroy();
return;
@ -1176,7 +1187,7 @@ Peer.prototype.maybeTimeout = function maybeTimeout() {
if (!this.isLoader())
return;
if (now > this.lastBlock + 60000) {
if (now > this.lastBlock + Peer.BLOCK_TIMEOUT) {
this.error('Peer is stalling (block).');
this.destroy();
}
@ -2212,7 +2223,7 @@ Peer.prototype.increaseBan = function increaseBan(score) {
*/
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 co = require('../utils/co');
var common = require('./common');
var errors = require('../protocol/errors');
var NetAddress = require('../primitives/netaddress');
var Address = require('../primitives/address');
var BIP150 = require('./bip150');
@ -33,7 +32,6 @@ var InvItem = require('../primitives/invitem');
var Map = require('../utils/map');
var packets = require('./packets');
var invTypes = InvItem.types;
var VerifyError = errors.VerifyError;
/**
* A pool of peers for handling all network activity.
@ -487,7 +485,7 @@ Pool.prototype.addLoader = function addLoader() {
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);
@ -625,17 +623,19 @@ Pool.prototype.requestHeaders = co(function* requestHeaders(peer) {
Pool.prototype.getNextTip = function getNextTip(height) {
var i, next;
if (!this.options.useCheckpoints) {
if (this.chain.isFull())
return;
return new BlockNode(null, height + 20000);
if (this.options.useCheckpoints) {
for (i = 0; i < this.checkpoints.length; i++) {
next = this.checkpoints[i];
if (next.height > height)
return next;
}
return;
}
for (i = 0; i < this.checkpoints.length; i++) {
next = this.checkpoints[i];
if (next.height > height)
return next;
}
if (this.chain.isFull())
return;
return new BlockNode(null, height + 20000);
};
/**
@ -842,6 +842,10 @@ Pool.prototype.bindPeer = function bindPeer(peer) {
self.handleClose(peer, connected);
});
peer.on('ban', function() {
self.ban(peer.address);
});
peer.on('error', function(err) {
self.emit('error', err, peer);
});
@ -1690,10 +1694,10 @@ Pool.prototype._addBlock = co(function* addBlock(peer, block) {
return;
if (!this.fulfill(peer, hash)) {
peer.invFilter.add(block.hash());
this.logger.warning(
'Received unrequested block: %s (%s).',
block.rhash(), peer.hostname);
peer.destroy();
return;
}
@ -1823,7 +1827,7 @@ Pool.prototype.handleTX = co(function* handleTX(peer, packet) {
Pool.prototype._handleTX = co(function* handleTX(peer, packet) {
var tx = packet.tx;
var hash = tx.hash('hex');
var requested, missing;
var missing;
if (peer.merkleBlock) {
assert(peer.merkleMatches > 0);
@ -1839,30 +1843,17 @@ Pool.prototype._handleTX = co(function* handleTX(peer, packet) {
}
}
requested = this.fulfill(peer, hash);
if (!requested) {
if (!this.fulfill(peer, hash)) {
this.logger.warning(
'Peer sent unrequested tx: %s (%s).',
tx.txid(), peer.hostname);
peer.invFilter.add(tx.hash());
}
if (!this.mempool) {
if (!requested)
this.txFilter.add(tx.hash());
this.emit('tx', tx, peer);
peer.destroy();
return;
}
if (!requested) {
if (this.mempool.hasReject(tx.hash())) {
throw new VerifyError(tx,
'alreadyknown',
'txn-already-in-mempool',
0);
}
if (!this.mempool) {
this.emit('tx', tx, peer);
return;
}
try {
@ -1993,7 +1984,8 @@ Pool.prototype.handleFilterClear = co(function* handleFilterClear(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 {
return yield this._handleMerkleBlock(peer, packet);
} finally {