-
Notifications
You must be signed in to change notification settings - Fork 1k
add engine_preparePayload_debug endpoint #4427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
garyschulte
merged 5 commits into
hyperledger:main
from
garyschulte:feature/debug_preparePayload
Apr 19, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a32bcff
rebase on origin/main
garyschulte b5d8351
address feedback, add withdrawals
garyschulte c845900
filter debug payload from engine capabilities, add a syncing response…
garyschulte 12314e4
make any/all parameters optional for debug payload
garyschulte 01f73e9
spdx header
garyschulte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...erledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EnginePreparePayloadDebug.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright contributors to Hyperledger Besu | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
| import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.SYNCING; | ||
| import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.VALID; | ||
|
|
||
| import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator; | ||
| import org.hyperledger.besu.consensus.merge.blockcreation.PayloadIdentifier; | ||
| import org.hyperledger.besu.ethereum.ProtocolContext; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EnginePreparePayloadParameter; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.WithdrawalParameter; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.EnginePreparePayloadResult; | ||
| import org.hyperledger.besu.ethereum.core.Withdrawal; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import graphql.VisibleForTesting; | ||
| import io.vertx.core.Vertx; | ||
|
|
||
| public class EnginePreparePayloadDebug extends ExecutionEngineJsonRpcMethod { | ||
| private final MergeMiningCoordinator mergeCoordinator; | ||
|
|
||
| public EnginePreparePayloadDebug( | ||
| final Vertx vertx, | ||
| final ProtocolContext protocolContext, | ||
| final EngineCallListener engineCallListener, | ||
| final MergeMiningCoordinator mergeCoordinator) { | ||
| super(vertx, protocolContext, engineCallListener); | ||
| this.mergeCoordinator = mergeCoordinator; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return RpcMethod.ENGINE_PREPARE_PAYLOAD_DEBUG.getMethodName(); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext) { | ||
| final EnginePreparePayloadParameter enginePreparePayloadParameter = | ||
| requestContext | ||
| .getOptionalParameter(0, EnginePreparePayloadParameter.class) | ||
| .orElse( | ||
| new EnginePreparePayloadParameter( | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| Optional.empty())); | ||
|
|
||
| final var requestId = requestContext.getRequest().getId(); | ||
|
|
||
| if (mergeContext.get().isSyncing()) { | ||
| return new JsonRpcSuccessResponse(requestId, new EnginePreparePayloadResult(SYNCING, null)); | ||
| } | ||
|
|
||
| return generatePayload(enginePreparePayloadParameter) | ||
| .<JsonRpcResponse>map( | ||
| payloadIdentifier -> | ||
| new JsonRpcSuccessResponse( | ||
| requestId, new EnginePreparePayloadResult(VALID, payloadIdentifier))) | ||
| .orElseGet(() -> new JsonRpcErrorResponse(requestId, JsonRpcError.INVALID_PARAMS)); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| Optional<PayloadIdentifier> generatePayload(final EnginePreparePayloadParameter param) { | ||
| final List<Withdrawal> withdrawals = | ||
| param.getWithdrawals().stream().map(WithdrawalParameter::toWithdrawal).collect(toList()); | ||
|
|
||
| return param | ||
| .getParentHash() | ||
| .map(header -> protocolContext.getBlockchain().getBlockHeader(header)) | ||
| .orElseGet(() -> Optional.of(protocolContext.getBlockchain().getChainHeadHeader())) | ||
| .map( | ||
| parentHeader -> | ||
| mergeCoordinator.preparePayload( | ||
| parentHeader, | ||
| param.getTimestamp().orElse(parentHeader.getTimestamp() + 1L), | ||
| param.getPrevRandao(), | ||
| param.getFeeRecipient(), | ||
| Optional.of(withdrawals))); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
...erledger/besu/ethereum/api/jsonrpc/internal/parameters/EnginePreparePayloadParameter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright contributors to Hyperledger Besu | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; | ||
|
|
||
| import org.hyperledger.besu.datatypes.Address; | ||
| import org.hyperledger.besu.datatypes.Hash; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import org.apache.tuweni.bytes.Bytes32; | ||
|
|
||
| public class EnginePreparePayloadParameter { | ||
| private final Optional<Hash> parentHash; | ||
| private final Address feeRecipient; | ||
| private final Bytes32 prevRandao; | ||
| private final Optional<Long> timestamp; | ||
| final List<WithdrawalParameter> withdrawals; | ||
|
|
||
| @JsonCreator | ||
| public EnginePreparePayloadParameter( | ||
| @JsonProperty("parentHash") final Optional<Hash> parentHash, | ||
| @JsonProperty("feeRecipient") final Optional<Address> feeRecipient, | ||
| @JsonProperty("timestamp") final Optional<UnsignedLongParameter> timestamp, | ||
| @JsonProperty("prevRandao") final Optional<String> prevRandao, | ||
| @JsonProperty("withdrawals") final Optional<List<WithdrawalParameter>> withdrawals) { | ||
| this.parentHash = parentHash; | ||
| this.feeRecipient = feeRecipient.orElse(Address.ZERO); | ||
| this.timestamp = timestamp.map(UnsignedLongParameter::getValue); | ||
| this.prevRandao = Bytes32.fromHexStringLenient(prevRandao.orElse("deadbeef")); | ||
| this.withdrawals = withdrawals.orElse(Collections.emptyList()); | ||
| } | ||
|
|
||
| public Optional<Hash> getParentHash() { | ||
| return parentHash; | ||
| } | ||
|
|
||
| public Address getFeeRecipient() { | ||
| return feeRecipient; | ||
| } | ||
|
|
||
| public Optional<Long> getTimestamp() { | ||
| return timestamp; | ||
| } | ||
|
|
||
| public Bytes32 getPrevRandao() { | ||
| return prevRandao; | ||
| } | ||
|
|
||
| public List<WithdrawalParameter> getWithdrawals() { | ||
| return withdrawals; | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
...rg/hyperledger/besu/ethereum/api/jsonrpc/internal/results/EnginePreparePayloadResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright contributors to Hyperledger Besu | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results; | ||
|
|
||
| import org.hyperledger.besu.consensus.merge.blockcreation.PayloadIdentifier; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonGetter; | ||
| import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
|
|
||
| @JsonPropertyOrder({"status", "payloadId"}) | ||
| public class EnginePreparePayloadResult { | ||
| private final EngineStatus status; | ||
| private final PayloadIdentifier payloadId; | ||
|
|
||
| public EnginePreparePayloadResult(final EngineStatus status, final PayloadIdentifier payloadId) { | ||
| this.status = status; | ||
| this.payloadId = payloadId; | ||
| } | ||
|
|
||
| @JsonGetter(value = "status") | ||
| public String getStatus() { | ||
| return status.name(); | ||
| } | ||
|
|
||
| @JsonGetter(value = "payloadId") | ||
| public String getPayloadId() { | ||
| return Optional.ofNullable(payloadId).map(PayloadIdentifier::toShortHexString).orElse(""); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...dger/besu/ethereum/api/jsonrpc/internal/methods/engine/EnginePreparePayloadDebugTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright Hyperledger Besu Contributors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
| import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.SYNCING; | ||
| import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.VALID; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.hyperledger.besu.consensus.merge.MergeContext; | ||
| import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator; | ||
| import org.hyperledger.besu.consensus.merge.blockcreation.PayloadIdentifier; | ||
| import org.hyperledger.besu.ethereum.ProtocolContext; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EnginePreparePayloadParameter; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.EnginePreparePayloadResult; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import io.vertx.core.Vertx; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.Answers; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class EnginePreparePayloadDebugTest { | ||
| private static final Vertx vertx = Vertx.vertx(); | ||
| EnginePreparePayloadDebug method; | ||
| @Mock private ProtocolContext protocolContext; | ||
| @Mock private EngineCallListener engineCallListener; | ||
| @Mock private MergeMiningCoordinator mergeCoordinator; | ||
| @Mock private MergeContext mergeContext; | ||
|
|
||
| @Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
| private JsonRpcRequestContext requestContext; | ||
|
|
||
| @Mock private EnginePreparePayloadParameter param; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| when(protocolContext.safeConsensusContext(MergeContext.class)) | ||
| .thenReturn(Optional.of(mergeContext)); | ||
| when(requestContext.getOptionalParameter(0, EnginePreparePayloadParameter.class)) | ||
| .thenReturn(Optional.of(param)); | ||
| method = | ||
| spy( | ||
| new EnginePreparePayloadDebug( | ||
| vertx, protocolContext, engineCallListener, mergeCoordinator)); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnSyncing() { | ||
| when(mergeContext.isSyncing()).thenReturn(true); | ||
| var resp = method.syncResponse(requestContext); | ||
| assertThat(resp).isInstanceOf(JsonRpcSuccessResponse.class); | ||
| var result = ((JsonRpcSuccessResponse) resp).getResult(); | ||
| assertThat(result).isInstanceOf(EnginePreparePayloadResult.class); | ||
| var payloadResult = (EnginePreparePayloadResult) result; | ||
| assertThat(payloadResult.getStatus()).isEqualTo(SYNCING.name()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnPayloadId() { | ||
| checkForPayloadId(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnPayloadIdWhenNoParams() { | ||
| when(requestContext.getOptionalParameter(0, EnginePreparePayloadParameter.class)) | ||
| .thenReturn(Optional.empty()); | ||
| checkForPayloadId(); | ||
| } | ||
|
|
||
| private void checkForPayloadId() { | ||
| doAnswer(__ -> Optional.of(new PayloadIdentifier(0xdeadbeefL))) | ||
| .when(method) | ||
| .generatePayload(any()); | ||
| var resp = method.syncResponse(requestContext); | ||
| assertThat(resp).isInstanceOf(JsonRpcSuccessResponse.class); | ||
| var result = ((JsonRpcSuccessResponse) resp).getResult(); | ||
| assertThat(result).isInstanceOf(EnginePreparePayloadResult.class); | ||
| var payloadResult = (EnginePreparePayloadResult) result; | ||
| assertThat(payloadResult.getStatus()).isEqualTo(VALID.name()); | ||
| assertThat(payloadResult.getPayloadId()).isEqualTo("0xdeadbeef"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is a tricky question, shouldn't this method be in the debug set? Another reason to move that is, the way we built the engine_exchange_capabilities mean Besu might report this method. CL is probably going to ignore it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DEBUG is present even if the ENGINE api is not, so I think this needs to live within engine api. Good point about the capabilities though 🤔 I will check to see if this causes problems for CLs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about a ENGINE_DEBUG set? So it is separated from ENGINE and can be enabled only if used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I filtered the debug prepare payload from the capabilities list in the same way the capabilities list filters itself 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just saw the ENGINE_DEBUG set suggestion. I think we could add something like that if we have more than one engine debug endpoint in the future 👍 . Rule of three applies in this case IMO