chain: improve locatorHashes.

This commit is contained in:
Christopher Jeffrey 2016-01-04 16:33:52 -08:00
parent 74b1562a87
commit 2c3edd50aa
3 changed files with 20 additions and 10 deletions

View File

@ -641,16 +641,19 @@ Chain.prototype.locatorHashes = function locatorHashes(obj) {
if (obj != null) { if (obj != null) {
if (typeof obj === 'string') { if (typeof obj === 'string') {
start = this.byHash(obj); start = this.byHash(obj);
if (!start) if (start) {
return [obj]; start = start.index;
} else {
// return [obj];
start = obj;
}
} else if (typeof obj === 'number') { } else if (typeof obj === 'number') {
start = this.byHeight(obj); start = this.byHeight(obj);
if (start)
start = start.index;
} }
assert(start); assert(start != null);
if (start)
start = start.index;
} }
return bcoin.fullChain.prototype.locatorHashes.call(this, start); return bcoin.fullChain.prototype.locatorHashes.call(this, start);

View File

@ -432,8 +432,11 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
if (typeof start === 'string') { if (typeof start === 'string') {
top = this.index.heights[start]; top = this.index.heights[start];
if (top == null) if (top == null) {
return [start]; // return [start];
hashes.push(start);
top = chain.length - 1;
}
} else if (typeof start === 'number') { } else if (typeof start === 'number') {
top = start; top = start;
} }

View File

@ -353,8 +353,12 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
} }
// Restart the getheaders process // Restart the getheaders process
// chain.locatorHashes will return [last.hash('hex')] here for now because // Technically `last` is not indexed yet so
// the headers have not been added to the chain in non-headers-first mode. // the locator hashes will not be entirely
// accurate. However, it shouldn't matter
// that much since FindForkInGlobalIndex
// simply tries to find the latest block in
// the peer's chain.
if (last && headers.length === 2000) if (last && headers.length === 2000)
peer.loadHeaders(this.chain.locatorHashes(last), null); peer.loadHeaders(this.chain.locatorHashes(last), null);