Skip to content

Commit

Permalink
Change the builderResultCache of ExecutionLayerBlockProductionManager…
Browse files Browse the repository at this point in the history
…Impl (#8766)

Change the builderResultCache of ExecutionLayerBlockProductionManagerImpl keys from slot to SlotAndBlockRoot. This allows the cache to return proposals based on slot and block root, which is required if there's multiple operators requesting different beacon block proposals.

fixes #8625
  • Loading branch information
David Ryan authored Oct 23, 2024
1 parent e2a9019 commit 21efe6e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public Function<SignedBlockContainer, List<BlobSidecar>> createBlobSidecarsSelec
// the blobs and the proofs wouldn't be part of the BlockContainer.
final BuilderPayloadOrFallbackData builderPayloadOrFallbackData =
executionLayerBlockProductionManager
.getCachedUnblindedPayload(slot)
.getCachedUnblindedPayload(block.getSlotAndBlockRoot())
.orElseThrow(
() ->
new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ protected BlockAndBlobSidecars createBlockAndBlobSidecars(
}

// simulate caching of the builder payload
when(executionLayer.getCachedUnblindedPayload(signedBlockContainer.getSlot()))
when(executionLayer.getCachedUnblindedPayload(
signedBlockContainer.getSignedBlock().getSlotAndBlockRoot()))
.thenReturn(builderPayload.map(BuilderPayloadOrFallbackData::create));

final List<BlobSidecar> blobSidecars =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void shouldCreateValidBlobSidecarsForBlindedBlock() {
final SignedBlockContainer block = blockAndBlobSidecars.block();
final List<BlobSidecar> blobSidecars = blockAndBlobSidecars.blobSidecars();

verify(executionLayer).getCachedUnblindedPayload(block.getSlot());
verify(executionLayer).getCachedUnblindedPayload(block.getSlotAndBlockRoot());

final SszList<SszKZGCommitment> expectedCommitments =
block.getSignedBlock().getMessage().getBody().getOptionalBlobKzgCommitments().orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.Eth1Data;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema;
Expand Down Expand Up @@ -817,7 +818,7 @@ void shouldFailCreatingBlobSidecarsIfBuilderBlobsBundleCommitmentsRootIsNotConsi
dataStructureUtil.randomBuilderBlobsBundle(3);

prepareCachedBuilderPayload(
signedBlindedBeaconBlock.getSlot(),
signedBlindedBeaconBlock.getSlotAndBlockRoot(),
dataStructureUtil.randomExecutionPayload(),
blobsBundle);

Expand All @@ -842,7 +843,7 @@ void shouldFailCreatingBlobSidecarsIfBuilderBlobsBundleProofsIsNotConsistent() {
when(blobsBundle.getBlobs()).thenReturn(dataStructureUtil.randomSszBlobs(2));

prepareCachedBuilderPayload(
signedBlindedBeaconBlock.getSlot(),
signedBlindedBeaconBlock.getSlotAndBlockRoot(),
dataStructureUtil.randomExecutionPayload(),
blobsBundle);

Expand All @@ -867,7 +868,7 @@ void shouldFailCreatingBlobSidecarsIfBuilderBlobsBundleBlobsIsNotConsistent() {
when(blobsBundle.getProofs()).thenReturn(dataStructureUtil.randomSszKZGProofs(2));

prepareCachedBuilderPayload(
signedBlindedBeaconBlock.getSlot(),
signedBlindedBeaconBlock.getSlotAndBlockRoot(),
dataStructureUtil.randomExecutionPayload(),
blobsBundle);

Expand Down Expand Up @@ -901,10 +902,14 @@ void shouldCreateBlobSidecarsForBlindedBlock(final boolean useLocalFallback) {
.toList(),
blobsBundle.getProofs().stream().map(SszKZGProof::getKZGProof).toList(),
blobsBundle.getBlobs().stream().toList());
prepareCachedFallbackData(slot, executionPayload, localFallbackBlobsBundle);
prepareCachedFallbackData(
signedBlindedBeaconBlock.getSlotAndBlockRoot(),
executionPayload,
localFallbackBlobsBundle);
} else {

prepareCachedBuilderPayload(slot, executionPayload, blobsBundle);
prepareCachedBuilderPayload(
signedBlindedBeaconBlock.getSlotAndBlockRoot(), executionPayload, blobsBundle);
}

final List<BlobSidecar> blobSidecars =
Expand Down Expand Up @@ -1281,20 +1286,23 @@ private void prepareCachedPayloadHeaderWithFallbackResult(
}

private void prepareCachedBuilderPayload(
final UInt64 slot,
final SlotAndBlockRoot slotAndBlockRoot,
final ExecutionPayload executionPayload,
final tech.pegasys.teku.spec.datastructures.builder.BlobsBundle blobsBundle) {
final BuilderPayload builderPayload =
SchemaDefinitionsDeneb.required(spec.atSlot(slot).getSchemaDefinitions())
SchemaDefinitionsDeneb.required(
spec.atSlot(slotAndBlockRoot.getSlot()).getSchemaDefinitions())
.getExecutionPayloadAndBlobsBundleSchema()
.create(executionPayload, blobsBundle);
when(executionLayer.getCachedUnblindedPayload(slot))
when(executionLayer.getCachedUnblindedPayload(slotAndBlockRoot))
.thenReturn(Optional.of(BuilderPayloadOrFallbackData.create(builderPayload)));
}

private void prepareCachedFallbackData(
final UInt64 slot, final ExecutionPayload executionPayload, final BlobsBundle blobsBundle) {
when(executionLayer.getCachedUnblindedPayload(slot))
final SlotAndBlockRoot slotAndBlockRoot,
final ExecutionPayload executionPayload,
final BlobsBundle blobsBundle) {
when(executionLayer.getCachedUnblindedPayload(slotAndBlockRoot))
.thenReturn(
Optional.of(
BuilderPayloadOrFallbackData.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot;
import tech.pegasys.teku.spec.datastructures.execution.BuilderBidOrFallbackData;
import tech.pegasys.teku.spec.datastructures.execution.BuilderPayloadOrFallbackData;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadContext;
Expand All @@ -40,7 +41,7 @@ public class ExecutionLayerBlockProductionManagerImpl
private final NavigableMap<UInt64, ExecutionPayloadResult> executionResultCache =
new ConcurrentSkipListMap<>();

private final NavigableMap<UInt64, BuilderPayloadOrFallbackData> builderResultCache =
private final NavigableMap<SlotAndBlockRoot, BuilderPayloadOrFallbackData> builderResultCache =
new ConcurrentSkipListMap<>();

private final ExecutionLayerChannel executionLayerChannel;
Expand All @@ -55,9 +56,8 @@ public void onSlot(final UInt64 slot) {
executionResultCache
.headMap(slot.minusMinZero(EXECUTION_RESULT_CACHE_RETENTION_SLOTS), false)
.clear();
builderResultCache
.headMap(slot.minusMinZero(BUILDER_RESULT_CACHE_RETENTION_SLOTS), false)
.clear();
final UInt64 slotMax = slot.minusMinZero(BUILDER_RESULT_CACHE_RETENTION_SLOTS);
builderResultCache.keySet().removeIf(key -> key.getSlot().isLessThan(slotMax));
}

@Override
Expand Down Expand Up @@ -92,13 +92,15 @@ public SafeFuture<BuilderPayloadOrFallbackData> getUnblindedPayload(
.builderGetPayload(signedBeaconBlock, this::getCachedPayloadResult)
.thenPeek(
builderPayloadOrFallbackData ->
builderResultCache.put(signedBeaconBlock.getSlot(), builderPayloadOrFallbackData))
builderResultCache.put(
signedBeaconBlock.getSlotAndBlockRoot(), builderPayloadOrFallbackData))
.alwaysRun(blockPublishingPerformance::builderGetPayload);
}

@Override
public Optional<BuilderPayloadOrFallbackData> getCachedUnblindedPayload(final UInt64 slot) {
return Optional.ofNullable(builderResultCache.get(slot));
public Optional<BuilderPayloadOrFallbackData> getCachedUnblindedPayload(
final SlotAndBlockRoot slotAndBlockRoot) {
return Optional.ofNullable(builderResultCache.get(slotAndBlockRoot));
}

private ExecutionPayloadResult executeLocalFlow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public UInt64 getSlot() {
return getMessage().getSlot();
}

@Override
public SlotAndBlockRoot getSlotAndBlockRoot() {
return getMessage().getSlotAndBlockRoot();
}

@Override
public Bytes32 getParentRoot() {
return getMessage().getParentRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ default Bytes32 getRoot() {
return getSignedBlock().getRoot();
}

default SlotAndBlockRoot getSlotAndBlockRoot() {
return getSignedBlock().getSlotAndBlockRoot();
}

default Optional<SszList<SszKZGProof>> getKzgProofs() {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot;
import tech.pegasys.teku.spec.datastructures.execution.BuilderPayloadOrFallbackData;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadContext;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadResult;
Expand Down Expand Up @@ -56,7 +57,8 @@ public SafeFuture<BuilderPayloadOrFallbackData> getUnblindedPayload(
}

@Override
public Optional<BuilderPayloadOrFallbackData> getCachedUnblindedPayload(final UInt64 slot) {
public Optional<BuilderPayloadOrFallbackData> getCachedUnblindedPayload(
final SlotAndBlockRoot slotAndBlockRoot) {
return Optional.empty();
}
};
Expand Down Expand Up @@ -93,5 +95,6 @@ SafeFuture<BuilderPayloadOrFallbackData> getUnblindedPayload(
* Requires {@link #getUnblindedPayload(SignedBeaconBlock, BlockPublishingPerformance)} to have
* been called first in order for a value to be present
*/
Optional<BuilderPayloadOrFallbackData> getCachedUnblindedPayload(UInt64 slot);
Optional<BuilderPayloadOrFallbackData> getCachedUnblindedPayload(
SlotAndBlockRoot slotAndBlockRoot);
}

0 comments on commit 21efe6e

Please sign in to comment.