better existence check.

This commit is contained in:
Christopher Jeffrey 2016-02-19 21:07:02 -08:00
parent 8ed054cbda
commit 509ed09603

View File

@ -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