pool: refactor packet handling.

This commit is contained in:
Christopher Jeffrey 2017-01-19 17:26:40 -08:00
parent 2d952306e6
commit 7543049180
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 1089 additions and 893 deletions

View File

@ -31,7 +31,6 @@ var NetAddress = require('../primitives/netaddress');
var services = common.services; var services = common.services;
var invTypes = InvItem.types; var invTypes = InvItem.types;
var packetTypes = packets.types; var packetTypes = packets.types;
var VerifyResult = errors.VerifyResult;
/** /**
* Represents a remote peer. * Represents a remote peer.
@ -1501,6 +1500,13 @@ Peer.prototype.handlePacket = co(function* handlePacket(packet) {
if (entry) if (entry)
entry.resolve(packet); entry.resolve(packet);
if (packet.type === packetTypes.UNKNOWN) {
this.emit('unknown', packet);
return;
}
this.emit(packet.cmd, packet);
}); });
/** /**
@ -1513,9 +1519,7 @@ Peer.prototype.handlePacket = co(function* handlePacket(packet) {
Peer.prototype.flushMerkle = co(function* flushMerkle() { Peer.prototype.flushMerkle = co(function* flushMerkle() {
assert(this.lastMerkle); assert(this.lastMerkle);
this.emit('merkleblock', this.lastMerkle); yield this.pool.addBlock(this, this.lastMerkle);
yield this.pool.handleBlock(this, this.lastMerkle);
this.lastMerkle = null; this.lastMerkle = null;
this.waitingTX = 0; this.waitingTX = 0;
@ -1535,6 +1539,8 @@ Peer.prototype.handleFilterLoad = co(function* handleFilterLoad(packet) {
this.spvFilter = packet.filter; this.spvFilter = packet.filter;
this.noRelay = false; this.noRelay = false;
yield this.pool.handleFilterLoad(this, packet);
}); });
/** /**
@ -1555,12 +1561,14 @@ Peer.prototype.handleFilterAdd = co(function* handleFilterAdd(packet) {
this.spvFilter.add(data); this.spvFilter.add(data);
this.noRelay = false; this.noRelay = false;
yield this.pool.handleFilterAdd(this, packet);
}); });
/** /**
* Handle `filterclear` packet. * Handle `filterclear` packet.
* @private * @private
* @param {FilterClearPacket} * @param {FilterClearPacket} packet
*/ */
Peer.prototype.handleFilterClear = co(function* handleFilterClear(packet) { Peer.prototype.handleFilterClear = co(function* handleFilterClear(packet) {
@ -1568,54 +1576,24 @@ Peer.prototype.handleFilterClear = co(function* handleFilterClear(packet) {
this.spvFilter.reset(); this.spvFilter.reset();
this.noRelay = false; this.noRelay = false;
yield this.pool.handleFilterClear(this, packet);
}); });
/** /**
* Handle `merkleblock` packet. * Handle `merkleblock` packet.
* @private * @private
* @param {MerkleBlockPacket} * @param {MerkleBlockPacket} packet
*/ */
Peer.prototype.handleMerkleBlock = co(function* handleMerkleBlock(packet) { Peer.prototype.handleMerkleBlock = co(function* handleMerkleBlock(packet) {
var block = packet.block; yield this.pool.handleMerkleBlock(this, packet);
var ret = new VerifyResult();
// Potential DoS.
if (!this.options.spv) {
this.logger.warning(
'Peer sent unsolicited merkleblock (%s).',
this.hostname);
this.increaseBan(100);
return;
}
if (this.lastMerkle) {
this.logger.warning(
'Peer sent a merkleblock prematurely (%s).',
this.hostname);
this.increaseBan(100);
return;
}
if (!block.verify(ret)) {
this.logger.warning(
'Peer sent an invalid merkleblock (%s).',
this.hostname);
this.reject(block, 'invalid', ret.reason, ret.score);
return;
}
this.lastMerkle = block;
this.waitingTX = block.matches.length;
if (this.waitingTX === 0)
yield this.flushMerkle();
}); });
/** /**
* Handle `feefilter` packet. * Handle `feefilter` packet.
* @private * @private
* @param {FeeFilterPacket} * @param {FeeFilterPacket} packet
*/ */
Peer.prototype.handleFeeFilter = co(function* handleFeeFilter(packet) { Peer.prototype.handleFeeFilter = co(function* handleFeeFilter(packet) {
@ -1628,28 +1606,26 @@ Peer.prototype.handleFeeFilter = co(function* handleFeeFilter(packet) {
this.feeRate = rate; this.feeRate = rate;
this.emit('feefilter', rate); yield this.pool.handleFeeFilter(this, packet);
}); });
/** /**
* Handle `getheaders` packet. * Handle `getheaders` packet.
* @private * @private
* @param {GetHeadersPacket} * @param {GetHeadersPacket} packet
*/ */
Peer.prototype.handleGetHeaders = co(function* handleGetHeaders(packet) { Peer.prototype.handleGetHeaders = co(function* handleGetHeaders(packet) {
this.emit('getheaders', packet);
yield this.pool.handleGetHeaders(this, packet); yield this.pool.handleGetHeaders(this, packet);
}); });
/** /**
* Handle `getblocks` packet. * Handle `getblocks` packet.
* @private * @private
* @param {GetBlocksPacket} * @param {GetBlocksPacket} packet
*/ */
Peer.prototype.handleGetBlocks = co(function* handleGetBlocks(packet) { Peer.prototype.handleGetBlocks = co(function* handleGetBlocks(packet) {
this.emit('getblocks', packet);
yield this.pool.handleGetBlocks(this, packet); yield this.pool.handleGetBlocks(this, packet);
}); });
@ -1700,8 +1676,6 @@ Peer.prototype.handleVersion = co(function* handleVersion(packet) {
} }
} }
this.emit('version', packet);
this.send(new packets.VerackPacket()); this.send(new packets.VerackPacket());
yield this.pool.handleVersion(this, packet); yield this.pool.handleVersion(this, packet);
@ -1710,75 +1684,77 @@ Peer.prototype.handleVersion = co(function* handleVersion(packet) {
/** /**
* Handle `verack` packet. * Handle `verack` packet.
* @private * @private
* @param {VerackPacket} * @param {VerackPacket} packet
*/ */
Peer.prototype.handleVerack = co(function* handleVerack(packet) { Peer.prototype.handleVerack = co(function* handleVerack(packet) {
if (this.ack) {
this.logger.debug('Peer sent duplicate ack (%s).', this.hostname);
return;
}
this.ack = true; this.ack = true;
this.emit('verack', packet);
this.logger.debug('Received verack (%s).', this.hostname); this.logger.debug('Received verack (%s).', this.hostname);
yield this.pool.handleVerack(this, packet);
}); });
/** /**
* Handle `mempool` packet. * Handle `mempool` packet.
* @private * @private
* @param {MempoolPacket} * @param {MempoolPacket} packet
*/ */
Peer.prototype.handleMempool = co(function* handleMempool(packet) { Peer.prototype.handleMempool = co(function* handleMempool(packet) {
this.emit('mempool', packet); yield this.pool.handleMempool(this, packet);
yield this.pool.handleMempool(this);
}); });
/** /**
* Handle `getdata` packet. * Handle `getdata` packet.
* @private * @private
* @param {GetDataPacket} * @param {GetDataPacket} packet
*/ */
Peer.prototype.handleGetData = co(function* handleGetData(packet) { Peer.prototype.handleGetData = co(function* handleGetData(packet) {
this.emit('getdata', packet.items); yield this.pool.handleGetData(this, packet);
yield this.pool.handleGetData(this, packet.items);
}); });
/** /**
* Handle `notfound` packet. * Handle `notfound` packet.
* @private * @private
* @param {NotFoundPacket} * @param {NotFoundPacket} packet
*/ */
Peer.prototype.handleNotFound = co(function* handleNotFound(packet) { Peer.prototype.handleNotFound = co(function* handleNotFound(packet) {
this.emit('notfound', packet.items); yield this.pool.handleNotFound(this, packet);
yield this.pool.handleNotFound(this, packet.items);
}); });
/** /**
* Handle `addr` packet. * Handle `addr` packet.
* @private * @private
* @param {AddrPacket} * @param {AddrPacket} packet
*/ */
Peer.prototype.handleAddr = co(function* handleAddr(packet) { Peer.prototype.handleAddr = co(function* handleAddr(packet) {
this.emit('addr', packet.items); yield this.pool.handleAddr(this, packet);
yield this.pool.handleAddr(this, packet.items);
}); });
/** /**
* Handle `ping` packet. * Handle `ping` packet.
* @private * @private
* @param {PingPacket} * @param {PingPacket} packet
*/ */
Peer.prototype.handlePing = co(function* handlePing(packet) { Peer.prototype.handlePing = co(function* handlePing(packet) {
this.emit('ping', this.minPing);
if (packet.nonce) if (packet.nonce)
this.send(new packets.PongPacket(packet.nonce)); this.send(new packets.PongPacket(packet.nonce));
yield this.pool.handlePing(this, packet);
}); });
/** /**
* Handle `pong` packet. * Handle `pong` packet.
* @private * @private
* @param {PongPacket} * @param {PongPacket} packet
*/ */
Peer.prototype.handlePong = co(function* handlePong(packet) { Peer.prototype.handlePong = co(function* handlePong(packet) {
@ -1811,111 +1787,84 @@ Peer.prototype.handlePong = co(function* handlePong(packet) {
this.challenge = null; this.challenge = null;
this.emit('pong', this.minPing); yield this.pool.handlePong(this, packet);
}); });
/** /**
* Handle `getaddr` packet. * Handle `getaddr` packet.
* @private * @private
* @param {GetAddrPacket} * @param {GetAddrPacket} packet
*/ */
Peer.prototype.handleGetAddr = co(function* handleGetAddr(packet) { Peer.prototype.handleGetAddr = co(function* handleGetAddr(packet) {
this.emit('getaddr', packet);
yield this.pool.handleGetAddr(this, packet); yield this.pool.handleGetAddr(this, packet);
}); });
/** /**
* Handle `inv` packet. * Handle `inv` packet.
* @private * @private
* @param {InvPacket} * @param {InvPacket} packet
*/ */
Peer.prototype.handleInv = co(function* handleInv(packet) { Peer.prototype.handleInv = co(function* handleInv(packet) {
this.emit('inv', packet.items); yield this.pool.handleInv(this, packet);
yield this.pool.handleInv(this, packet.items);
}); });
/** /**
* Handle `headers` packet. * Handle `headers` packet.
* @private * @private
* @param {HeadersPacket} * @param {HeadersPacket} packet
*/ */
Peer.prototype.handleHeaders = co(function* handleHeaders(packet) { Peer.prototype.handleHeaders = co(function* handleHeaders(packet) {
this.emit('headers', packet.items); yield this.pool.handleHeaders(this, packet);
yield this.pool.handleHeaders(this, packet.items);
}); });
/** /**
* Handle `sendheaders` packet. * Handle `sendheaders` packet.
* @private * @private
* @param {SendHeadersPacket} * @param {SendHeadersPacket} packet
*/ */
Peer.prototype.handleSendHeaders = co(function* handleSendHeaders(packet) { Peer.prototype.handleSendHeaders = co(function* handleSendHeaders(packet) {
this.preferHeaders = true; this.preferHeaders = true;
this.emit('sendheaders'); yield this.pool.handleSendHeaders(this, packet);
}); });
/** /**
* Handle `block` packet. * Handle `block` packet.
* @private * @private
* @param {BlockPacket} * @param {BlockPacket} packet
*/ */
Peer.prototype.handleBlock = co(function* handleBlock(packet) { Peer.prototype.handleBlock = co(function* handleBlock(packet) {
if (this.options.spv) { yield this.pool.handleBlock(this, packet);
this.logger.warning(
'Peer sent unsolicited block (%s).',
this.hostname);
return;
}
this.emit('block', packet.block);
yield this.pool.handleBlock(this, packet.block);
}); });
/** /**
* Handle `tx` packet. * Handle `tx` packet.
* @private * @private
* @param {TXPacket} * @param {TXPacket} packet
*/ */
Peer.prototype.handleTX = co(function* handleTX(packet) { Peer.prototype.handleTX = co(function* handleTX(packet) {
var tx = packet.tx; yield this.pool.handleTX(this, packet);
if (this.lastMerkle) {
assert(this.waitingTX > 0);
if (this.lastMerkle.hasTX(tx)) {
this.lastMerkle.addTX(tx);
if (--this.waitingTX === 0)
yield this.flushMerkle();
return;
}
}
this.emit('tx', tx);
yield this.pool.handleTX(this, tx);
}); });
/** /**
* Handle `reject` packet. * Handle `reject` packet.
* @private * @private
* @param {RejectPacket} reject * @param {RejectPacket} packet
*/ */
Peer.prototype.handleReject = co(function* handleReject(reject) { Peer.prototype.handleReject = co(function* handleReject(packet) {
this.emit('reject', reject); yield this.pool.handleReject(this, packet);
yield this.pool.handleReject(this, reject);
}); });
/** /**
* Handle `encinit` packet. * Handle `encinit` packet.
* @private * @private
* @param {EncinitPacket} * @param {EncinitPacket} packet
*/ */
Peer.prototype.handleEncinit = co(function* handleEncinit(packet) { Peer.prototype.handleEncinit = co(function* handleEncinit(packet) {
@ -1924,15 +1873,15 @@ Peer.prototype.handleEncinit = co(function* handleEncinit(packet) {
this.bip151.encinit(packet.publicKey, packet.cipher); this.bip151.encinit(packet.publicKey, packet.cipher);
this.emit('encinit', packet);
this.send(this.bip151.toEncack()); this.send(this.bip151.toEncack());
yield this.pool.handleEncinit(this, packet);
}); });
/** /**
* Handle `encack` packet. * Handle `encack` packet.
* @private * @private
* @param {EncackPacket} * @param {EncackPacket} packet
*/ */
Peer.prototype.handleEncack = co(function* handleEncack(packet) { Peer.prototype.handleEncack = co(function* handleEncack(packet) {
@ -1941,13 +1890,13 @@ Peer.prototype.handleEncack = co(function* handleEncack(packet) {
this.bip151.encack(packet.publicKey); this.bip151.encack(packet.publicKey);
this.emit('encack', packet); yield this.pool.handleEncack(this, packet);
}); });
/** /**
* Handle `authchallenge` packet. * Handle `authchallenge` packet.
* @private * @private
* @param {AuthChallengePacket} * @param {AuthChallengePacket} packet
*/ */
Peer.prototype.handleAuthChallenge = co(function* handleAuthChallenge(packet) { Peer.prototype.handleAuthChallenge = co(function* handleAuthChallenge(packet) {
@ -1958,15 +1907,15 @@ Peer.prototype.handleAuthChallenge = co(function* handleAuthChallenge(packet) {
sig = this.bip150.challenge(packet.hash); sig = this.bip150.challenge(packet.hash);
this.emit('authchallenge', packet.hash);
this.send(new packets.AuthReplyPacket(sig)); this.send(new packets.AuthReplyPacket(sig));
yield this.pool.handleAuthChallenge(this, packet);
}); });
/** /**
* Handle `authreply` packet. * Handle `authreply` packet.
* @private * @private
* @param {AuthReplyPacket} * @param {AuthReplyPacket} packet
*/ */
Peer.prototype.handleAuthReply = co(function* handleAuthReply(packet) { Peer.prototype.handleAuthReply = co(function* handleAuthReply(packet) {
@ -1980,13 +1929,13 @@ Peer.prototype.handleAuthReply = co(function* handleAuthReply(packet) {
if (hash) if (hash)
this.send(new packets.AuthProposePacket(hash)); this.send(new packets.AuthProposePacket(hash));
this.emit('authreply', packet.signature); yield this.pool.handleAuthReply(this, packet);
}); });
/** /**
* Handle `authpropose` packet. * Handle `authpropose` packet.
* @private * @private
* @param {AuthProposePacket} * @param {AuthProposePacket} packet
*/ */
Peer.prototype.handleAuthPropose = co(function* handleAuthPropose(packet) { Peer.prototype.handleAuthPropose = co(function* handleAuthPropose(packet) {
@ -1999,18 +1948,17 @@ Peer.prototype.handleAuthPropose = co(function* handleAuthPropose(packet) {
this.send(new packets.AuthChallengePacket(hash)); this.send(new packets.AuthChallengePacket(hash));
this.emit('authpropose', packet.hash); yield this.pool.handleAuthPropose(this, packet);
}); });
/** /**
* Handle an unknown packet. * Handle an unknown packet.
* @private * @private
* @param {UnknownPacket} * @param {UnknownPacket} packet
*/ */
Peer.prototype.handleUnknown = co(function* handleUnknown(packet) { Peer.prototype.handleUnknown = co(function* handleUnknown(packet) {
this.logger.warning('Unknown packet: %s.', packet.cmd); yield this.pool.handleUnknown(this, packet);
this.emit('unknown', packet);
}); });
/** /**
@ -2046,7 +1994,8 @@ Peer.prototype.handleSendCmpct = co(function* handleSendCmpct(packet) {
this.compactMode = packet.mode; this.compactMode = packet.mode;
this.compactWitness = packet.version === 2; this.compactWitness = packet.version === 2;
this.emit('sendcmpct', packet);
yield this.pool.handleSendCmpct(this, packet);
}); });
/** /**
@ -2056,8 +2005,7 @@ Peer.prototype.handleSendCmpct = co(function* handleSendCmpct(packet) {
*/ */
Peer.prototype.handleCmpctBlock = co(function* handleCmpctBlock(packet) { Peer.prototype.handleCmpctBlock = co(function* handleCmpctBlock(packet) {
this.emit('cmpctblock', packet.block); yield this.pool.handleCmpctBlock(this, packet);
yield this.pool.handleCmpctBlock(this, packet.block);
}); });
/** /**
@ -2067,8 +2015,7 @@ Peer.prototype.handleCmpctBlock = co(function* handleCmpctBlock(packet) {
*/ */
Peer.prototype.handleGetBlockTxn = co(function* handleGetBlockTxn(packet) { Peer.prototype.handleGetBlockTxn = co(function* handleGetBlockTxn(packet) {
this.emit('getblocktxn', packet.request); yield this.pool.handleGetBlockTxn(this, packet);
yield this.pool.handleGetBlockTxn(this, packet.request);
}); });
/** /**
@ -2078,8 +2025,7 @@ Peer.prototype.handleGetBlockTxn = co(function* handleGetBlockTxn(packet) {
*/ */
Peer.prototype.handleBlockTxn = co(function* handleBlockTxn(packet) { Peer.prototype.handleBlockTxn = co(function* handleBlockTxn(packet) {
this.emit('blocktxn', packet.response); yield this.pool.handleGetBlockTxn(this, packet);
yield this.pool.handleGetBlockTxn(this, packet.response);
}); });
/** /**

File diff suppressed because it is too large Load Diff