pool: refactor requests.
This commit is contained in:
parent
9215e4fe48
commit
136a48b4b4
@ -793,7 +793,8 @@ Pool.prototype.__handleHeaders = co(function* _handleHeaders(headers, peer) {
|
|||||||
|
|
||||||
last = hash;
|
last = hash;
|
||||||
|
|
||||||
yield this.getBlock(peer, hash);
|
if (!(yield this.chain.has(hash)))
|
||||||
|
this.getBlock(peer, hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schedule the getdata's we just added.
|
// Schedule the getdata's we just added.
|
||||||
@ -853,7 +854,11 @@ Pool.prototype._handleBlocks = co(function* _handleBlocks(hashes, peer) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
exists = yield this.getBlock(peer, hash);
|
// Request the block if we don't have it.
|
||||||
|
if (!(yield this.chain.has(hash))) {
|
||||||
|
this.getBlock(peer, hash);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Normally we request the hashContinue.
|
// Normally we request the hashContinue.
|
||||||
// In the odd case where we already have
|
// In the odd case where we already have
|
||||||
@ -862,15 +867,9 @@ Pool.prototype._handleBlocks = co(function* _handleBlocks(hashes, peer) {
|
|||||||
// continue the sync, or do a getblocks
|
// continue the sync, or do a getblocks
|
||||||
// from the last hash (this will reset
|
// from the last hash (this will reset
|
||||||
// the hashContinue on the remote node).
|
// the hashContinue on the remote node).
|
||||||
if (exists && i === hashes.length - 1) {
|
if (i === hashes.length - 1) {
|
||||||
// Make sure we _actually_ have this block.
|
this.logger.debug('Received existing hash (%s).', peer.hostname);
|
||||||
if (!this.requestMap[hash]) {
|
yield peer.getBlocks(hash, null);
|
||||||
this.logger.debug('Received existing hash (%s).', peer.hostname);
|
|
||||||
yield peer.getBlocks(hash, null);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Otherwise, we're still requesting it. Ignore.
|
|
||||||
this.logger.debug('Received requested hash (%s).', peer.hostname);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1223,7 +1222,7 @@ Pool.prototype.createPeer = function createPeer(addr, socket) {
|
|||||||
for (i = 0; i < txs.length; i++) {
|
for (i = 0; i < txs.length; i++) {
|
||||||
hash = txs[i];
|
hash = txs[i];
|
||||||
try {
|
try {
|
||||||
yield self.getTX(peer, hash);
|
self.getTX(peer, hash);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
self.emit('error', e);
|
self.emit('error', e);
|
||||||
}
|
}
|
||||||
@ -1389,8 +1388,12 @@ Pool.prototype._handleTX = co(function* _handleTX(tx, peer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.requestMissing && missing) {
|
if (this.options.requestMissing && missing) {
|
||||||
for (i = 0; i < missing.length; i++)
|
try {
|
||||||
yield this.getTX(peer, missing[i]);
|
for (i = 0; i < missing.length; i++)
|
||||||
|
this.getTX(peer, missing[i]);
|
||||||
|
} catch (e) {
|
||||||
|
this.emit('error', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('tx', tx, peer);
|
this.emit('tx', tx, peer);
|
||||||
@ -1572,7 +1575,7 @@ Pool.prototype.watchAddress = function watchAddress(address) {
|
|||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype.getBlock = co(function* getBlock(peer, hash) {
|
Pool.prototype.getBlock = function getBlock(peer, hash) {
|
||||||
var item;
|
var item;
|
||||||
|
|
||||||
if (!this.loaded)
|
if (!this.loaded)
|
||||||
@ -1581,15 +1584,13 @@ Pool.prototype.getBlock = co(function* getBlock(peer, hash) {
|
|||||||
if (peer.destroyed)
|
if (peer.destroyed)
|
||||||
throw new Error('Peer is already destroyed (getdata).');
|
throw new Error('Peer is already destroyed (getdata).');
|
||||||
|
|
||||||
if (yield this.hasBlock(hash))
|
if (this.requestMap[hash])
|
||||||
return true;
|
return;
|
||||||
|
|
||||||
item = new LoadRequest(this, peer, this.blockType, hash);
|
item = new LoadRequest(this, peer, this.blockType, hash);
|
||||||
|
|
||||||
peer.queueBlock.push(item);
|
peer.queueBlock.push(item);
|
||||||
|
};
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test whether the chain has or has seen an item.
|
* Test whether the chain has or has seen an item.
|
||||||
@ -1619,7 +1620,7 @@ Pool.prototype.hasBlock = co(function* hasBlock(hash) {
|
|||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype.getTX = co(function* getTX(peer, hash) {
|
Pool.prototype.getTX = function getTX(peer, hash) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var item;
|
var item;
|
||||||
|
|
||||||
@ -1650,7 +1651,7 @@ Pool.prototype.getTX = co(function* getTX(peer, hash) {
|
|||||||
peer.queueTX.push(item.start());
|
peer.queueTX.push(item.start());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
});
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test whether the mempool has or has seen an item.
|
* Test whether the mempool has or has seen an item.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user