diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index d4ac891c..2e75f790 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -994,9 +994,18 @@ Chain.prototype.add = function add(initial, peer, callback, force) { var prevHash = block.prevBlock; var prevHeight, entry, checkpoint, prev, orphan; - // Special case for genesis block. - if (block.isGenesis()) - return done(); + // We already have this block. Do regular + // orphan resolution (won't do anything). + // NOTE: Wrap this in a nextTick to avoid + // a stack overflow if there are a lot of + // existing blocks. + if (self.db.has(hash)) { + self.emit('exists', block, { + height: entry.height, + hash: entry.hash + }, peer); + return utils.nextTick(handleOrphans); + } // Do not revalidate known invalid blocks. if (self.invalid[hash] || self.invalid[prevHash]) { @@ -1027,6 +1036,10 @@ Chain.prototype.add = function add(initial, peer, callback, force) { return done(); } + // Special case for genesis block. + if (block.isGenesis()) + return done(); + // If the block is already known to be // an orphan, ignore it. orphan = self.orphan.map[prevHash]; @@ -1126,18 +1139,8 @@ Chain.prototype.add = function add(initial, peer, callback, force) { if (err) return done(err); - // We already have this block. Do regular - // orphan resolution (won't do anything). - // NOTE: Wrap this in a nextTick to avoid - // a stack overflow if there are a lot of - // existing blocks. - if (existing.hash === hash) { - self.emit('exists', block, { - height: entry.height, - hash: entry.hash - }, peer); - return utils.nextTick(handleOrphans); - } + // Shouldn't be the same by this point. + assert(existing.hash !== entry.hash); // A valid block with an already existing // height came in, that spells fork. We