move all block sync logic to pool.

This commit is contained in:
Christopher Jeffrey 2015-12-29 18:40:31 -08:00
parent 22427fe78f
commit 350ca9c875
2 changed files with 27 additions and 32 deletions

View File

@ -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;

View File

@ -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) {