improve locatorHashes. fix utils ref in tx-pool. add utils.hash.
This commit is contained in:
parent
d0cbc8f358
commit
10fce032b7
@ -627,12 +627,27 @@ Chain.prototype.getStartHeight = function getStartHeight() {
|
|||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.locatorHashes = function locatorHashes(start) {
|
Chain.prototype.locatorHashes = function locatorHashes(obj) {
|
||||||
if (start != null) {
|
var start;
|
||||||
if (typeof start === 'number')
|
|
||||||
start = this.byHeight(start);
|
if (obj) {
|
||||||
else
|
if (Array.isArray(obj))
|
||||||
start = this.byHash(start);
|
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)
|
if (start)
|
||||||
start = start.index;
|
start = start.index;
|
||||||
|
|||||||
@ -431,14 +431,15 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof start === 'string') {
|
if (typeof start === 'string') {
|
||||||
// Hash
|
top = this.index.heights[start];
|
||||||
if (this.index.heights[start] != null)
|
if (top == null)
|
||||||
top = this.index.heights[start];
|
return [start];
|
||||||
} else if (typeof start === 'number') {
|
} else if (typeof start === 'number') {
|
||||||
// Height
|
|
||||||
top = start;
|
top = start;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(chain[top]);
|
||||||
|
|
||||||
i = top;
|
i = top;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (chain[i])
|
if (chain[i])
|
||||||
|
|||||||
@ -353,12 +353,10 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Restart the getheaders process
|
// Restart the getheaders process
|
||||||
if (last && headers.length === 2000) {
|
// chain.locatorHashes will return [last.hash('hex')] here for now because
|
||||||
// Ideally we should use chain.locatorHashes here, but we can't since we
|
// the headers have not been added to the chain in non-headers-first mode.
|
||||||
// didn't add the headers to the chain (in non-headers-first mode)
|
if (last && headers.length === 2000)
|
||||||
// peer.loadHeaders(this.chain.locatorHashes(last), null);
|
peer.loadHeaders(this.chain.locatorHashes(last), null);
|
||||||
peer.loadHeaders([last.hash('hex')], null);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push our getdata packet
|
// Push our getdata packet
|
||||||
this._scheduleRequests();
|
this._scheduleRequests();
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
var bn = require('bn.js');
|
var bn = require('bn.js');
|
||||||
var inherits = require('inherits');
|
var inherits = require('inherits');
|
||||||
var bcoin = require('../bcoin');
|
var bcoin = require('../bcoin');
|
||||||
|
var utils = bcoin.utils;
|
||||||
var assert = bcoin.utils.assert;
|
var assert = bcoin.utils.assert;
|
||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
|
|||||||
@ -830,3 +830,25 @@ utils.testTarget = function testTarget(target, hash) {
|
|||||||
utils.now = function now() {
|
utils.now = function now() {
|
||||||
return +new Date() / 1000 | 0;
|
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;
|
||||||
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user