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.
|
* Calculate chain locator without a lock.
|
||||||
* @method
|
* @method
|
||||||
* @private
|
* @private
|
||||||
* @param {Hash} start
|
* @param {Hash?} start
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -1860,10 +1860,8 @@ Chain.prototype._getLocator = co(function* getLocator(start) {
|
|||||||
|
|
||||||
entry = yield this.db.getEntry(start);
|
entry = yield this.db.getEntry(start);
|
||||||
|
|
||||||
if (!entry) {
|
if (!entry)
|
||||||
hashes.push(start);
|
throw new Error('Tip not found.');
|
||||||
entry = this.tip;
|
|
||||||
}
|
|
||||||
|
|
||||||
hash = entry.hash;
|
hash = entry.hash;
|
||||||
height = entry.height;
|
height = entry.height;
|
||||||
@ -1875,29 +1873,23 @@ Chain.prototype._getLocator = co(function* getLocator(start) {
|
|||||||
if (height === 0)
|
if (height === 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
height = Math.max(0, height - step);
|
height -= step;
|
||||||
|
|
||||||
|
if (height < 0)
|
||||||
|
height = 0;
|
||||||
|
|
||||||
if (hashes.length > 10)
|
if (hashes.length > 10)
|
||||||
step *= 2;
|
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 (main) {
|
||||||
|
// If we're on the main chain, we can
|
||||||
|
// do a fast lookup of the hash.
|
||||||
hash = yield this.db.getHash(height);
|
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;
|
return hashes;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user