From 982490835770784fdbe7b40a7b84980b75a3d546 Mon Sep 17 00:00:00 2001 From: Stefan Bratanov Date: Wed, 23 Oct 2024 18:31:49 +0800 Subject: [PATCH] change --- .../coordinator/ValidatorApiHandler.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java b/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java index 0f589a9327e..892bdaa483f 100644 --- a/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java +++ b/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java @@ -122,7 +122,10 @@ public class ValidatorApiHandler implements ValidatorApiChannel { */ private static final int DUTY_EPOCH_TOLERANCE = 1; - private final Map createdBlocksBySlotCache = + private final Map>> + blockProductionFuturesBySlotCache = LimitedMap.createSynchronizedLRU(2); + + private final Map createdBlockRootsBySlotCache = LimitedMap.createSynchronizedLRU(2); private final BlockProductionAndPublishingPerformanceFactory @@ -336,9 +339,13 @@ public SafeFuture> createUnsignedBlock( final BLSSignature randaoReveal, final Optional graffiti, final Optional requestedBuilderBoostFactor) { - if (createdBlocksBySlotCache.containsKey(slot)) { - return SafeFuture.completedFuture(Optional.of(createdBlocksBySlotCache.get(slot))); + final SafeFuture> maybeProcessing = + blockProductionFuturesBySlotCache.putIfAbsent(slot, new SafeFuture<>()); + if (maybeProcessing != null) { + return maybeProcessing; } + final SafeFuture> blockProductionFuture = + blockProductionFuturesBySlotCache.get(slot); LOG.info("Creating unsigned block for slot {}", slot); performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(slot)); if (isSyncActive()) { @@ -346,7 +353,7 @@ public SafeFuture> createUnsignedBlock( } final BlockProductionPerformance blockProductionPerformance = blockProductionAndPublishingPerformanceFactory.createForProduction(slot); - return forkChoiceTrigger + forkChoiceTrigger .prepareForBlockProduction(slot, blockProductionPerformance) .thenCompose( __ -> @@ -362,7 +369,9 @@ public SafeFuture> createUnsignedBlock( requestedBuilderBoostFactor, blockSlotState, blockProductionPerformance)) - .alwaysRun(blockProductionPerformance::complete); + .alwaysRun(blockProductionPerformance::complete) + .propagateTo(blockProductionFuture); + return blockProductionFuture; } private SafeFuture> createBlock( @@ -394,7 +403,8 @@ private SafeFuture> createBlock( blockProductionPerformance) .thenApply( block -> { - createdBlocksBySlotCache.put(slot, block); + final Bytes32 blockRoot = block.blockContainer().getBlock().getRoot(); + createdBlockRootsBySlotCache.put(slot, blockRoot); return Optional.of(block); }); } @@ -871,11 +881,7 @@ private List 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); }