Skip to content

Commit

Permalink
Use safe block as pivot block suring snapsync (hyperledger#4819)
Browse files Browse the repository at this point in the history
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
2 people authored and garyschulte committed Jan 14, 2023
1 parent 45e83a1 commit 364a808
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,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 @@ -523,7 +523,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

0 comments on commit 364a808

Please sign in to comment.