diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index d54a80dc..ebde3c3a 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -601,12 +601,11 @@ Peer.prototype._handleInv = function handleInv(items) { }; Peer.prototype._handleHeaders = function handleHeaders(headers) { - var self = this; - headers = headers.map(function(header) { + header._hash = utils.dsha256(header._raw); + header.hash = utils.toHex(header._hash); header.prevBlock = utils.toHex(header.prevBlock); header.merkleRoot = utils.toHex(header.merkleRoot); - header.hash = utils.toHex(utils.dsha256(header._raw)); return header; }); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 71ed95e8..7bd0ecb0 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -63,7 +63,7 @@ function Pool(options) { options.headers = true; } else { if (options.headers == null) - options.headers = false; + options.headers = true; } this.syncing = false; @@ -534,18 +534,20 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) { this.chain.onFlush(function() { for (i = 0; i < headers.length; i++) { - block = bcoin.block(headers[i], 'header'); + header = headers[i]; - if (last && block.prevBlock !== last.hash('hex')) + if (last && header.prevBlock !== last.hash) break; - if (!block.verify()) + if (!utils.testTarget(header.bits, header._hash)) { + utils.debug('Header failed POW test.'); break; + } - if (!self.chain.has(block)) - self.getData(peer, self.block.type, block.hash('hex')); + if (!self.chain.has(header.hash)) + self.getData(peer, self.block.type, header.hash); - last = block; + last = header; } // Restart the getheaders process @@ -556,7 +558,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) { // simply tries to find the latest block in // the peer's chain. if (last && headers.length === 2000) - self.getHeaders(peer, last, null); + self.getHeaders(peer, last.hash, null); }); // Reset interval to avoid calling getheaders unnecessarily diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index f82214cf..44d1c307 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -876,9 +876,7 @@ utils.testTarget = function testTarget(target, hash) { if (typeof hash === 'string') hash = new Buffer(hash, 'hex'); - hash = Array.prototype.slice.call(hash); - - return new bn(hash.slice().reverse()).cmp(target) < 0; + return new bn(hash, 'le').cmp(target) < 0; }; utils.now = function now() {