-
Notifications
You must be signed in to change notification settings - Fork 390
Implemented PostPtcDuties api endpoint #10441
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a53ca02
Implemented PostPtcDuties api endpoint
rolfyone 2d61864
Merge branch 'master' into 10405-0
rolfyone 953ab28
Merge branch 'master' into 10405-0
rolfyone b9f9f4d
Merge remote-tracking branch 'upstream/master' into 10405-0
rolfyone 2484ef5
updated description
rolfyone d2a4518
Merge branch 'master' into 10405-0
rolfyone e9365bc
fixed integration test after descriptions changed
rolfyone 824176f
Merge remote-tracking branch 'upstream/master' into 10405-0
rolfyone 422be9c
review feedback
rolfyone 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
88 changes: 88 additions & 0 deletions
88
...-test/java/tech/pegasys/teku/beaconrestapi/v1/validator/PostPtcDutiesIntegrationTest.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,88 @@ | ||
| /* | ||
| * Copyright Consensys Software Inc., 2026 | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package tech.pegasys.teku.beaconrestapi.v1.validator; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.when; | ||
| import static tech.pegasys.teku.ethereum.json.types.validator.PtcDuties.PTC_DUTIES_TYPE_DEFINITION; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_SERVICE_UNAVAILABLE; | ||
| import static tech.pegasys.teku.infrastructure.json.JsonUtil.parse; | ||
| import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import okhttp3.Response; | ||
| import org.apache.tuweni.bytes.Bytes32; | ||
| import org.junit.jupiter.api.Test; | ||
| import tech.pegasys.teku.beacon.sync.events.SyncState; | ||
| import tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest; | ||
| import tech.pegasys.teku.beaconrestapi.handlers.v1.validator.PostPtcDuties; | ||
| import tech.pegasys.teku.ethereum.json.types.validator.PtcDuties; | ||
| import tech.pegasys.teku.ethereum.json.types.validator.PtcDuty; | ||
| import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
| import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
| import tech.pegasys.teku.spec.SpecMilestone; | ||
| import tech.pegasys.teku.spec.util.DataStructureUtil; | ||
|
|
||
| public class PostPtcDutiesIntegrationTest extends AbstractDataBackedRestAPIIntegrationTest { | ||
| @Test | ||
| void shouldErrorIfPriorToGloas() throws IOException { | ||
| startRestAPIAtGenesis(SpecMilestone.FULU); | ||
|
|
||
| when(syncService.getCurrentSyncState()).thenReturn(SyncState.IN_SYNC); | ||
|
|
||
| final Response response = post(PostPtcDuties.ROUTE.replace("{epoch}", "1"), ""); | ||
|
|
||
| assertThat(response.code()).isEqualTo(SC_BAD_REQUEST); | ||
| assertThat(response.body().string()).contains("prior to gloas"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldErrorIfSyncing() throws IOException { | ||
| startRestAPIAtGenesis(SpecMilestone.GLOAS); | ||
| when(syncService.getCurrentSyncState()).thenReturn(SyncState.SYNCING); | ||
| final Response response = post(PostPtcDuties.ROUTE.replace("{epoch}", "1"), ""); | ||
|
|
||
| assertThat(response.code()).isEqualTo(SC_SERVICE_UNAVAILABLE); | ||
| assertThat(response.body().string()).contains("syncing"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldUsePreviousDependentRootForCurrentEpochDuties() throws IOException { | ||
| startRestAPIAtGenesis(SpecMilestone.GLOAS); | ||
| final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec); | ||
| when(syncService.getCurrentSyncState()).thenReturn(SyncState.IN_SYNC); | ||
| final Bytes32 dependentRoot = dataStructureUtil.randomBytes32(); | ||
| final PtcDuty duty = new PtcDuty(VALIDATOR_KEYS.get(1).getPublicKey(), ONE, UInt64.valueOf(13)); | ||
| final SafeFuture<Optional<PtcDuties>> out = | ||
| SafeFuture.completedFuture(Optional.of(new PtcDuties(false, dependentRoot, List.of(duty)))); | ||
| when(validatorApiChannel.getPtcDuties(eq(ONE), any())).thenReturn(out); | ||
|
|
||
| final Response response = post(PostPtcDuties.ROUTE.replace("{epoch}", "1"), "[1]"); | ||
| final String responseBody = response.body().string(); | ||
| assertThat(responseBody).isNotEmpty(); | ||
| assertThat(response.code()).isEqualTo(SC_OK); | ||
|
|
||
| final PtcDuties duties = parse(responseBody, PTC_DUTIES_TYPE_DEFINITION); | ||
|
|
||
| assertThat(duties.dependentRoot()).isEqualTo(dependentRoot); | ||
| assertThat(duties.executionOptimistic()).isFalse(); | ||
| assertThat(duties.duties().getFirst()).isEqualTo(duty); | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
...es/tech/pegasys/teku/beaconrestapi/beacon/paths/_eth_v1_validator_duties_ptc_{epoch}.json
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,81 @@ | ||
| { | ||
| "post" : { | ||
| "tags" : [ "Validator", "Validator Required Api" ], | ||
| "operationId" : "getPtcDuties", | ||
| "summary" : "Get PTC duties", | ||
| "description" : "Requests the beacon node to provide a set of Payload Timeliness Committee (PTC) duties, which should be performed by validators, for a particular epoch. Duties should only need to be checked once per epoch, however a chain reorganization could occur, resulting in a change of duties. For full safety, you should monitor head events and confirm the dependent root in this response matches:\n\n - event.previous_duty_dependent_root when compute_epoch_at_slot(event.slot) == epoch\n - event.block otherwise\n\nThe dependent_root value is get_block_root_at_slot(state, compute_start_slot_at_epoch(epoch - 1) - 1) or the genesis block root in the case of underflow.", | ||
| "parameters" : [ { | ||
| "name" : "epoch", | ||
| "required" : true, | ||
| "in" : "path", | ||
| "schema" : { | ||
| "type" : "string", | ||
| "description" : "`uint64` Epoch number to query.", | ||
| "example" : "1", | ||
| "format" : "uint64" | ||
| } | ||
| } ], | ||
| "requestBody" : { | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "type" : "array", | ||
| "minItems" : 1, | ||
| "items" : { | ||
| "type" : "string", | ||
| "description" : "integer string", | ||
| "example" : "1", | ||
| "format" : "integer" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "responses" : { | ||
| "200" : { | ||
| "description" : "Success response", | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "$ref" : "#/components/schemas/GetPtcDutiesResponse" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "204" : { | ||
| "description" : "Data is unavailable because the chain has not yet reached genesis", | ||
| "content" : { } | ||
| }, | ||
| "503" : { | ||
| "description" : "Beacon node is currently syncing and not serving requests.", | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "$ref" : "#/components/schemas/HttpErrorResponse" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "400" : { | ||
| "description" : "The request could not be processed, check the response for more information.", | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "$ref" : "#/components/schemas/HttpErrorResponse" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "500" : { | ||
| "description" : "Internal server error", | ||
| "content" : { | ||
| "application/json" : { | ||
| "schema" : { | ||
| "$ref" : "#/components/schemas/HttpErrorResponse" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...on-test/resources/tech/pegasys/teku/beaconrestapi/beacon/schema/GetPtcDutiesResponse.json
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,22 @@ | ||
| { | ||
| "title" : "GetPtcDutiesResponse", | ||
| "type" : "object", | ||
| "required" : [ "dependent_root", "execution_optimistic", "data" ], | ||
| "properties" : { | ||
| "dependent_root" : { | ||
| "type" : "string", | ||
| "description" : "Bytes32 hexadecimal", | ||
| "example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2", | ||
| "format" : "byte" | ||
| }, | ||
| "execution_optimistic" : { | ||
| "type" : "boolean" | ||
| }, | ||
| "data" : { | ||
| "type" : "array", | ||
| "items" : { | ||
| "$ref" : "#/components/schemas/PtcDuty" | ||
| } | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...src/integration-test/resources/tech/pegasys/teku/beaconrestapi/beacon/schema/PtcDuty.json
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,25 @@ | ||
| { | ||
| "title" : "PtcDuty", | ||
| "type" : "object", | ||
| "required" : [ "pubkey", "validator_index", "slot" ], | ||
| "properties" : { | ||
| "pubkey" : { | ||
| "type" : "string", | ||
| "description" : "`BLSPublicKey Hex` The validator's BLS public key, uniquely identifying them. 48-bytes, hex encoded with 0x prefix, case insensitive.", | ||
| "example" : "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a", | ||
| "format" : "string" | ||
| }, | ||
| "validator_index" : { | ||
| "type" : "string", | ||
| "description" : "unsigned 64 bit integer", | ||
| "example" : "1", | ||
| "format" : "uint64" | ||
| }, | ||
| "slot" : { | ||
| "type" : "string", | ||
| "description" : "unsigned 64 bit integer", | ||
| "example" : "1", | ||
| "format" : "uint64" | ||
| } | ||
| } | ||
| } |
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
114 changes: 114 additions & 0 deletions
114
...pi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/validator/PostPtcDuties.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,114 @@ | ||
| /* | ||
| * Copyright Consensys Software Inc., 2026 | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package tech.pegasys.teku.beaconrestapi.handlers.v1.validator; | ||
|
|
||
| import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.EPOCH_PARAMETER; | ||
| import static tech.pegasys.teku.ethereum.json.types.SharedApiTypes.BODY_INTEGER_LIST; | ||
| import static tech.pegasys.teku.ethereum.json.types.validator.PtcDuties.PTC_DUTIES_TYPE_DEFINITION; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_NO_CONTENT; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; | ||
| import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_SERVICE_UNAVAILABLE; | ||
| import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR; | ||
| import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR_REQUIRED; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import it.unimi.dsi.fastutil.ints.IntArrayList; | ||
| import it.unimi.dsi.fastutil.ints.IntList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import tech.pegasys.teku.api.DataProvider; | ||
| import tech.pegasys.teku.api.SyncDataProvider; | ||
| import tech.pegasys.teku.api.ValidatorDataProvider; | ||
| import tech.pegasys.teku.ethereum.json.types.validator.PtcDuties; | ||
| import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
| import tech.pegasys.teku.infrastructure.restapi.endpoints.AsyncApiResponse; | ||
| import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata; | ||
| import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint; | ||
| import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest; | ||
| import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
| import tech.pegasys.teku.spec.Spec; | ||
| import tech.pegasys.teku.spec.SpecMilestone; | ||
|
|
||
| public class PostPtcDuties extends RestApiEndpoint { | ||
| public static final String ROUTE = "/eth/v1/validator/duties/ptc/{epoch}"; | ||
| private final ValidatorDataProvider validatorDataProvider; | ||
| private final SyncDataProvider syncDataProvider; | ||
| private final Spec spec; | ||
|
|
||
| public PostPtcDuties(final DataProvider dataProvider, final Spec spec) { | ||
| this(dataProvider.getSyncDataProvider(), spec, dataProvider.getValidatorDataProvider()); | ||
| } | ||
|
|
||
| PostPtcDuties( | ||
| final SyncDataProvider syncDataProvider, | ||
| final Spec spec, | ||
| final ValidatorDataProvider validatorDataProvider) { | ||
| super( | ||
| EndpointMetadata.post(ROUTE) | ||
| .operationId("getPtcDuties") | ||
| .summary("Get PTC duties") | ||
| .description( | ||
| "Requests the beacon node to provide a set of Payload Timeliness Committee (PTC) duties, which " | ||
| + "should be performed by validators, for a particular epoch. Duties should only need to be " | ||
| + "checked once per epoch, however a chain reorganization could occur, " | ||
| + "resulting in a change of duties. For full safety, you should monitor head events and confirm the " | ||
| + "dependent root in this response matches:\n\n" | ||
| + " - event.previous_duty_dependent_root when compute_epoch_at_slot(event.slot) == epoch\n" | ||
| + " - event.block otherwise\n\n" | ||
|
rolfyone marked this conversation as resolved.
|
||
| + "The dependent_root value is get_block_root_at_slot(state, compute_start_slot_at_epoch(epoch - 1) - 1) " | ||
| + "or the genesis block root in the case of underflow.") | ||
|
rolfyone marked this conversation as resolved.
|
||
| .tags(TAG_VALIDATOR, TAG_VALIDATOR_REQUIRED) | ||
| .requestBodyType(BODY_INTEGER_LIST) | ||
| .pathParam(EPOCH_PARAMETER) | ||
| .response(SC_OK, "Success response", PTC_DUTIES_TYPE_DEFINITION) | ||
| .response( | ||
| SC_NO_CONTENT, "Data is unavailable because the chain has not yet reached genesis") | ||
| .withServiceUnavailableResponse() | ||
| .build()); | ||
| this.syncDataProvider = syncDataProvider; | ||
| this.spec = spec; | ||
| this.validatorDataProvider = validatorDataProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleRequest(final RestApiRequest request) throws JsonProcessingException { | ||
| if (!validatorDataProvider.isStoreAvailable() || syncDataProvider.isSyncing()) { | ||
| request.respondError( | ||
| SC_SERVICE_UNAVAILABLE, "Beacon node is currently syncing and not serving requests."); | ||
| return; | ||
| } | ||
|
|
||
| final UInt64 epoch = request.getPathParameter(EPOCH_PARAMETER); | ||
|
|
||
| if (spec.atEpoch(epoch).getMilestone().isLessThan(SpecMilestone.GLOAS)) { | ||
| request.respondError( | ||
| SC_BAD_REQUEST, "Cannot request PTC duties for epochs prior to gloas fork."); | ||
| return; | ||
| } | ||
|
|
||
| final List<Integer> requestBody = request.getRequestBody(); | ||
| final IntList indices = IntArrayList.toList(requestBody.stream().mapToInt(Integer::intValue)); | ||
|
|
||
| final SafeFuture<Optional<PtcDuties>> future = | ||
| validatorDataProvider.getPtcDuties(epoch, indices); | ||
|
|
||
| request.respondAsync( | ||
| future.thenApply( | ||
| attesterDuties -> | ||
| attesterDuties | ||
| .map(AsyncApiResponse::respondOk) | ||
| .orElse(AsyncApiResponse.respondServiceUnavailable()))); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.