From 71f8b25d3c22e599b57b33514dd1837f8ab73383 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 16 Jun 2017 00:04:26 -0700 Subject: [PATCH] chain: refactor getLocator. --- lib/blockchain/chain.js | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 0dd3c0ff..a3975da5 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -1844,7 +1844,7 @@ Chain.prototype.getLocator = co(function* getLocator(start) { * Calculate chain locator without a lock. * @method * @private - * @param {Hash} start + * @param {Hash?} start * @returns {Promise} */ @@ -1860,10 +1860,8 @@ Chain.prototype._getLocator = co(function* getLocator(start) { entry = yield this.db.getEntry(start); - if (!entry) { - hashes.push(start); - entry = this.tip; - } + if (!entry) + throw new Error('Tip not found.'); hash = entry.hash; height = entry.height; @@ -1875,29 +1873,23 @@ Chain.prototype._getLocator = co(function* getLocator(start) { if (height === 0) break; - height = Math.max(0, height - step); + height -= step; + + if (height < 0) + height = 0; if (hashes.length > 10) step *= 2; - if (height === 0) { - hash = this.network.genesis.hash; - continue; - } - - // If we're on the main chain, we can - // do a fast lookup of the hash. if (main) { + // If we're on the main chain, we can + // do a fast lookup of the hash. hash = yield this.db.getHash(height); - continue; + } else { + entry = yield entry.getAncestor(height); + assert(entry); + hash = entry.hash; } - - entry = yield entry.getAncestor(height); - - if (!entry) - break; - - hash = entry.hash; } return hashes;