pool: repurpose regular peers if no loader is available.

This commit is contained in:
Christopher Jeffrey 2016-08-27 15:53:04 -07:00
parent 2d61a43fba
commit dd53e43958
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 78 additions and 48 deletions

View File

@ -417,12 +417,6 @@ Peer.prototype._onAck = function _onAck(err) {
// Start syncing the chain. // Start syncing the chain.
this.sync(); this.sync();
// Ask for the mempool if we're synced.
if (this.network.requestMempool) {
if (this.isLoader() && this.chain.synced)
this.sendMempool();
}
this.logger.debug('Received verack (%s).', this.hostname); this.logger.debug('Received verack (%s).', this.hostname);
// Finally we can let the pool know // Finally we can let the pool know
@ -2397,6 +2391,12 @@ Peer.prototype.sync = function sync(callback) {
return; return;
} }
// Ask for the mempool if we're synced.
if (this.network.requestMempool) {
if (this.isLoader() && this.chain.synced)
this.sendMempool();
}
this.syncSent = true; this.syncSent = true;
if (this.options.headers) { if (this.options.headers) {

View File

@ -275,20 +275,8 @@ Pool.prototype._init = function _init() {
this.chain.on('full', function() { this.chain.on('full', function() {
self.stopTimeout(); self.stopTimeout();
self.stopInterval(); self.stopInterval();
self.sync();
if (!self.chain.synced) {
// Ask loader for a mempool snapshot.
if (self.network.requestMempool) {
if (self.peers.load)
self.peers.load.sendMempool();
}
// Ask all peers for their latest blocks.
self.sync();
}
self.emit('full'); self.emit('full');
self.logger.info('Chain is fully synced (height=%d).', self.chain.height); self.logger.info('Chain is fully synced (height=%d).', self.chain.height);
}); });
}; };
@ -626,21 +614,16 @@ Pool.prototype.addLoader = function addLoader() {
addr = this.getLoaderHost(); addr = this.getLoaderHost();
peer = this.peers.get(addr); peer = this.peers.get(addr);
if (peer) {
this.setLoader(peer);
return;
}
if (this.syncing) { if (this.syncing) {
this.startTimeout(); this.startTimeout();
this.startInterval(); this.startInterval();
} }
if (peer) {
this.logger.info('Repurposing peer for loader (%s).', peer.hostname);
this.peers.repurpose(peer);
this.fillPeers();
utils.nextTick(function() {
self.emit('loader', peer);
});
return;
}
peer = this.createPeer(addr); peer = this.createPeer(addr);
this.logger.info('Added loader peer (%s).', peer.hostname); this.logger.info('Added loader peer (%s).', peer.hostname);
@ -653,6 +636,33 @@ Pool.prototype.addLoader = function addLoader() {
}); });
}; };
/**
* Add a loader peer. Necessary for
* a sync to even begin.
* @private
*/
Pool.prototype.setLoader = function setLoader(peer) {
var self = this;
if (!this.loaded)
return;
if (this.syncing) {
this.startTimeout();
this.startInterval();
}
this.logger.info('Repurposing peer for loader (%s).', peer.hostname);
this.peers.repurpose(peer);
this.fillPeers();
peer.sync();
utils.nextTick(function() {
self.emit('loader', peer);
});
};
/** /**
* Start the blockchain sync. * Start the blockchain sync.
*/ */
@ -1029,6 +1039,18 @@ Pool.prototype.createPeer = function createPeer(addr, socket) {
var self = this; var self = this;
var peer = new bcoin.peer(this, addr, socket); var peer = new bcoin.peer(this, addr, socket);
peer.once('ack', function() {
if (!peer.outbound)
return;
// Attempt to promote from pending->regular
self.peers.promote(peer);
// If we don't have an ack'd loader yet, use this peer.
if (!self.peers.load || !self.peers.load.ack)
self.setLoader(peer);
});
peer.once('close', function() { peer.once('close', function() {
if (!self.loaded) { if (!self.loaded) {
self.removePeer(peer); self.removePeer(peer);
@ -1400,10 +1422,6 @@ Pool.prototype.addPeer = function addPeer() {
this.peers.addPending(peer); this.peers.addPending(peer);
peer.once('ack', function() {
self.peers.promote(peer);
});
utils.nextTick(function() { utils.nextTick(function() {
self.emit('peer', peer); self.emit('peer', peer);
}); });
@ -1944,6 +1962,13 @@ function PeerList(pool) {
this.map = {}; this.map = {};
} }
PeerList.prototype.addLoader = function addLoader(peer) {
this.load = peer;
this.all.push(peer);
assert(!this.map[peer.hostname]);
this.map[peer.hostname] = peer;
};
PeerList.prototype.addPending = function addPending(peer) { PeerList.prototype.addPending = function addPending(peer) {
this.pending.push(peer); this.pending.push(peer);
this.all.push(peer); this.all.push(peer);
@ -1951,6 +1976,13 @@ PeerList.prototype.addPending = function addPending(peer) {
this.map[peer.hostname] = peer; this.map[peer.hostname] = peer;
}; };
PeerList.prototype.addLeech = function addLeech(peer) {
this.leeches.push(peer);
this.all.push(peer);
assert(!this.map[peer.hostname]);
this.map[peer.hostname] = peer;
};
PeerList.prototype.promote = function promote(peer) { PeerList.prototype.promote = function promote(peer) {
if (utils.binaryRemove(this.pending, peer, compare)) if (utils.binaryRemove(this.pending, peer, compare))
utils.binaryInsert(this.regular, peer, compare); utils.binaryInsert(this.regular, peer, compare);
@ -1971,11 +2003,23 @@ PeerList.prototype.remove = function remove(peer) {
} }
}; };
PeerList.prototype.demoteLoader = function demoteLoader() {
var peer = this.load;
assert(peer);
this.load = null;
if (peer.ack)
utils.binaryInsert(this.regular, peer, compare);
else
utils.binaryInsert(this.pending, peer, compare);
};
PeerList.prototype.repurpose = function repurpose(peer) { PeerList.prototype.repurpose = function repurpose(peer) {
var r1, r2; var r1, r2;
assert(peer.outbound); assert(peer.outbound);
assert(!this.load);
if (this.load)
this.demoteLoader();
r1 = utils.binaryRemove(this.pending, peer, compare); r1 = utils.binaryRemove(this.pending, peer, compare);
r2 = utils.binaryRemove(this.regular, peer, compare); r2 = utils.binaryRemove(this.regular, peer, compare);
@ -1993,20 +2037,6 @@ PeerList.prototype.size = function size() {
return this.regular.length + this.pending.length; return this.regular.length + this.pending.length;
}; };
PeerList.prototype.addLeech = function addLeech(peer) {
this.leeches.push(peer);
this.all.push(peer);
assert(!this.map[peer.hostname]);
this.map[peer.hostname] = peer;
};
PeerList.prototype.addLoader = function addLoader(peer) {
this.load = peer;
this.all.push(peer);
assert(!this.map[peer.hostname]);
this.map[peer.hostname] = peer;
};
PeerList.prototype.get = function get(addr) { PeerList.prototype.get = function get(addr) {
return this.map[addr.hostname]; return this.map[addr.hostname];
}; };