From 1839bb5725943a8c55e876dfa3bdfe028ed25fcb Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 2 Jun 2016 08:22:19 -0700 Subject: [PATCH] fix reorganize. --- lib/bcoin/chain.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 8b9ce7c3..666d572c 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -1002,20 +1002,18 @@ Chain.prototype._reorganize = function _reorganize(entry, block, callback) { function disconnect(callback) { var entries = []; - entries.push(tip); - (function collect(entry) { + if (entry.hash === fork.hash) + return finish(); + + entries.push(entry); + entry.getPrevious(function(err, entry) { if (err) return callback(err); assert(entry); - if (entry.hash === fork.hash) - return finish(); - - entries.push(entry); - collect(entry); }); })(tip); @@ -1032,23 +1030,28 @@ Chain.prototype._reorganize = function _reorganize(entry, block, callback) { var entries = []; (function collect(entry) { + if (entry.hash === fork.hash) + return finish(); + + entries.push(entry); + entry.getPrevious(function(err, entry) { if (err) return callback(err); assert(entry); - if (entry.hash === fork.hash) - return finish(); - - entries.push(entry); - collect(entry); }); })(entry); function finish() { entries = entries.slice().reverse(); + + // We don't want to connect the new tip here. + // That will be done outside in setBestChain. + entries.pop(); + utils.forEachSerial(entries, function(entry, next) { self.connect(entry, next); }, callback);