block sync.

This commit is contained in:
Christopher Jeffrey 2016-05-07 00:27:46 -07:00
parent 6fb0689ce4
commit 41a864d130
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -728,26 +728,30 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer, callback) {
// Resolve orphan chain. // Resolve orphan chain.
if (self.chain.hasOrphan(hash)) { if (self.chain.hasOrphan(hash)) {
bcoin.debug('Peer sent a hash that is already a known orphan.'); bcoin.debug('Peer sent a hash that is already a known orphan.');
self.resolveOrphan(peer, null, hash, next); return self.resolveOrphan(peer, null, hash, next);
return;
} }
// Normally we request the hashContinue. self.getData(peer, self.block.type, hash, function(err, exists) {
// In the odd case where we already have if (err)
// it, we can do one of two things: either return next(err);
// 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;
}
// Request block. // Normally we request the hashContinue.
self.getData(peer, self.block.type, hash, next); // 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) { }, function(err) {
if (err) if (err)
return callback(err); return callback(err);
@ -1562,7 +1566,7 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) {
if (typeof options === 'function') { if (typeof options === 'function') {
callback = options; callback = options;
options = {}; options = null;
} }
callback = utils.ensure(callback); callback = utils.ensure(callback);
@ -1570,23 +1574,26 @@ Pool.prototype.getData = function getData(peer, type, hash, options, callback) {
if (this.destroyed) if (this.destroyed)
return callback(); return callback();
if (!options) if (options == null)
options = {}; options = {};
if (typeof options === 'boolean')
options = { force: options };
function done(err, exists) { function done(err, exists) {
if (err) if (err)
return callback(err); return callback(err);
if (exists) if (exists)
return callback(); return callback(null, true);
if (self.request.map[hash]) if (self.request.map[hash])
return callback(); return callback(null, true);
item = new LoadRequest(self, peer, type, hash); item = new LoadRequest(self, peer, type, hash);
if (options.noQueue) if (options.noQueue)
return callback(); return callback(null, false);
if (type === self.tx.type) { if (type === self.tx.type) {
if (peer.queue.tx.length === 0) { 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()); peer.queue.tx.push(item.start());
return callback(); return callback(null, false);
} }
peer.queue.block.push(item); peer.queue.block.push(item);
return callback(); return callback(null, false);
} }
if (options.force) { if (options.force) {
@ -1721,7 +1728,7 @@ Pool.prototype.getBlock = function getBlock(hash, callback) {
if (!this.peers.load) if (!this.peers.load)
return setTimeout(this.getBlock.bind(this, hash, callback), 1000); 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); callback(null, block);
}); });