Skip to content

Commit

Permalink
change
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Oct 23, 2024
1 parent 9f1823c commit 152667e
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ public class ValidatorApiHandler implements ValidatorApiChannel {
*/
private static final int DUTY_EPOCH_TOLERANCE = 1;

private final Map<UInt64, BlockContainerAndMetaData> createdBlocksBySlotCache =
private final Map<UInt64, SafeFuture<Optional<BlockContainerAndMetaData>>>
blockProductionFuturesBySlotCache = LimitedMap.createSynchronizedLRU(2);

private final Map<UInt64, Bytes32> createdBlockRootsBySlotCache =
LimitedMap.createSynchronizedLRU(2);

private final BlockProductionAndPublishingPerformanceFactory
Expand Down Expand Up @@ -336,17 +339,21 @@ public SafeFuture<Optional<BlockContainerAndMetaData>> createUnsignedBlock(
final BLSSignature randaoReveal,
final Optional<Bytes32> graffiti,
final Optional<UInt64> requestedBuilderBoostFactor) {
if (createdBlocksBySlotCache.containsKey(slot)) {
return SafeFuture.completedFuture(Optional.of(createdBlocksBySlotCache.get(slot)));
final SafeFuture<Optional<BlockContainerAndMetaData>> maybeProcessing =
blockProductionFuturesBySlotCache.putIfAbsent(slot, new SafeFuture<>());
if (maybeProcessing != null) {
return maybeProcessing;
}
final SafeFuture<Optional<BlockContainerAndMetaData>> blockProductionFuture =
blockProductionFuturesBySlotCache.get(slot);
LOG.info("Creating unsigned block for slot {}", slot);
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(slot));
if (isSyncActive()) {
return NodeSyncingException.failedFuture();
}
final BlockProductionPerformance blockProductionPerformance =
blockProductionAndPublishingPerformanceFactory.createForProduction(slot);
return forkChoiceTrigger
forkChoiceTrigger
.prepareForBlockProduction(slot, blockProductionPerformance)
.thenCompose(
__ ->
Expand All @@ -362,7 +369,9 @@ public SafeFuture<Optional<BlockContainerAndMetaData>> createUnsignedBlock(
requestedBuilderBoostFactor,
blockSlotState,
blockProductionPerformance))
.alwaysRun(blockProductionPerformance::complete);
.alwaysRun(blockProductionPerformance::complete)
.propagateTo(blockProductionFuture);
return blockProductionFuture;
}

private SafeFuture<Optional<BlockContainerAndMetaData>> createBlock(
Expand Down Expand Up @@ -394,7 +403,8 @@ private SafeFuture<Optional<BlockContainerAndMetaData>> createBlock(
blockProductionPerformance)
.thenApply(
block -> {
createdBlocksBySlotCache.put(slot, block);
final Bytes32 blockRoot = block.blockContainer().getBlock().getRoot();
createdBlockRootsBySlotCache.put(slot, blockRoot);
return Optional.of(block);
});
}
Expand Down Expand Up @@ -871,11 +881,7 @@ private List<ProposerDuty> getProposalSlotsForEpoch(final BeaconState state, fin
private boolean isLocallyCreatedBlock(final SignedBlockContainer blockContainer) {
final Bytes32 blockRoot = blockContainer.getSignedBlock().getMessage().getRoot();
final Bytes32 locallyCreatedBlockRoot =
createdBlocksBySlotCache
.get(blockContainer.getSlot())
.blockContainer()
.getBlock()
.getRoot();
createdBlockRootsBySlotCache.get(blockContainer.getSlot());
return Objects.equals(blockRoot, locallyCreatedBlockRoot);
}

Expand Down

0 comments on commit 152667e

Please sign in to comment.