3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Incorporate protocol version into download peer selection logic.

This commit is contained in:
Mike Hearn 2012-12-25 00:28:49 +00:00
parent ac61578e8a
commit ea3fca970c
2 changed files with 20 additions and 3 deletions

View File

@ -1110,10 +1110,21 @@ public class PeerGroup extends AbstractIdleService {
return null; return null;
// Make sure we don't select a peer that is behind/synchronizing itself. // Make sure we don't select a peer that is behind/synchronizing itself.
int mostCommonChainHeight = getMostCommonChainHeight(); int mostCommonChainHeight = getMostCommonChainHeight();
List<Peer> candidates = new ArrayList<Peer>();
for (Peer peer : peers) { for (Peer peer : peers) {
if (peer.getBestHeight() == mostCommonChainHeight) return peer; if (peer.getBestHeight() == mostCommonChainHeight) candidates.add(peer);
} }
throw new IllegalStateException("Unreachable"); // Of the candidates, find the highest protocol version.
int highestVersion = 0;
Peer leadingCandidate = null;
for (Peer peer : candidates) {
int v = peer.getPeerVersionMessage().clientVersion;
if (v > highestVersion) {
highestVersion = v;
leadingCandidate = peer;
}
}
return leadingCandidate;
} }
private static class PeerGroupThreadFactory implements ThreadFactory { private static class PeerGroupThreadFactory implements ThreadFactory {

View File

@ -419,8 +419,14 @@ public class PeerGroupTest extends TestWithNetworkConnections {
Peer c = PeerGroup.peerFromChannel(connectPeer(3, versionMessage3)); Peer c = PeerGroup.peerFromChannel(connectPeer(3, versionMessage3));
assertEquals(2, peerGroup.getMostCommonChainHeight()); assertEquals(2, peerGroup.getMostCommonChainHeight());
assertEquals(a, peerGroup.getDownloadPeer()); // No change yet. assertEquals(a, peerGroup.getDownloadPeer()); // No change yet.
Peer d = PeerGroup.peerFromChannel(connectPeer(4, versionMessage3)); PeerGroup.peerFromChannel(connectPeer(4, versionMessage3));
assertEquals(3, peerGroup.getMostCommonChainHeight()); assertEquals(3, peerGroup.getMostCommonChainHeight());
assertEquals(c, peerGroup.getDownloadPeer()); // Switch to first peer advertising new height. assertEquals(c, peerGroup.getDownloadPeer()); // Switch to first peer advertising new height.
// New peer with a higher protocol version but same chain height.
VersionMessage versionMessage4 = new VersionMessage(params, 3);
versionMessage4.clientVersion = 100000;
Peer d = PeerGroup.peerFromChannel(connectPeer(5, versionMessage4));
assertEquals(d, peerGroup.getDownloadPeer());
} }
} }