From 350ca9c875e585482315da55937085aacd0b57bb Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 29 Dec 2015 18:40:31 -0800 Subject: [PATCH] move all block sync logic to pool. --- lib/bcoin/peer.js | 31 ------------------------------- lib/bcoin/pool.js | 28 +++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 4ed512b4..8e8aae2e 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -141,15 +141,6 @@ Peer.prototype._init = function init() { }); }; -Peer.prototype.startSync = function startSync() { - if (!this.pool.options.fullNode) - return; - - this.pool.emit('debug', 'version (%s): sending locator hashes', this.address); - - this.loadBlocks(this.chain.locatorHashes(), 0); -}; - Peer.prototype.broadcast = function broadcast(items) { var self = this; var result; @@ -515,28 +506,6 @@ Peer.prototype._handleInv = function handleInv(items) { this.emit('blocks', blocks); - if (this.pool.options.fullNode) { - req = []; - for (i = 0; i < blocks.length; i++) { - block = blocks[i]; - hash = utils.toHex(block); - if (this.chain.hasOrphan(hash)) { - this.loadBlocks(this.chain.locatorHashes(), this.chain.getOrphanRoot(hash)); - continue; - } - if (!this.chain.hasBlock(hash)) { - req.push({ type: 'block', hash: block }); - continue; - } - if (i === blocks.length - 1) { - this.loadBlocks(this.chain.locatorHashes(), 0); - continue; - } - } - if (req.length) - this.getData(req); - } - if (txs.length === 0) return; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 7681c754..aa377f34 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -517,7 +517,33 @@ Pool.prototype.startSync = function startSync(peer) { this.syncPeer = peer; - peer.startSync(); + peer.on('blocks', function(hashes) { + var req = []; + var i, hash; + + for (i = 0; i < hashes.length; i++) { + hash = hashes[i]; + if (peer.chain.hasOrphan(hash)) { + peer.loadBlocks(peer.chain.locatorHashes(), peer.chain.getOrphanRoot(hash)); + continue; + } + if (!peer.chain.hasBlock(hash)) { + req.push({ type: 'block', hash: hash }); + continue; + } + if (i === hashes.length - 1) { + peer.loadBlocks(peer.chain.locatorHashes(), 0); + continue; + } + } + + if (req.length) + peer.getData(req); + }); + + this.emit('debug', 'version (%s): sending locator hashes', peer.address); + + peer.loadBlocks(this.chain.locatorHashes(), 0); }; Pool.prototype._removePeer = function _removePeer(peer) {