From 9eed16cd5b62d48328d6f0beb0dac649f27834c4 Mon Sep 17 00:00:00 2001 From: Stefan Bratanov Date: Fri, 16 Jun 2023 16:02:38 +0300 Subject: [PATCH] Implement deneb builder changes in stub --- .../ExecutionLayerChannelStub.java | 72 ++++++++++++++----- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/executionlayer/ExecutionLayerChannelStub.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/executionlayer/ExecutionLayerChannelStub.java index 6c7aef5a705..d3f2c91bec0 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/executionlayer/ExecutionLayerChannelStub.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/executionlayer/ExecutionLayerChannelStub.java @@ -47,6 +47,7 @@ import tech.pegasys.teku.spec.config.SpecConfigBellatrix; import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob; import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer; +import tech.pegasys.teku.spec.datastructures.builder.BlindedBlobsBundle; import tech.pegasys.teku.spec.datastructures.builder.BuilderPayload; import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration; import tech.pegasys.teku.spec.datastructures.execution.BlobsBundle; @@ -60,7 +61,9 @@ import tech.pegasys.teku.spec.datastructures.execution.PowBlock; import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; import tech.pegasys.teku.spec.datastructures.util.BlobsUtil; +import tech.pegasys.teku.spec.schemas.SchemaDefinitions; import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix; +import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb; public class ExecutionLayerChannelStub implements ExecutionLayerChannel { private static final Logger LOG = LogManager.getLogger(); @@ -91,6 +94,8 @@ public class ExecutionLayerChannelStub implements ExecutionLayerChannel { // block and payload tracking private Optional lastBuilderPayloadToBeUnblinded = Optional.empty(); + private Optional + lastBuilderBlobsBundleToBeUnblinded = Optional.empty(); private Optional lastValidBlock = Optional.empty(); public ExecutionLayerChannelStub( @@ -215,13 +220,7 @@ public SafeFuture engineGetPayload( "getPayload not supported for non-Bellatrix milestones")); } - final Optional maybeHeadAndAttrs = - payloadIdToHeadAndAttrsCache.getCached(executionPayloadContext.getPayloadId()); - if (maybeHeadAndAttrs.isEmpty()) { - return SafeFuture.failedFuture(new RuntimeException("payloadId not found in cache")); - } - - final HeadAndAttributes headAndAttrs = maybeHeadAndAttrs.get(); + final HeadAndAttributes headAndAttrs = getCachedHeadAndAttributes(executionPayloadContext); final PayloadBuildingAttributes payloadAttributes = headAndAttrs.attributes; final List transactions = generateTransactions(slot, headAndAttrs); @@ -336,34 +335,55 @@ public SafeFuture builderGetHeader( executionPayloadContext, slot); - SafeFuture payloadHeaderFuture = + final SchemaDefinitions schemaDefinitions = spec.atSlot(slot).getSchemaDefinitions(); + final SafeFuture payloadHeaderFuture = engineGetPayload(executionPayloadContext, slot) - .thenApply(GetPayloadResponse::getExecutionPayload) .thenApply( - executionPayload -> { + getPayloadResponse -> { + final ExecutionPayload executionPayload = + getPayloadResponse.getExecutionPayload(); LOG.info( "getPayloadHeader: payloadId: {} slot: {} -> executionPayload blockHash: {}", executionPayloadContext, slot, executionPayload.getBlockHash()); lastBuilderPayloadToBeUnblinded = Optional.of(executionPayload); - return spec.atSlot(slot) - .getSchemaDefinitions() + lastBuilderBlobsBundleToBeUnblinded = + getPayloadResponse + .getBlobsBundle() + .map( + blobsBundle -> + SchemaDefinitionsDeneb.required(schemaDefinitions) + .getBlobsBundleSchema() + .createFromExecutionBlobsBundle(blobsBundle)); + return schemaDefinitions .toVersionBellatrix() .orElseThrow() .getExecutionPayloadHeaderSchema() .createFromExecutionPayload(executionPayload); }); - return payloadHeaderFuture.thenApply(HeaderWithFallbackData::create); + final HeadAndAttributes headAndAttrs = getCachedHeadAndAttributes(executionPayloadContext); + + final Optional blindedBlobsBundle = + headAndAttrs.currentBlobsBundle.map( + blobsBundle -> + SchemaDefinitionsDeneb.required(schemaDefinitions) + .getBlindedBlobsBundleSchema() + .createFromExecutionBlobsBundle(blobsBundle)); + + return payloadHeaderFuture.thenApply( + payloadHeader -> HeaderWithFallbackData.create(payloadHeader, blindedBlobsBundle)); } @Override public SafeFuture builderGetPayload( final SignedBlockContainer signedBlockContainer, final Function> getCachedPayloadResultFunction) { + final UInt64 slot = signedBlockContainer.getSlot(); + final SchemaDefinitions schemaDefinitions = spec.atSlot(slot).getSchemaDefinitions(); final Optional schemaDefinitionsBellatrix = - spec.atSlot(signedBlockContainer.getSlot()).getSchemaDefinitions().toVersionBellatrix(); + schemaDefinitions.toVersionBellatrix(); checkState( schemaDefinitionsBellatrix.isPresent(), @@ -394,11 +414,21 @@ public SafeFuture builderGetPayload( LOG.info( "proposeBlindedBlock: slot: {} block: {} -> unblinded executionPayload blockHash: {}", - signedBlockContainer.getSlot(), + slot, signedBlockContainer.getRoot(), lastBuilderPayloadToBeUnblinded.get().getBlockHash()); - return SafeFuture.completedFuture(lastBuilderPayloadToBeUnblinded.get()); + final BuilderPayload builderPayload = + lastBuilderBlobsBundleToBeUnblinded + .map( + blobsBundle -> + (BuilderPayload) + SchemaDefinitionsDeneb.required(schemaDefinitions) + .getExecutionPayloadAndBlobsBundleSchema() + .create(lastBuilderPayloadToBeUnblinded.get(), blobsBundle)) + .orElseGet(() -> lastBuilderPayloadToBeUnblinded.get()); + + return SafeFuture.completedFuture(builderPayload); } public PayloadStatus getPayloadStatus() { @@ -490,6 +520,16 @@ private void prepareTransitionBlocks(final UInt64 bellatrixActivationTime) { terminalBlockHash, TERMINAL_BLOCK_PARENT_HASH, terminalTotalDifficulty, transitionTime); } + private HeadAndAttributes getCachedHeadAndAttributes( + final ExecutionPayloadContext executionPayloadContext) { + final Bytes8 payloadId = executionPayloadContext.getPayloadId(); + return payloadIdToHeadAndAttrsCache + .getCached(payloadId) + .orElseThrow( + () -> + new RuntimeException(String.format("payloadId %s not found in cache", payloadId))); + } + private List generateTransactions( final UInt64 slot, final HeadAndAttributes headAndAttrs) { final List transactions = new ArrayList<>();