handle verification better. make use of cache.

This commit is contained in:
Christopher Jeffrey 2016-02-19 20:57:06 -08:00
parent ad95ffbfcd
commit 8ed054cbda
2 changed files with 78 additions and 52 deletions

View File

@ -1131,27 +1131,55 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
// Lookup previous entry. // Lookup previous entry.
// We can do this synchronously: // We can do this synchronously:
// it is already cached. // This will be cached in 99.9% of cases.
if (!self.db.isCached(prevHeight))
utils.debug('Warning: height %d is not cached.', prevHeight);
try {
prev = self.db.getSync(prevHeight); prev = self.db.getSync(prevHeight);
} catch (e) {
return done(e);
}
assert(prev); assert(prev);
// Do "contextual" verification on our block // Do "contextual" verification on our block
// now that we're certain its previous // now that we're certain its previous
// block is in the chain. // block is in the chain.
self._verifyContext(block, prev, function(err, verified) { self._verifyContext(block, prev, function(err, verified) {
var existing;
if (err) if (err)
return done(err); return done(err);
if (!verified) {
self.invalid[entry.hash] = true;
self.emit('invalid', block, {
height: entry.height,
hash: entry.hash,
seen: false,
chain: false
}, peer);
return done();
}
// Real fork resolution would just be this. // Real fork resolution would just be this.
// if (entry.chainwork.cmp(self.tip.chainwork) > 0) // if (entry.chainwork.cmp(self.tip.chainwork) > 0)
// return self.setBestChain(entry); // return self.setBestChain(entry);
// return done(); // return done();
// See if the entry already exists. // See if the height already exists (fork).
// Do this synchronously: This will
// be cached in 99.9% of cases.
if (self.db.has(entry.height)) { if (self.db.has(entry.height)) {
return self.db.getAsync(entry.height, function(err, existing) { if (!self.db.isCached(entry.height))
if (err) utils.debug('Warning: height %d is not cached.', entry.height);
return done(err);
try {
existing = self.db.getSync(entry.height);
} catch (e) {
return done(e);
}
// Shouldn't be the same by this point. // Shouldn't be the same by this point.
assert(existing.hash !== entry.hash); assert(existing.hash !== entry.hash);
@ -1172,8 +1200,8 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
// The block has equal chainwork (an // The block has equal chainwork (an
// alternate tip). Reset the chain, find // alternate tip). Reset the chain, find
// a new peer, and wait to see who wins. // a new peer, and wait to see who wins.
// self.revertHeight(existing.height - 1, function(err) { // return self.revertHeight(existing.height - 1, function(err) {
self._revertLast(existing, function(err, existingBlock) { return self._revertLast(existing, function(err, existingBlock) {
if (err) if (err)
return done(err); return done(err);
@ -1186,23 +1214,11 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
return done(); return done();
}, true); }, true);
});
} }
// Add entry if we do not have it. // Add entry if we do not have it.
assert(self.db.getHeight(entry.hash) === -1); assert(self.db.getHeight(entry.hash) === -1);
if (!verified) {
self.invalid[entry.hash] = true;
self.emit('invalid', block, {
height: entry.height,
hash: entry.hash,
seen: false,
chain: false
}, peer);
return done();
}
// Update the block height // Update the block height
block.height = entry.height; block.height = entry.height;
block.txs.forEach(function(tx) { block.txs.forEach(function(tx) {

View File

@ -243,6 +243,16 @@ ChainDB.prototype._cache = function _cache(entry) {
} }
}; };
ChainDB.prototype.isCached = function isCached(height) {
if (this.queue[height] != null)
return true;
if (this.cache[height] != null)
return true;
return false;
};
ChainDB.prototype.getHeight = function getHeight(hash) { ChainDB.prototype.getHeight = function getHeight(hash) {
var height = this.heightLookup[hash]; var height = this.heightLookup[hash];