Skip to content
Draft
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 @@ -797,6 +797,10 @@ public SafeFuture<Optional<List<ValidatorLivenessAtEpoch>>> getValidatorsLivenes
validatorIndices, epoch, chainDataProvider.getCurrentEpoch());
}

public boolean isBlockInProductionAtSlot(final UInt64 slot) {
return blockProductionPreparationContextBySlotCache.containsKey(slot);
}

private Optional<SubmitDataError> fromInternalValidationResult(
final InternalValidationResult internalValidationResult, final int resultIndex) {
if (!internalValidationResult.isReject()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Consensys Software Inc., 2025
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.statetransition.block;

import tech.pegasys.teku.infrastructure.unsigned.UInt64;

@FunctionalInterface
public interface BlockInProductionProvider {
boolean isBlockInProductionAtSlot(UInt64 slot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

package tech.pegasys.teku.statetransition.block;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.ethereum.events.SlotEventsChannel;
import tech.pegasys.teku.infrastructure.async.AsyncRunner;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.exceptions.ExceptionUtil;
import tech.pegasys.teku.infrastructure.logging.EventLogger;
Expand Down Expand Up @@ -64,6 +67,9 @@ public class BlockManager extends Service
private final Subscribers<PreImportBlockListener> preImportBlockSubscribers =
Subscribers.create(true);

private final AsyncRunner asyncRunner;

private final BlockInProductionProvider blockInProductionProvider;
private final Optional<BlockImportMetrics> blockImportMetrics;

public BlockManager(
Expand All @@ -76,6 +82,8 @@ public BlockManager(
final BlockValidator blockValidator,
final TimeProvider timeProvider,
final EventLogger eventLogger,
final BlockInProductionProvider blockInProductionProvider,
final AsyncRunner asyncRunner,
final Optional<BlockImportMetrics> blockImportMetrics) {
this.recentChainData = recentChainData;
this.blockImporter = blockImporter;
Expand All @@ -86,6 +94,8 @@ public BlockManager(
this.blockValidator = blockValidator;
this.timeProvider = timeProvider;
this.eventLogger = eventLogger;
this.blockInProductionProvider = blockInProductionProvider;
this.asyncRunner = asyncRunner;
this.blockImportMetrics = blockImportMetrics;
}

Expand Down Expand Up @@ -214,9 +224,30 @@ private SafeFuture<BlockImportResult> doImportBlock(
.or(() -> handleKnownBlock(block))
.orElseGet(
() ->
handleBlockImport(block, blockImportPerformance, blockBroadcastValidator, origin)
.thenPeek(
result -> lateBlockImportCheck(blockImportPerformance, block, result)));
deferIfBlockInProduction(
block.getSlot(),
() ->
handleBlockImport(
block, blockImportPerformance, blockBroadcastValidator, origin)
.thenPeek(
result ->
lateBlockImportCheck(blockImportPerformance, block, result))));
}

private SafeFuture<BlockImportResult> deferIfBlockInProduction(
final UInt64 blockSlot, final Supplier<SafeFuture<BlockImportResult>> blockImport) {
final boolean blockFromThePastWhileBlockIsInProduction =
recentChainData
.getCurrentSlot()
.map(
currentSlot ->
blockSlot.isLessThan(currentSlot)
&& blockInProductionProvider.isBlockInProductionAtSlot(currentSlot))
.orElse(false);
if (!blockFromThePastWhileBlockIsInProduction) {
return blockImport.get();
}
return asyncRunner.runAfterDelay(blockImport::get, Duration.ofSeconds(2));
}

private Optional<BlockImportResult> propagateInvalidity(final SignedBeaconBlock block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,8 @@ public void initBlockManager() {
blockValidator,
timeProvider,
EVENT_LOG,
slot -> validatorApiHandler.isBlockInProductionAtSlot(slot),
beaconAsyncRunner,
importMetrics);
if (spec.isMilestoneSupported(SpecMilestone.BELLATRIX)) {
final FailedExecutionPool failedExecutionPool =
Expand Down
Loading