Skip to content

Commit

Permalink
As per feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Oct 23, 2024
1 parent c763d1c commit 073b83e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class ValidatorApiHandler implements ValidatorApiChannel {
*/
private static final int DUTY_EPOCH_TOLERANCE = 1;

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

private final BlockProductionAndPublishingPerformanceFactory
Expand Down Expand Up @@ -336,6 +336,9 @@ 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)));
}
LOG.info("Creating unsigned block for slot {}", slot);
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(slot));
if (isSyncActive()) {
Expand Down Expand Up @@ -391,8 +394,7 @@ private SafeFuture<Optional<BlockContainerAndMetaData>> createBlock(
blockProductionPerformance)
.thenApply(
block -> {
final Bytes32 blockRoot = block.blockContainer().getBlock().getRoot();
createdBlockRootsBySlotCache.put(slot, blockRoot);
createdBlocksBySlotCache.put(slot, block);
return Optional.of(block);
});
}
Expand Down Expand Up @@ -869,7 +871,11 @@ private List<ProposerDuty> getProposalSlotsForEpoch(final BeaconState state, fin
private boolean isLocallyCreatedBlock(final SignedBlockContainer blockContainer) {
final Bytes32 blockRoot = blockContainer.getSignedBlock().getMessage().getRoot();
final Bytes32 locallyCreatedBlockRoot =
createdBlockRootsBySlotCache.get(blockContainer.getSlot());
createdBlocksBySlotCache
.get(blockContainer.getSlot())
.blockContainer()
.getBlock()
.getRoot();
return Objects.equals(blockRoot, locallyCreatedBlockRoot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,20 @@ public void createUnsignedBlock_shouldCreateBlock() {
// even if passing a non-empty requestedBlinded and requestedBuilderBoostFactor isn't a valid
// combination,
// we still want to check that all parameters are passed down the line to the block factory
final SafeFuture<Optional<BlockContainerAndMetaData>> result =
SafeFuture<Optional<BlockContainerAndMetaData>> result =
validatorApiHandler.createUnsignedBlock(
newSlot, randaoReveal, Optional.empty(), Optional.of(ONE));

assertThat(result).isCompletedWithValue(Optional.of(blockContainerAndMetaData));

// further calls in the same slot should return the same block
result =
validatorApiHandler.createUnsignedBlock(
newSlot, randaoReveal, Optional.empty(), Optional.of(ONE));

assertThat(result).isCompletedWithValue(Optional.of(blockContainerAndMetaData));

// only produced once
verify(blockFactory)
.createUnsignedBlock(
blockSlotState,
Expand All @@ -555,7 +565,6 @@ public void createUnsignedBlock_shouldCreateBlock() {
Optional.empty(),
Optional.of(ONE),
BlockProductionPerformance.NOOP);
assertThat(result).isCompletedWithValue(Optional.of(blockContainerAndMetaData));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ public ExecutionPayloadResult initiateBlockProduction(
final boolean attemptBuilderFlow,
final Optional<UInt64> requestedBuilderBoostFactor,
final BlockProductionPerformance blockProductionPerformance) {
return executionResultCache.computeIfAbsent(
blockSlotState.getSlot(),
__ -> {
if (attemptBuilderFlow) {
return executeBuilderFlow(
context, blockSlotState, requestedBuilderBoostFactor, blockProductionPerformance);
} else {
return executeLocalFlow(context, blockSlotState, blockProductionPerformance);
}
});
final ExecutionPayloadResult result;
if (attemptBuilderFlow) {
result =
executeBuilderFlow(
context, blockSlotState, requestedBuilderBoostFactor, blockProductionPerformance);
} else {
result = executeLocalFlow(context, blockSlotState, blockProductionPerformance);
}
executionResultCache.put(blockSlotState.getSlot(), result);
return result;
}

@Override
Expand All @@ -88,15 +88,11 @@ public Optional<ExecutionPayloadResult> getCachedPayloadResult(final UInt64 slot
public SafeFuture<BuilderPayloadOrFallbackData> getUnblindedPayload(
final SignedBeaconBlock signedBeaconBlock,
final BlockPublishingPerformance blockPublishingPerformance) {
final UInt64 slot = signedBeaconBlock.getSlot();
if (builderResultCache.containsKey(slot)) {
return SafeFuture.completedFuture(builderResultCache.get(slot));
}
return executionLayerChannel
.builderGetPayload(signedBeaconBlock, this::getCachedPayloadResult)
.thenPeek(
builderPayloadOrFallbackData ->
builderResultCache.put(slot, builderPayloadOrFallbackData))
builderResultCache.put(signedBeaconBlock.getSlot(), builderPayloadOrFallbackData))
.alwaysRun(blockPublishingPerformance::builderGetPayload);
}

Expand Down

0 comments on commit 073b83e

Please sign in to comment.