chain: refactor getLocator.
This commit is contained in:
parent
fb262042a3
commit
71f8b25d3c
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user