diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index e1fe2c77..cb8353f5 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -1346,7 +1346,7 @@ ChainDB.prototype.reset = co(function* reset(block) { this.logger.debug('Resetting main chain to: %s', entry.rhash); - while (!tip.isGenesis()) { + for (;;) { this.start(); if (tip.hash === entry.hash) { @@ -1355,6 +1355,8 @@ ChainDB.prototype.reset = co(function* reset(block) { break; } + assert(!tip.isGenesis()); + this.del(layout.H(tip.height)); this.del(layout.h(tip.hash)); this.del(layout.e(tip.hash)); @@ -1391,24 +1393,29 @@ ChainDB.prototype.reset = co(function* reset(block) { ChainDB.prototype.removeChain = co(function* removeChain(tip, entry) { this.logger.debug('Removing alternate chain: %s->%s', tip.rhash, entry.rhash); - while (!tip.isGenesis()) { + this.start(); + + for (;;) } if (tip.hash === entry.hash) break; - this.start(); + if (tip.isGenesis()) { + this.drop(); + throw new Error('Target entry not in chain.'); + } this.del(layout.h(tip.hash)); this.del(layout.e(tip.hash)); this.del(layout.b(tip.hash)); - yield this.commit(); - - this.cacheHash.remove(tip.hash); + this.cacheHash.unpush(tip.hash); tip = yield this.get(tip.prevBlock); assert(tip); } + yield this.commit(); + return tip; });