From 509ed09603980780207b67ac8f36a5f01e13e808 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 19 Feb 2016 21:07:02 -0800 Subject: [PATCH] better existence check. --- lib/bcoin/chain.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 15239eb2..c16fcf5f 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -991,13 +991,17 @@ Chain.prototype.add = function add(initial, peer, callback, force) { (function next(block) { var hash = block.hash('hex'); var prevHash = block.prevBlock; - var prevHeight, entry, checkpoint, prev, orphan; + var prevHeight, height, entry, checkpoint, prev, orphan; + + // Find the previous block height/index. + prevHeight = self.db.getHeight(prevHash); + height = prevHeight === -1 ? -1 : prevHeight + 1; // We already have this block. - if (self.db.has(hash)) { + if (self.db.has(hash) || self.hasPending(hash)) { self.emit('exists', block, { - height: entry.height, - hash: entry.hash + height: height, + hash: hash }, peer); return done(); } @@ -1005,7 +1009,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) { // Do not revalidate known invalid blocks. if (self.invalid[hash] || self.invalid[prevHash]) { self.emit('invalid', block, { - height: -1, + height: height, hash: hash, seen: !!self.invalid[hash], chain: !!self.invalid[prevHash] @@ -1014,9 +1018,6 @@ Chain.prototype.add = function add(initial, peer, callback, force) { return done(); } - // Find the previous block height/index. - prevHeight = self.db.getHeight(prevHash); - // Validate the block we want to add. // This is only necessary for new // blocks coming in, not the resolving @@ -1024,7 +1025,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) { if (block === initial && !block.verify()) { self.invalid[hash] = true; self.emit('invalid', block, { - height: prevHeight === -1 ? -1 : prevHeight + 1, + height: height, hash: hash, seen: false, chain: false