From 41a864d130cf589a6817ff1b20b9d9e2ca3639d7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 7 May 2016 00:27:46 -0700 Subject: [PATCH] block sync. --- lib/bcoin/pool.js | 57 ++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 0aea4a6e..98ba0707 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -728,26 +728,30 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer, callback) { // Resolve orphan chain. if (self.chain.hasOrphan(hash)) { bcoin.debug('Peer sent a hash that is already a known orphan.'); - self.resolveOrphan(peer, null, hash, next); - return; + return self.resolveOrphan(peer, null, hash, next); } - // Normally we request the hashContinue. - // In the odd case where we already have - // it, we can do one of two things: either - // force re-downloading of the block to - // continue the sync, or do a getblocks - // from the last hash. - if (i === hashes.length - 1) { - // Request more hashes: - // self.getBlocks(peer, hash, null, next); - // Re-download the block (traditional method): - self.getData(peer, self.block.type, hash, { force: true }, next); - return; - } + self.getData(peer, self.block.type, hash, function(err, exists) { + if (err) + return next(err); - // Request block. - self.getData(peer, self.block.type, hash, next); + // Normally we request the hashContinue. + // In the odd case where we already have + // it, we can do one of two things: either + // force re-downloading of the block to + // continue the sync, or do a getblocks + // from the last hash (this will reset + // the hashContinue on the remote node). + if (exists && i === hashes.length - 1) { + // Request more hashes: + self.getBlocks(peer, hash, null, next); + // Re-download the block (traditional method): + // self.getData(peer, self.block.type, hash, true, next); + return; + } + + next(); + }); }, function(err) { if (err) return callback(err); @@ -1562,7 +1566,7 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) { if (typeof options === 'function') { callback = options; - options = {}; + options = null; } callback = utils.ensure(callback); @@ -1570,23 +1574,26 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) { if (this.destroyed) return callback(); - if (!options) + if (options == null) options = {}; + if (typeof options === 'boolean') + options = { force: options }; + function done(err, exists) { if (err) return callback(err); if (exists) - return callback(); + return callback(null, true); if (self.request.map[hash]) - return callback(); + return callback(null, true); item = new LoadRequest(self, peer, type, hash); if (options.noQueue) - return callback(); + return callback(null, false); if (type === self.tx.type) { if (peer.queue.tx.length === 0) { @@ -1604,12 +1611,12 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) { peer.queue.tx.push(item.start()); - return callback(); + return callback(null, false); } peer.queue.block.push(item); - return callback(); + return callback(null, false); } if (options.force) { @@ -1721,7 +1728,7 @@ Pool.prototype.getBlock = function getBlock(hash, callback) { if (!this.peers.load) return setTimeout(this.getBlock.bind(this, hash, callback), 1000); - this.getData(this.peers.load, this.block.type, hash, { force: true }, function(block) { + this.getData(this.peers.load, this.block.type, hash, true, function(block) { callback(null, block); });