add some data integrity checks when loading the chain.

This commit is contained in:
Christopher Jeffrey 2016-02-08 19:55:56 -08:00
parent d20b3b8737
commit adb304748f

View File

@ -97,23 +97,36 @@ Chain.prototype._init = function _init() {
utils.nextTick(function() { utils.nextTick(function() {
var count = self.db.count(); var count = self.db.count();
var entry; var lastEntry;
var i = 1; var i = 1;
function done() { function done(height) {
if (height != null) {
utils.debug(
'Blockchain is corrupt after height %d. Resetting.',
height);
self.resetHeight(height);
} else {
utils.debug('Chain successfully loaded.');
}
self.loading = false; self.loading = false;
self.emit('load'); self.emit('load');
utils.debug('Chain successfully loaded.');
} }
(function next() { (function next() {
if (i >= count) if (i >= count)
return done(); return done();
self.db.getAsync(i, function(err, entry) { self.db.getAsync(i, function(err, entry) {
if (err) if (err)
throw err; throw err;
// Do some paranoid checks.
if (lastEntry && entry.prevBlock !== lastEntry.hash)
return done(Math.max(0, i - 2));
self._saveEntry(entry); self._saveEntry(entry);
lastEntry = entry;
i += 1; i += 1;
next(); next();
}); });