handle chain forks from checkpoints. enforce one syncPeer.
This commit is contained in:
parent
4984c306b9
commit
34e90c6baa
@ -157,7 +157,7 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) {
|
||||
if (checkpoint) {
|
||||
this.emit('checkpoint', height, hash, checkpoint);
|
||||
if (hash !== checkpoint) {
|
||||
// this.resetLastCheckpoint(height);
|
||||
this.resetLastCheckpoint(height);
|
||||
this.emit('fork', height, hash, checkpoint);
|
||||
return new Error('Forked chain at checkpoint.');
|
||||
}
|
||||
|
||||
@ -62,8 +62,6 @@ function Pool(options) {
|
||||
this.bestHeight = 0;
|
||||
|
||||
this.peers = {
|
||||
// Peers that have a forked blockchain
|
||||
// bad: [],
|
||||
// Peers that are loading blocks themselves
|
||||
block: [],
|
||||
// Peers that are still connecting
|
||||
@ -348,6 +346,9 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
|
||||
});
|
||||
} else {
|
||||
peer.on('block', function(block) {
|
||||
if (self.syncPeer !== peer)
|
||||
return;
|
||||
|
||||
backoff = 0;
|
||||
|
||||
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) {
|
||||
// // self.chain.resetLastCheckpoint(height);
|
||||
// if (!self.syncPeer)
|
||||
// return;
|
||||
// self.peers.bad.push(self.syncPeer);
|
||||
// self.startSync();
|
||||
// });
|
||||
this.chain.on('fork', function(height, hash, checkpoint) {
|
||||
if (!self.syncPeer)
|
||||
return;
|
||||
var peer = self.syncPeer;
|
||||
delete self.syncPeer;
|
||||
peer.destroy();
|
||||
self.startSync();
|
||||
});
|
||||
|
||||
// Just FYI
|
||||
peer.on('reject', function(payload) {
|
||||
@ -436,8 +437,6 @@ Pool.prototype.bestPeer = function bestPeer() {
|
||||
this.peers.block.forEach(function(peer) {
|
||||
if (!peer.version || !peer.socket)
|
||||
return;
|
||||
// if (~self.peers.bad.indexOf(peer))
|
||||
// return;
|
||||
if (!best || peer.version.height > best.version.height)
|
||||
best = peer;
|
||||
});
|
||||
@ -452,6 +451,9 @@ Pool.prototype.startSync = function startSync(peer) {
|
||||
if (!this.options.fullNode)
|
||||
return;
|
||||
|
||||
if (this.syncPeer)
|
||||
return;
|
||||
|
||||
peer = peer || this.bestPeer();
|
||||
if (!peer)
|
||||
return;
|
||||
@ -472,6 +474,11 @@ Pool.prototype._removePeer = function _removePeer(peer) {
|
||||
|
||||
if (this.peers.load === peer)
|
||||
this.peers.load = null;
|
||||
|
||||
if (this.syncPeer === peer) {
|
||||
delete this.syncPeer;
|
||||
this.startSync();
|
||||
}
|
||||
};
|
||||
|
||||
Pool.prototype.watch = function watch(id) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user