Skip to content
This repository was archived by the owner on Sep 26, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public class EthPeers {
public static final Comparator<EthPeer> CHAIN_HEIGHT =
Comparator.comparing(((final EthPeer p) -> p.chainState().getEstimatedHeight()));

private static final Comparator<EthPeer> HIGHEST_TOTAL_DIFFICULTY_PEER =
TOTAL_DIFFICULTY.thenComparing(CHAIN_HEIGHT);

public static final Comparator<EthPeer> BEST_CHAIN = CHAIN_HEIGHT.thenComparing(TOTAL_DIFFICULTY);
public static final Comparator<EthPeer> BEST_CHAIN = TOTAL_DIFFICULTY.thenComparing(CHAIN_HEIGHT);

public static final Comparator<EthPeer> LEAST_TO_MOST_BUSY =
Comparator.comparing(EthPeer::outstandingRequests);
Expand Down Expand Up @@ -96,10 +93,6 @@ public Optional<EthPeer> bestPeer() {
return availablePeers().max(BEST_CHAIN);
}

public Optional<EthPeer> highestTotalDifficultyPeer() {
return availablePeers().max(HIGHEST_TOTAL_DIFFICULTY_PEER);
}

public Optional<EthPeer> idlePeer() {
return idlePeers().min(LEAST_TO_MOST_BUSY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public CompletableFuture<FastSyncState> selectPivotBlock(final FastSyncState fas
private CompletableFuture<FastSyncState> selectPivotBlockFromPeers() {
return ethContext
.getEthPeers()
.highestTotalDifficultyPeer()
.bestPeer()
.filter(peer -> peer.chainState().hasEstimatedHeight())
.map(
peer -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import tech.pegasys.pantheon.ethereum.core.BlockDataGenerator;
import tech.pegasys.pantheon.util.uint.UInt256;

import org.junit.Before;
Expand All @@ -23,33 +22,25 @@
public class EthPeersTest {

private EthProtocolManager ethProtocolManager;
private BlockDataGenerator gen;

@Before
public void setup() {
gen = new BlockDataGenerator();
ethProtocolManager = EthProtocolManagerTestUtil.create();
}

@Test
public void comparesPeersWithHeightAndTd() {
// Set peerA with better height, lower td
final EthPeer peerA =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, UInt256.of(50), 0).getEthPeer();
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, UInt256.of(50), 20).getEthPeer();
final EthPeer peerB =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, UInt256.of(100), 0).getEthPeer();
peerA.chainState().update(gen.hash(), 20);
peerB.chainState().update(gen.hash(), 10);

// Sanity check
assertThat(peerA.chainState().getEstimatedHeight()).isEqualTo(20);
assertThat(peerB.chainState().getEstimatedHeight()).isEqualTo(10);
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, UInt256.of(100), 10).getEthPeer();

assertThat(EthPeers.CHAIN_HEIGHT.compare(peerA, peerB)).isGreaterThan(0);
assertThat(EthPeers.TOTAL_DIFFICULTY.compare(peerA, peerB)).isLessThan(0);

assertThat(EthPeers.BEST_CHAIN.compare(peerA, peerB)).isGreaterThan(0);
assertThat(EthPeers.BEST_CHAIN.compare(peerB, peerA)).isLessThan(0);
assertThat(EthPeers.BEST_CHAIN.compare(peerA, peerB)).isLessThan(0);
assertThat(EthPeers.BEST_CHAIN.compare(peerB, peerA)).isGreaterThan(0);
assertThat(EthPeers.BEST_CHAIN.compare(peerA, peerA)).isEqualTo(0);
assertThat(EthPeers.BEST_CHAIN.compare(peerB, peerB)).isEqualTo(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,9 @@ public void choosesBestPeerAsSyncTarget_byTdAndHeight() {

final Responder responder = RespondingEthPeer.blockchainResponder(otherBlockchain);
final RespondingEthPeer peerA =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, localTd.plus(100), 0);
peerA.getEthPeer().chainState().update(gen.hash(), 100);
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, localTd.plus(100), 100);
final RespondingEthPeer peerB =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, localTd.plus(200), 0);
peerA.getEthPeer().chainState().update(gen.hash(), 50);
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, localTd.plus(200), 50);

final ChainDownloader downloader = downloader();
downloader.start();
Expand All @@ -281,7 +279,7 @@ public void choosesBestPeerAsSyncTarget_byTdAndHeight() {
RespondingEthPeer.respondOnce(responder, peerA, peerB);
}
assertThat(syncState.syncTarget()).isPresent();
assertThat(syncState.syncTarget().get().peer()).isEqualTo(peerA.getEthPeer());
assertThat(syncState.syncTarget().get().peer()).isEqualTo(peerB.getEthPeer());
}

@Test
Expand All @@ -291,7 +289,7 @@ public void switchesSyncTarget_betterHeight() {

// Peer A is initially better
final RespondingEthPeer peerA =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, localTd.plus(200), 50);
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, localTd.plus(100), 60);
final RespondingEthPeer peerB =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, localTd.plus(100), 50);

Expand Down