chain locator.

This commit is contained in:
Christopher Jeffrey 2016-07-01 14:01:37 -07:00
parent e0373dbf65
commit 2b05bd0581
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1883,7 +1883,7 @@ Chain.prototype.getLocator = function getLocator(start, callback, force) {
if (start == null) if (start == null)
start = this.tip.hash; start = this.tip.hash;
return this.db.get(start, function(err, entry) { this.db.get(start, function(err, entry) {
if (err) if (err)
return callback(err); return callback(err);
@ -1898,45 +1898,47 @@ Chain.prototype.getLocator = function getLocator(start, callback, force) {
entry = self.tip; entry = self.tip;
} }
height = entry.height;
entry.isMainChain(function(err, main) { entry.isMainChain(function(err, main) {
if (err) if (err)
return callback(err); return callback(err);
(function next(err, entry) { (function next(err, hash) {
if (err) if (err)
return callback(err); return callback(err);
if (!entry) if (!hash)
return callback(null, hashes); return callback(null, hashes);
hashes.push(entry.hash); hashes.push(hash);
if (entry.height === 0) if (height === 0)
return callback(null, hashes); return callback(null, hashes);
height = Math.max(entry.height - step, 0); height = Math.max(height - step, 0);
if (hashes.length > 10) if (hashes.length > 10)
step *= 2; step *= 2;
if (height === 0) if (height === 0)
return next(null, { hash: self.network.genesis.hash, height: 0 }); return next(null, self.network.genesis.hash);
if (!main) // If we're on the main chain, we can
return entry.getAncestorByHeight(height, next); // do a fast lookup of the hash.
if (main)
return self.db.getHash(height, next);
// If we're in the main chain, we can entry.getAncestorByHeight(height, function(err, entry) {
// do an O(1) lookup of the hash.
self.db.getHash(height, function(err, hash) {
if (err) if (err)
return next(err); return callback(err);
if (!hash) if (!entry)
return next(); return next();
next(null, { hash: hash, height: height }); return next(null, entry.hash);
}); });
})(null, entry); })(null, entry.hash);
}); });
}); });
}; };