Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAN-3239] Only set sync targets that have an estimated height value #115

Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -103,8 +103,8 @@ public void updateForAnnouncedBlock(

public void updateHeightEstimate(final long blockNumber) {
synchronized (this) {
estimatedHeightKnown = true;
if (blockNumber > estimatedHeight) {
estimatedHeightKnown = true;
estimatedHeight = blockNumber;
estimatedHeightListeners.forEach(e -> e.onEstimatedHeightChanged(estimatedHeight));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public FastSyncTargetManager(

@Override
protected CompletableFuture<Optional<EthPeer>> selectBestAvailableSyncTarget() {
final Optional<EthPeer> maybeBestPeer = ethContext.getEthPeers().bestPeer();
final Optional<EthPeer> maybeBestPeer = ethContext.getEthPeers().bestPeerWithHeightEstimate();
if (!maybeBestPeer.isPresent()) {
LOG.info("No sync target, wait for peers.");
return completedFuture(Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected Optional<SyncTarget> finalizeSelectedSyncTarget(final SyncTarget syncT

@Override
protected CompletableFuture<Optional<EthPeer>> selectBestAvailableSyncTarget() {
final Optional<EthPeer> maybeBestPeer = ethContext.getEthPeers().bestPeer();
final Optional<EthPeer> maybeBestPeer = ethContext.getEthPeers().bestPeerWithHeightEstimate();
if (!maybeBestPeer.isPresent()) {
LOG.info("No sync target, wait for peers.");
return completedFuture(Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public void statusReceivedUpdatesBestBlock() {
assertThat(chainState.getBestBlock().getTotalDifficulty()).isEqualTo(INITIAL_TOTAL_DIFFICULTY);
}

@Test
public void updateHeightEstimate_toZero() {
chainState.updateHeightEstimate(0L);
assertThat(chainState.hasEstimatedHeight()).isFalse();
assertThat(chainState.getEstimatedHeight()).isEqualTo(0L);
}

@Test
public void updateHeightEstimate_toNonZeroValue() {
chainState.updateHeightEstimate(1L);
assertThat(chainState.hasEstimatedHeight()).isTrue();
assertThat(chainState.getEstimatedHeight()).isEqualTo(1L);
}

@Test
public void updateBestBlockAndHeightFromHashAndHeight() {
final long blockNumber = 12;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.util.uint.UInt256;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -82,6 +83,34 @@ public void tearDown() {
ethProtocolManager.stop();
}

@Test
public void findSyncTarget_withHeightEstimates() {
when(localWorldState.isWorldStateAvailable(localBlockchain.getChainHeadHeader().getStateRoot()))
.thenReturn(true);
final RespondingEthPeer bestPeer =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, UInt256.MAX_VALUE, 1);

final CompletableFuture<SyncTarget> result = syncTargetManager.findSyncTarget(Optional.empty());
bestPeer.respond(responder);

assertThat(result)
.isCompletedWithValue(
new SyncTarget(bestPeer.getEthPeer(), localBlockchain.getBlockHeader(4L).get()));
}

@Test
public void findSyncTarget_noHeightEstimates() {
when(localWorldState.isWorldStateAvailable(localBlockchain.getChainHeadHeader().getStateRoot()))
.thenReturn(true);
final RespondingEthPeer bestPeer =
EthProtocolManagerTestUtil.createPeer(ethProtocolManager, UInt256.MAX_VALUE, 0);

final CompletableFuture<SyncTarget> result = syncTargetManager.findSyncTarget(Optional.empty());
bestPeer.respond(responder);

assertThat(result).isNotCompleted();
}

@Test
public void shouldDisconnectPeerIfWorldStateIsUnavailableForCommonAncestor() {
when(localWorldState.isWorldStateAvailable(localBlockchain.getChainHeadHeader().getStateRoot()))
Expand Down