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

Use safe block as pivot block during snapsync #4819

Merged
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 @@ -65,8 +65,8 @@
import org.hyperledger.besu.ethereum.eth.sync.PivotBlockSelector;
import org.hyperledger.besu.ethereum.eth.sync.SyncMode;
import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotSelectorFromFinalizedBlock;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotSelectorFromPeers;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotSelectorFromSafeBlock;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.Checkpoint;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.ImmutableCheckpoint;
import org.hyperledger.besu.ethereum.eth.sync.fullsync.SyncTerminationCondition;
Expand Down Expand Up @@ -550,7 +550,7 @@ private PivotBlockSelector createPivotSelector(
LOG.info("Initial sync done, unsubscribe forkchoice supplier");
};

return new PivotSelectorFromFinalizedBlock(
return new PivotSelectorFromSafeBlock(
protocolContext,
protocolSchedule,
ethContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public boolean hasValidFinalizedBlockHash() {
return !finalizedBlockHash.equals(Hash.ZERO);
}

public boolean hasValidSafeBlockHash() {
return !safeBlockHash.equals(Hash.ZERO);
}

public Hash getHeadBlockHash() {
return headBlockHash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PivotSelectorFromFinalizedBlock implements PivotBlockSelector {
public class PivotSelectorFromSafeBlock implements PivotBlockSelector {

private static final Logger LOG = LoggerFactory.getLogger(PivotSelectorFromFinalizedBlock.class);
private static final Logger LOG = LoggerFactory.getLogger(PivotSelectorFromSafeBlock.class);
private final ProtocolContext protocolContext;
private final ProtocolSchedule protocolSchedule;
private final EthContext ethContext;
Expand All @@ -48,7 +48,7 @@ public class PivotSelectorFromFinalizedBlock implements PivotBlockSelector {

private volatile Optional<BlockHeader> maybeCachedHeadBlockHeader = Optional.empty();

public PivotSelectorFromFinalizedBlock(
public PivotSelectorFromSafeBlock(
final ProtocolContext protocolContext,
final ProtocolSchedule protocolSchedule,
final EthContext ethContext,
Expand All @@ -68,9 +68,8 @@ public PivotSelectorFromFinalizedBlock(
@Override
public Optional<FastSyncState> selectNewPivotBlock() {
final Optional<ForkchoiceEvent> maybeForkchoice = forkchoiceStateSupplier.get();
if (maybeForkchoice.isPresent() && maybeForkchoice.get().hasValidFinalizedBlockHash()) {
return Optional.of(
selectLastFinalizedBlockAsPivot(maybeForkchoice.get().getFinalizedBlockHash()));
if (maybeForkchoice.isPresent() && maybeForkchoice.get().hasValidSafeBlockHash()) {
return Optional.of(selectLastSafeBlockAsPivot(maybeForkchoice.get().getSafeBlockHash()));
}
LOG.debug("No finalized block hash announced yet");
return Optional.empty();
Expand All @@ -82,9 +81,9 @@ public CompletableFuture<Void> prepareRetry() {
return CompletableFuture.completedFuture(null);
}

private FastSyncState selectLastFinalizedBlockAsPivot(final Hash finalizedHash) {
LOG.debug("Returning finalized block hash {} as pivot", finalizedHash);
return new FastSyncState(finalizedHash);
private FastSyncState selectLastSafeBlockAsPivot(final Hash safeHash) {
LOG.debug("Returning safe block hash {} as pivot", safeHash);
return new FastSyncState(safeHash);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,16 @@ public void downloadPivotBlockHeaderShouldRetrievePivotBlockHash() {
when(genesisConfig.getTerminalBlockNumber()).thenReturn(OptionalLong.of(10L));

final Optional<ForkchoiceEvent> finalizedEvent =
Optional.of(new ForkchoiceEvent(null, null, blockchain.getBlockHashByNumber(2L).get()));
Optional.of(
new ForkchoiceEvent(
null,
blockchain.getBlockHashByNumber(3L).get(),
blockchain.getBlockHashByNumber(2L).get()));

fastSyncActions =
createFastSyncActions(
syncConfig,
new PivotSelectorFromFinalizedBlock(
new PivotSelectorFromSafeBlock(
blockchainSetupUtil.getProtocolContext(),
blockchainSetupUtil.getProtocolSchedule(),
ethContext,
Expand All @@ -471,13 +475,13 @@ public void downloadPivotBlockHeaderShouldRetrievePivotBlockHash() {
final RespondingEthPeer peer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1001);
final CompletableFuture<FastSyncState> result =
fastSyncActions.downloadPivotBlockHeader(
new FastSyncState(finalizedEvent.get().getFinalizedBlockHash()));
new FastSyncState(finalizedEvent.get().getSafeBlockHash()));
assertThat(result).isNotCompleted();

final RespondingEthPeer.Responder responder = RespondingEthPeer.blockchainResponder(blockchain);
peer.respond(responder);

assertThat(result).isCompletedWithValue(new FastSyncState(blockchain.getBlockHeader(2).get()));
assertThat(result).isCompletedWithValue(new FastSyncState(blockchain.getBlockHeader(3).get()));
}

private FastSyncActions createFastSyncActions(
Expand Down