|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2023 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.ethereum.executionclient.methods; |
| 15 | + |
| 16 | +import org.apache.logging.log4j.LogManager; |
| 17 | +import org.apache.logging.log4j.Logger; |
| 18 | +import tech.pegasys.teku.ethereum.executionclient.ExecutionEngineClient; |
| 19 | +import tech.pegasys.teku.ethereum.executionclient.response.ResponseUnwrapper; |
| 20 | +import tech.pegasys.teku.ethereum.executionclient.schema.GetPayloadV4Response; |
| 21 | +import tech.pegasys.teku.infrastructure.async.SafeFuture; |
| 22 | +import tech.pegasys.teku.infrastructure.unsigned.UInt64; |
| 23 | +import tech.pegasys.teku.spec.Spec; |
| 24 | +import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSchema; |
| 25 | +import tech.pegasys.teku.spec.datastructures.execution.BlobsBundle; |
| 26 | +import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload; |
| 27 | +import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadContext; |
| 28 | +import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSchema; |
| 29 | +import tech.pegasys.teku.spec.datastructures.execution.GetPayloadResponse; |
| 30 | +import tech.pegasys.teku.spec.schemas.SchemaDefinitions; |
| 31 | +import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix; |
| 32 | +import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb; |
| 33 | + |
| 34 | +public class EngineGetPayloadV4 extends AbstractEngineJsonRpcMethod<GetPayloadResponse> { |
| 35 | + |
| 36 | + private static final Logger LOG = LogManager.getLogger(); |
| 37 | + |
| 38 | + private final Spec spec; |
| 39 | + |
| 40 | + public EngineGetPayloadV4(final ExecutionEngineClient executionEngineClient, final Spec spec) { |
| 41 | + super(executionEngineClient); |
| 42 | + this.spec = spec; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getName() { |
| 47 | + return EngineApiMethod.ENGINE_GET_PAYLOAD.getName(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int getVersion() { |
| 52 | + return 4; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public SafeFuture<GetPayloadResponse> execute(final JsonRpcRequestParams params) { |
| 57 | + final ExecutionPayloadContext executionPayloadContext = |
| 58 | + params.getRequiredParameter(0, ExecutionPayloadContext.class); |
| 59 | + final UInt64 slot = params.getRequiredParameter(1, UInt64.class); |
| 60 | + |
| 61 | + LOG.trace( |
| 62 | + "Calling {}(payloadId={}, slot={})", |
| 63 | + getVersionedName(), |
| 64 | + executionPayloadContext.getPayloadId(), |
| 65 | + slot); |
| 66 | + |
| 67 | + return executionEngineClient |
| 68 | + .getPayloadV4(executionPayloadContext.getPayloadId()) |
| 69 | + .thenApply(ResponseUnwrapper::unwrapExecutionClientResponseOrThrow) |
| 70 | + .thenApply( |
| 71 | + response -> { |
| 72 | + final SchemaDefinitions schemaDefinitions = spec.atSlot(slot).getSchemaDefinitions(); |
| 73 | + final ExecutionPayloadSchema<?> payloadSchema = |
| 74 | + SchemaDefinitionsBellatrix.required(schemaDefinitions) |
| 75 | + .getExecutionPayloadSchema(); |
| 76 | + final ExecutionPayload executionPayload = |
| 77 | + response.executionPayload.asInternalExecutionPayload(payloadSchema); |
| 78 | + final BlobsBundle blobsBundle = getBlobsBundle(response, schemaDefinitions); |
| 79 | + return new GetPayloadResponse( |
| 80 | + executionPayload, |
| 81 | + response.blockValue, |
| 82 | + blobsBundle, |
| 83 | + response.shouldOverrideBuilder); |
| 84 | + }) |
| 85 | + .thenPeek( |
| 86 | + getPayloadResponse -> |
| 87 | + LOG.trace( |
| 88 | + "Response {}(payloadId={}, slot={}) -> {}", |
| 89 | + getVersionedName(), |
| 90 | + executionPayloadContext.getPayloadId(), |
| 91 | + slot, |
| 92 | + getPayloadResponse)); |
| 93 | + } |
| 94 | + |
| 95 | + private BlobsBundle getBlobsBundle( |
| 96 | + final GetPayloadV4Response response, final SchemaDefinitions schemaDefinitions) { |
| 97 | + final BlobSchema blobSchema = |
| 98 | + SchemaDefinitionsDeneb.required(schemaDefinitions).getBlobSchema(); |
| 99 | + return response.blobsBundle.asInternalBlobsBundle(blobSchema); |
| 100 | + } |
| 101 | +} |
0 commit comments