pool: add startSync and stopSync.

This commit is contained in:
Christopher Jeffrey 2016-01-12 00:38:11 -08:00
parent f420b41907
commit f17f3b5bee

View File

@ -65,6 +65,8 @@ function Pool(options) {
if (!this.options.headers) if (!this.options.headers)
this.options.multiplePeers = false; this.options.multiplePeers = false;
this.syncing = false;
this.backoff = { this.backoff = {
delta: options.backoffDelta || 500, delta: options.backoffDelta || 500,
max: options.backoffMax || 5000 max: options.backoffMax || 5000
@ -201,6 +203,9 @@ Pool.prototype._startTimer = function _startTimer() {
this._stopTimer(); this._stopTimer();
function destroy() { function destroy() {
if (!self.syncing)
return;
// Chain is full and up-to-date // Chain is full and up-to-date
if (self.chain.isFull()) { if (self.chain.isFull()) {
self._stopTimer(); self._stopTimer();
@ -229,6 +234,8 @@ Pool.prototype._startInterval = function _startInterval() {
this._stopInterval(); this._stopInterval();
function load() { function load() {
if (!self.syncing)
return;
// self._load(); // self._load();
} }
@ -301,12 +308,16 @@ Pool.prototype._addLoader = function _addLoader() {
}); });
peer.once('ack', function() { peer.once('ack', function() {
if (!self.syncing)
return;
peer.updateWatch(); peer.updateWatch();
if (!self._load()) if (!self._load())
self._stopTimer(); self._stopTimer();
}); });
peer.on('merkleblock', function(block) { peer.on('merkleblock', function(block) {
if (!self.syncing)
return;
// If the peer sent us a block that was added // If the peer sent us a block that was added
// to the chain (not orphans), reset the timeout. // to the chain (not orphans), reset the timeout.
if (self._handleBlock(block, peer)) { if (self._handleBlock(block, peer)) {
@ -316,6 +327,8 @@ Pool.prototype._addLoader = function _addLoader() {
}); });
peer.on('block', function(block) { peer.on('block', function(block) {
if (!self.syncing)
return;
// If the peer sent us a block that was added // If the peer sent us a block that was added
// to the chain (not orphans), reset the timeout. // to the chain (not orphans), reset the timeout.
if (self._handleBlock(block, peer)) { if (self._handleBlock(block, peer)) {
@ -326,20 +339,50 @@ Pool.prototype._addLoader = function _addLoader() {
if (self.options.headers) { if (self.options.headers) {
peer.on('blocks', function(hashes) { peer.on('blocks', function(hashes) {
if (!self.syncing)
return;
self._handleInv(hashes, peer); self._handleInv(hashes, peer);
}); });
peer.on('headers', function(headers) { peer.on('headers', function(headers) {
if (!self.syncing)
return;
self._handleHeaders(headers, peer); self._handleHeaders(headers, peer);
}); });
} else { } else {
peer.on('blocks', function(hashes) { peer.on('blocks', function(hashes) {
if (!self.syncing)
return;
self._handleBlocks(hashes, peer); self._handleBlocks(hashes, peer);
}); });
} }
};
Pool.prototype.startSync = function startSync() {
this.syncing = true;
this._startInterval(); this._startInterval();
this._startTimer(); this._startTimer();
if (!this.peers.load) {
this._addLoader();
return;
}
if (this.peers.load.ack) {
if (!this._load())
this._stopTimer();
}
};
Pool.prototype.stopSync = function stopSync() {
if (!this.syncing)
return;
this.syncing = false;
this._stopInterval();
this._stopTimer();
}; };
Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) { Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
@ -574,6 +617,9 @@ Pool.prototype._load = function _load() {
var self = this; var self = this;
var next; var next;
if (!this.syncing)
return true;
if (this.request.queue.length >= this.load.hwm) { if (this.request.queue.length >= this.load.hwm) {
this.load.hiReached = true; this.load.hiReached = true;
return false; return false;
@ -1000,7 +1046,6 @@ Pool.prototype.searchWallet = function(w) {
); );
}); });
// this.search(ts);
this.chain.resetTime(ts); this.chain.resetTime(ts);
}; };
@ -1085,10 +1130,12 @@ Pool.prototype.search = function search(id, range, e) {
this.chain.resetTime(range.start); this.chain.resetTime(range.start);
this.stopSync();
if (this.peers.load) if (this.peers.load)
this.peers.load.destroy(); this.peers.load.destroy();
this._load(); this.startSync();
return e; return e;
} }