From 10fce032b77e6cfd0a8fe66c0bf6f3bb0bd9c545 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 4 Jan 2016 15:12:50 -0800 Subject: [PATCH] improve locatorHashes. fix utils ref in tx-pool. add utils.hash. --- lib/bcoin/chain.js | 27 +++++++++++++++++++++------ lib/bcoin/fullchain.js | 9 +++++---- lib/bcoin/pool.js | 10 ++++------ lib/bcoin/tx-pool.js | 1 + lib/bcoin/utils.js | 22 ++++++++++++++++++++++ 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 42404433..f579b044 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -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; diff --git a/lib/bcoin/fullchain.js b/lib/bcoin/fullchain.js index 9f914bf6..6d585354 100644 --- a/lib/bcoin/fullchain.js +++ b/lib/bcoin/fullchain.js @@ -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]) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 469e7d63..9b5dd87b 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -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(); diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index 1ec28b78..0d1a2e5e 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -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; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 038e8685..96f4f866 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -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; +};