Fix FastSyncCheckpointHeaderManager to ensure that the first checkpoint header, if present, is always the common ancestor. (#750)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Adrian Sutton 6 years ago committed by GitHub
parent 8d3fb7e4ae
commit 87685b1983
  1. 9
      ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/fastsync/FastSyncChainDownloader.java
  2. 5
      ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/fastsync/FastSyncCheckpointHeaderManager.java
  3. 9
      ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/fastsync/FastSyncCheckpointHeaderManagerTest.java

@ -38,7 +38,6 @@ public class FastSyncChainDownloader<C> {
private final ProtocolContext<C> protocolContext; private final ProtocolContext<C> protocolContext;
private final EthContext ethContext; private final EthContext ethContext;
private final LabelledMetric<OperationTimer> ethTasksTimer; private final LabelledMetric<OperationTimer> ethTasksTimer;
private final BlockHeader pivotBlockHeader;
FastSyncChainDownloader( FastSyncChainDownloader(
final SynchronizerConfiguration config, final SynchronizerConfiguration config,
@ -53,7 +52,6 @@ public class FastSyncChainDownloader<C> {
this.protocolContext = protocolContext; this.protocolContext = protocolContext;
this.ethContext = ethContext; this.ethContext = ethContext;
this.ethTasksTimer = ethTasksTimer; this.ethTasksTimer = ethTasksTimer;
this.pivotBlockHeader = pivotBlockHeader;
chainDownloader = chainDownloader =
new ChainDownloader<>( new ChainDownloader<>(
config, config,
@ -86,12 +84,7 @@ public class FastSyncChainDownloader<C> {
private CompletableFuture<List<Block>> importBlocksForCheckpoints( private CompletableFuture<List<Block>> importBlocksForCheckpoints(
final List<BlockHeader> checkpointHeaders) { final List<BlockHeader> checkpointHeaders) {
if (checkpointHeaders.size() < 2) { if (checkpointHeaders.size() < 2) {
final BlockHeader chainHeadHeader = protocolContext.getBlockchain().getChainHeadHeader(); return CompletableFuture.completedFuture(emptyList());
if (chainHeadHeader.getNumber() < pivotBlockHeader.getNumber()) {
checkpointHeaders.add(0, chainHeadHeader);
} else {
return CompletableFuture.completedFuture(emptyList());
}
} }
final PipelinedImportChainSegmentTask<C, BlockWithReceipts> importTask = final PipelinedImportChainSegmentTask<C, BlockWithReceipts> importTask =
PipelinedImportChainSegmentTask.forCheckpoints( PipelinedImportChainSegmentTask.forCheckpoints(

@ -28,6 +28,8 @@ import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import com.google.common.collect.Lists;
class FastSyncCheckpointHeaderManager<C> extends CheckpointHeaderManager<C> { class FastSyncCheckpointHeaderManager<C> extends CheckpointHeaderManager<C> {
private final SynchronizerConfiguration config; private final SynchronizerConfiguration config;
private final BlockHeader pivotBlockHeader; private final BlockHeader pivotBlockHeader;
@ -58,6 +60,9 @@ class FastSyncCheckpointHeaderManager<C> extends CheckpointHeaderManager<C> {
if (shouldDownloadMoreCheckpoints() if (shouldDownloadMoreCheckpoints()
&& nextChainSegmentIncludesPivotBlock(lastSegmentEnd) && nextChainSegmentIncludesPivotBlock(lastSegmentEnd)
&& pivotBlockNotAlreadyIncluded(lastSegmentEnd)) { && pivotBlockNotAlreadyIncluded(lastSegmentEnd)) {
if (checkpointHeaders.isEmpty()) {
return Lists.newArrayList(lastHeader, pivotBlockHeader);
}
return concat(checkpointHeaders, pivotBlockHeader); return concat(checkpointHeaders, pivotBlockHeader);
} }
return checkpointHeaders; return checkpointHeaders;

@ -14,7 +14,6 @@ package tech.pegasys.pantheon.ethereum.eth.sync.fastsync;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@ -125,10 +124,10 @@ public class FastSyncCheckpointHeaderManagerTest {
} }
@Test @Test
public void shouldHaveOnlyPivotBlockWhenCommonAncestorImmediatelyBeforePivotBlock() { public void shouldHaveCommonAncestorAndPivotBlockWhenCommonAncestorImmediatelyBeforePivotBlock() {
final SyncTarget syncTarget = final BlockHeader commonAncestor = block(pivotBlockHeader.getNumber() - 1);
syncState.setSyncTarget(peer.getEthPeer(), block(pivotBlockHeader.getNumber() - 1)); final SyncTarget syncTarget = syncState.setSyncTarget(peer.getEthPeer(), commonAncestor);
assertCheckpointHeaders(syncTarget, singletonList(pivotBlockHeader)); assertCheckpointHeaders(syncTarget, asList(commonAncestor, pivotBlockHeader));
} }
private void assertCheckpointHeaders( private void assertCheckpointHeaders(

Loading…
Cancel
Save