improve locatorHashes. fix utils ref in tx-pool. add utils.hash.

This commit is contained in:
Christopher Jeffrey 2016-01-04 15:12:50 -08:00
parent d0cbc8f358
commit 10fce032b7
5 changed files with 53 additions and 16 deletions

View File

@ -627,12 +627,27 @@ Chain.prototype.getStartHeight = function getStartHeight() {
return 0;
};
Chain.prototype.locatorHashes = function locatorHashes(start) {
if (start != null) {
if (typeof start === 'number')
start = this.byHeight(start);
else
start = this.byHash(start);
Chain.prototype.locatorHashes = function locatorHashes(obj) {
var start;
if (obj) {
if (Array.isArray(obj))
obj = utils.toHex(obj);
else if (obj.hash)
obj = obj.hash('hex');
}
// Convert the start to indexes
if (obj != null) {
if (typeof obj === 'string') {
start = this.byHash(obj);
if (!start)
return [obj];
} else if (typeof obj === 'number') {
start = this.byHeight(obj);
}
assert(start);
if (start)
start = start.index;

View File

@ -431,14 +431,15 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
}
if (typeof start === 'string') {
// Hash
if (this.index.heights[start] != null)
top = this.index.heights[start];
top = this.index.heights[start];
if (top == null)
return [start];
} else if (typeof start === 'number') {
// Height
top = start;
}
assert(chain[top]);
i = top;
for (;;) {
if (chain[i])

View File

@ -353,12 +353,10 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
}
// Restart the getheaders process
if (last && headers.length === 2000) {
// Ideally we should use chain.locatorHashes here, but we can't since we
// didn't add the headers to the chain (in non-headers-first mode)
// peer.loadHeaders(this.chain.locatorHashes(last), null);
peer.loadHeaders([last.hash('hex')], null);
}
// chain.locatorHashes will return [last.hash('hex')] here for now because
// the headers have not been added to the chain in non-headers-first mode.
if (last && headers.length === 2000)
peer.loadHeaders(this.chain.locatorHashes(last), null);
// Push our getdata packet
this._scheduleRequests();

View File

@ -7,6 +7,7 @@
var bn = require('bn.js');
var inherits = require('inherits');
var bcoin = require('../bcoin');
var utils = bcoin.utils;
var assert = bcoin.utils.assert;
var EventEmitter = require('events').EventEmitter;

View File

@ -830,3 +830,25 @@ utils.testTarget = function testTarget(target, hash) {
utils.now = function now() {
return +new Date() / 1000 | 0;
};
utils.hash = function hash(obj, enc) {
if (!obj)
return obj;
if (typeof obj === 'string')
return enc === 'hex' ? obj : utils.toArray(obj, 'hex');
if (Array.isArray(obj))
return enc === 'hex' ? utils.toHex(obj) : obj;
if (typeof obj.hash === 'function')
return obj.hash(enc);
if (obj.hash)
return hash(obj.hash, enc);
if (obj._hash)
return hash(obj._hash, enc);
return obj;
};