From b3c6eb06bcd9fd839c6ca47155ea7d2c4eb336e5 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 2 May 2014 15:00:31 +0400 Subject: [PATCH] chain: fix requesting known blocks --- lib/bcoin/chain.js | 27 ++++++++++++++++----------- package.json | 1 + 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index f98e1d1e..f2e5ea2a 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -49,7 +49,7 @@ Chain.prototype.add = function add(block) { var last = (this.hashes.length && pos > 0) ? this.hashes[pos - 1] : null; // Add orphan - if (last && prev !== last) { + if (last && prev !== last && !this.bloom.test(rhash)) { if (!this.bloom.test(rhash) && !this.orphan.map[prev]) { this.orphan.count++; this.orphan.map[prev] = block; @@ -59,16 +59,19 @@ Chain.prototype.add = function add(block) { break; } - this.bloom.add(rhash); - - // Sorted insert - var pos = utils.binaryInsert(this.ts, block.ts, function(a, b) { - return a - b; - }, true); - var hash = block.hash('hex'); - this.ts.splice(pos, 0, block.ts); - this.hashes.splice(pos, 0, hash); + + // It may be a re-requested block + if (!this.bloom.test(rhash)) { + // Sorted insert + var pos = utils.binaryInsert(this.ts, block.ts, function(a, b) { + return a - b; + }, true); + + this.ts.splice(pos, 0, block.ts); + this.hashes.splice(pos, 0, hash); + this.bloom.add(rhash); + } // Blocks is a FIFO queue, acting like a cache for the requests this.blocks.push(block); @@ -117,6 +120,8 @@ Chain.prototype.getLast = function getLast() { }; Chain.prototype.has = function has(hash) { + if (!Array.isArray(hash)) + hash = utils.toArray(hash, 'hex'); return this.bloom.test(hash) && !this.requests[utils.toHex(hash)]; }; @@ -128,11 +133,11 @@ Chain.prototype.get = function get(hash, cb) { if (this.blocks[i].hash('hex') === hash) return cb(this.blocks[i]); - this.emit('missing', hash); if (this.requests[hash]) this.requests[hash].push(cb); else this.requests[hash] = [ cb ]; + this.emit('missing', hash); }; Chain.prototype.isFull = function isFull() { diff --git a/package.json b/package.json index 07940c92..d0928215 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "hash.js": "^0.2.0" }, "devDependencies": { + "browserify": "^3.44.2", "mocha": "^1.18.2" } }