handle chain forks from checkpoints. enforce one syncPeer.

This commit is contained in:
Christopher Jeffrey 2015-12-18 16:30:58 -08:00
parent 4984c306b9
commit 34e90c6baa
2 changed files with 20 additions and 13 deletions

View File

@ -157,7 +157,7 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) {
if (checkpoint) { if (checkpoint) {
this.emit('checkpoint', height, hash, checkpoint); this.emit('checkpoint', height, hash, checkpoint);
if (hash !== checkpoint) { if (hash !== checkpoint) {
// this.resetLastCheckpoint(height); this.resetLastCheckpoint(height);
this.emit('fork', height, hash, checkpoint); this.emit('fork', height, hash, checkpoint);
return new Error('Forked chain at checkpoint.'); return new Error('Forked chain at checkpoint.');
} }

View File

@ -62,8 +62,6 @@ function Pool(options) {
this.bestHeight = 0; this.bestHeight = 0;
this.peers = { this.peers = {
// Peers that have a forked blockchain
// bad: [],
// Peers that are loading blocks themselves // Peers that are loading blocks themselves
block: [], block: [],
// Peers that are still connecting // Peers that are still connecting
@ -348,6 +346,9 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
}); });
} else { } else {
peer.on('block', function(block) { peer.on('block', function(block) {
if (self.syncPeer !== peer)
return;
backoff = 0; backoff = 0;
var len = self.chain.index.hashes.length; var len = self.chain.index.hashes.length;
@ -371,14 +372,14 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
}); });
} }
// Bad peer. this.chain.on('fork', function(height, hash, checkpoint) {
// this.chain.on('fork', function(height, hash, checkpoint) { if (!self.syncPeer)
// // self.chain.resetLastCheckpoint(height); return;
// if (!self.syncPeer) var peer = self.syncPeer;
// return; delete self.syncPeer;
// self.peers.bad.push(self.syncPeer); peer.destroy();
// self.startSync(); self.startSync();
// }); });
// Just FYI // Just FYI
peer.on('reject', function(payload) { peer.on('reject', function(payload) {
@ -436,8 +437,6 @@ Pool.prototype.bestPeer = function bestPeer() {
this.peers.block.forEach(function(peer) { this.peers.block.forEach(function(peer) {
if (!peer.version || !peer.socket) if (!peer.version || !peer.socket)
return; return;
// if (~self.peers.bad.indexOf(peer))
// return;
if (!best || peer.version.height > best.version.height) if (!best || peer.version.height > best.version.height)
best = peer; best = peer;
}); });
@ -452,6 +451,9 @@ Pool.prototype.startSync = function startSync(peer) {
if (!this.options.fullNode) if (!this.options.fullNode)
return; return;
if (this.syncPeer)
return;
peer = peer || this.bestPeer(); peer = peer || this.bestPeer();
if (!peer) if (!peer)
return; return;
@ -472,6 +474,11 @@ Pool.prototype._removePeer = function _removePeer(peer) {
if (this.peers.load === peer) if (this.peers.load === peer)
this.peers.load = null; this.peers.load = null;
if (this.syncPeer === peer) {
delete this.syncPeer;
this.startSync();
}
}; };
Pool.prototype.watch = function watch(id) { Pool.prototype.watch = function watch(id) {