chain: refactor getLocator.

This commit is contained in:
Christopher Jeffrey 2017-06-16 00:04:26 -07:00
parent fb262042a3
commit 71f8b25d3c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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;