public void check(final BiConsumer<BlockHeader, Boolean> onNewPivotBlock) {
    if (isTimeToCheckAgain.compareAndSet(true, false)) {
      AtomicBoolean delayNextCheck = new AtomicBoolean(false);

      syncState
          .getPivotBlockNumber()
          .ifPresent(
              currentPivotBlockNumber -> {
                final long bestChainHeight = syncActions.getBestChainHeight();
                final long distanceNextPivotBlock =
                    bestChainHeight
                        - lastPivotBlockFound
                            .map(ProcessableBlockHeader::getNumber)
                            .orElse(currentPivotBlockNumber);

                final CompletableFuture<Void> searchForNewPivot;

                if (distanceNextPivotBlock > pivotBlockDistanceBeforeCaching) {
                  LOG.atDebug()
                      .setMessage(
                          "Searching for a new pivot: current pivot {} best chain height {} distance next pivot {} last pivot block found {}")
                      .addArgument(currentPivotBlockNumber)
                      .addArgument(bestChainHeight)
                      .addArgument(distanceNextPivotBlock)
                      .addArgument(this::logLastPivotBlockFound)
                      .log();

                  searchForNewPivot =
                      CompletableFuture.completedFuture(FastSyncState.EMPTY_SYNC_STATE)
                          .thenCompose(syncActions::selectPivotBlock)
                          .thenCompose(
                              fss -> {
                                if (isSamePivotBlock(fss)) {
                                  LOG.atDebug()
                                      .setMessage(
                                          "New pivot {} is equal to last found {}, nothing to do")
                                      .addArgument(fss::getPivotBlockHash)
                                      .addArgument(this::logLastPivotBlockFound)
                                      .log();
                                  return CompletableFuture.completedFuture(null);
                                }
                                return downloadNewPivotBlock(fss);
                              })
                          .whenComplete(
                              (unused, throwable) -> {
                                if (throwable != null) {
                                  LOG.debug("Error while searching for a new pivot", throwable);
                                }
                              });
                } else {
                  searchForNewPivot = CompletableFuture.completedFuture(null);
                }

                try {
                  searchForNewPivot
                      .thenRun(
                          () -> {
                            final long distance = bestChainHeight - currentPivotBlockNumber;
                            if (distance > pivotBlockWindowValidity) {
                              LOG.atDebug()
                                  .setMessage(
                                      "Switch to new pivot: current pivot {} is distant {} from current best chain height {} last pivot block found {}")
                                  .addArgument(currentPivotBlockNumber)
                                  .addArgument(distance)
                                  .addArgument(bestChainHeight)
                                  .addArgument(this::logLastPivotBlockFound)
                                  .log();
                              switchToNewPivotBlock(onNewPivotBlock);
                            }
                            // delay next check only if we are successful
                            delayNextCheck.set(true);
                          })
                      .get();
                } catch (InterruptedException | ExecutionException e) {
                  LOG.debug("Exception while searching for new pivot", e);
                }
              });

      scheduleNextCheck(delayNextCheck.get());
    }
  }
DynamicPivotBlockSelector is responsible for dynamically selecting the pivot block for Snapsync.
It uses an algorithm to find the most suitable pivot block based on the current network
conditions.
ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/DynamicPivotBlockSelector.java