From 0af709388c7663db7db2f7d5d95cde07101325f3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 24 Feb 2016 06:54:27 -0800 Subject: [PATCH] update getheaders sync. --- lib/bcoin/peer.js | 9 +-------- lib/bcoin/pool.js | 15 +++++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 89ac9abc..77d60bf8 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -415,17 +415,14 @@ Peer.prototype._onPacket = function onPacket(packet) { return this._handleReject(payload); if (cmd === 'block') { - payload.network = true; payload.relayedBy = this.host || '0.0.0.0'; payload = bcoin.block(payload); } else if (cmd === 'merkleblock') { - payload.network = true; payload.relayedBy = this.host || '0.0.0.0'; payload = bcoin.merkleblock(payload); this.lastBlock = payload; return; } else if (cmd === 'tx') { - payload.network = true; payload.relayedBy = this.host || '0.0.0.0'; payload = bcoin.tx(payload, this.lastBlock); if (this.lastBlock) { @@ -602,11 +599,7 @@ Peer.prototype._handleInv = function handleInv(items) { Peer.prototype._handleHeaders = function handleHeaders(headers) { 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); - return header; + return new bcoin.headers(header); }); this.emit('headers', headers); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index edd593b9..bc145bff 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -539,19 +539,18 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) { this.chain.onFlush(function() { for (i = 0; i < headers.length; i++) { header = headers[i]; + hash = header.hash('hex'); - if (last && header.prevBlock !== last.hash) + if (last && header.prevBlock !== last) break; - if (!utils.testTarget(header.bits, header._hash)) { - utils.debug('Header failed POW test.'); + if (!header.verify()) break; - } - if (!self.chain.has(header.hash)) - self.getData(peer, self.block.type, header.hash); + if (!self.chain.has(hash)) + self.getData(peer, self.block.type, hash); - last = header; + last = hash; } // Restart the getheaders process @@ -562,7 +561,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.hash, null); + self.getHeaders(peer, last, null); }); // Reset interval to avoid calling getheaders unnecessarily