pool/peer: do not allow duplicate txs in merkle blocks.

This commit is contained in:
Christopher Jeffrey 2017-03-07 20:13:32 -08:00
parent bfca120f4f
commit 4aecaf9158
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 28 additions and 9 deletions

View File

@ -117,6 +117,7 @@ function Peer(options) {
this.merkleBlock = null;
this.merkleTime = -1;
this.merkleMatches = 0;
this.merkleMap = null;
this.syncing = false;
this.sentAddr = false;
this.sentGetAddr = false;

View File

@ -736,6 +736,7 @@ Pool.prototype.stopSync = function stopSync() {
peer.merkleBlock = null;
peer.merkleTime = -1;
peer.merkleMatches = 0;
peer.merkleMap = null;
peer.blockTime = -1;
peer.blockMap.reset();
peer.compactBlocks.reset();
@ -2454,18 +2455,34 @@ Pool.prototype._handleTX = co(function* handleTX(peer, packet) {
var tx = packet.tx;
var hash = tx.hash('hex');
var flags = chainCommon.flags.VERIFY_NONE;
var block = peer.merkleBlock;
var missing;
if (peer.merkleBlock) {
if (block) {
assert(peer.merkleMatches > 0);
if (peer.merkleBlock.hasTX(hash)) {
peer.merkleBlock.addTX(tx);
if (--peer.merkleMatches === 0) {
yield this._addBlock(peer, peer.merkleBlock, flags);
peer.merkleTime = -1;
peer.merkleBlock = null;
peer.merkleMatches = 0;
assert(peer.merkleMap);
if (block.hasTX(hash)) {
if (peer.merkleMap.has(hash)) {
this.logger.warning(
'Peer sent duplicate merkle tx: %s (%s).',
tx.txid(), peer.hostname());
peer.increaseBan(100);
return;
}
peer.merkleMap.insert(hash);
block.addTX(tx);
if (--peer.merkleMatches === 0) {
peer.merkleBlock = null;
peer.merkleTime = -1;
peer.merkleMatches = 0;
peer.merkleMap = null;
yield this._addBlock(peer, block, flags);
}
return;
}
}
@ -2684,9 +2701,10 @@ Pool.prototype._handleMerkleBlock = co(function* handleMerkleBlock(peer, packet)
return;
}
peer.merkleTime = util.ms();
peer.merkleBlock = block;
peer.merkleTime = util.ms();
peer.merkleMatches = block.tree.matches.length;
peer.merkleMap = new Map();
});
/**