Skip to content

Commit

Permalink
Caching changes in ExecutionLayerBlockProductionManager
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Oct 23, 2024
1 parent 21efe6e commit c763d1c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 65 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(block.getSlotAndBlockRoot())
.getCachedUnblindedPayload(slot)
.orElseThrow(
() ->
new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ protected BlockAndBlobSidecars createBlockAndBlobSidecars(
}

// simulate caching of the builder payload
when(executionLayer.getCachedUnblindedPayload(
signedBlockContainer.getSignedBlock().getSlotAndBlockRoot()))
when(executionLayer.getCachedUnblindedPayload(signedBlockContainer.getSlot()))
.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.getSlotAndBlockRoot());
verify(executionLayer).getCachedUnblindedPayload(block.getSlot());

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,7 +50,6 @@
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 @@ -818,7 +817,7 @@ void shouldFailCreatingBlobSidecarsIfBuilderBlobsBundleCommitmentsRootIsNotConsi
dataStructureUtil.randomBuilderBlobsBundle(3);

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

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

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

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

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

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

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

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

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

private void prepareCachedFallbackData(
final SlotAndBlockRoot slotAndBlockRoot,
final ExecutionPayload executionPayload,
final BlobsBundle blobsBundle) {
when(executionLayer.getCachedUnblindedPayload(slotAndBlockRoot))
final UInt64 slot, final ExecutionPayload executionPayload, final BlobsBundle blobsBundle) {
when(executionLayer.getCachedUnblindedPayload(slot))
.thenReturn(
Optional.of(
BuilderPayloadOrFallbackData.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 @@ -41,7 +40,7 @@ public class ExecutionLayerBlockProductionManagerImpl
private final NavigableMap<UInt64, ExecutionPayloadResult> executionResultCache =
new ConcurrentSkipListMap<>();

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

private final ExecutionLayerChannel executionLayerChannel;
Expand All @@ -56,13 +55,9 @@ public void onSlot(final UInt64 slot) {
executionResultCache
.headMap(slot.minusMinZero(EXECUTION_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
public Optional<ExecutionPayloadResult> getCachedPayloadResult(final UInt64 slot) {
return Optional.ofNullable(executionResultCache.get(slot));
builderResultCache
.headMap(slot.minusMinZero(BUILDER_RESULT_CACHE_RETENTION_SLOTS), false)
.clear();
}

@Override
Expand All @@ -72,35 +67,42 @@ public ExecutionPayloadResult initiateBlockProduction(
final boolean attemptBuilderFlow,
final Optional<UInt64> requestedBuilderBoostFactor,
final BlockProductionPerformance 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;
return executionResultCache.computeIfAbsent(
blockSlotState.getSlot(),
__ -> {
if (attemptBuilderFlow) {
return executeBuilderFlow(
context, blockSlotState, requestedBuilderBoostFactor, blockProductionPerformance);
} else {
return executeLocalFlow(context, blockSlotState, blockProductionPerformance);
}
});
}

@Override
public Optional<ExecutionPayloadResult> getCachedPayloadResult(final UInt64 slot) {
return Optional.ofNullable(executionResultCache.get(slot));
}

@Override
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(
signedBeaconBlock.getSlotAndBlockRoot(), builderPayloadOrFallbackData))
builderResultCache.put(slot, builderPayloadOrFallbackData))
.alwaysRun(blockPublishingPerformance::builderGetPayload);
}

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

private ExecutionPayloadResult executeLocalFlow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,6 @@ 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,10 +40,6 @@ 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,7 +19,6 @@
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 All @@ -34,11 +33,6 @@
public interface ExecutionLayerBlockProductionManager {
ExecutionLayerBlockProductionManager NOOP =
new ExecutionLayerBlockProductionManager() {
@Override
public Optional<ExecutionPayloadResult> getCachedPayloadResult(final UInt64 slot) {
return Optional.empty();
}

@Override
public ExecutionPayloadResult initiateBlockProduction(
final ExecutionPayloadContext context,
Expand All @@ -49,6 +43,11 @@ public ExecutionPayloadResult initiateBlockProduction(
return null;
}

@Override
public Optional<ExecutionPayloadResult> getCachedPayloadResult(final UInt64 slot) {
return Optional.empty();
}

@Override
public SafeFuture<BuilderPayloadOrFallbackData> getUnblindedPayload(
final SignedBeaconBlock signedBeaconBlock,
Expand All @@ -57,8 +56,7 @@ public SafeFuture<BuilderPayloadOrFallbackData> getUnblindedPayload(
}

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

0 comments on commit c763d1c

Please sign in to comment.