-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GetAggregateAttestationV2 API (#8464)
- Loading branch information
1 parent
bdff765
commit 8c36891
Showing
11 changed files
with
446 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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
81 changes: 81 additions & 0 deletions
81
...tech/pegasys/teku/beaconrestapi/beacon/paths/_eth_v2_validator_aggregate_attestation.json
This file contains 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 @@ | ||
{ | ||
"get" : { | ||
"tags" : [ "Validator", "Validator Required Api" ], | ||
"operationId" : "getAggregatedAttestationV2", | ||
"summary" : "Get aggregated attestation", | ||
"description" : "Aggregates all attestations matching given attestation data root, slot and committee index.\nA 503 error must be returned if the block identified by the response\n`beacon_block_root` is optimistic (i.e. the aggregated attestation attests\nto a block that has not been fully verified by an execution engine).\nA 404 error must be returned if no attestation is available for the requested\n`attestation_data_root`.", | ||
"parameters" : [ { | ||
"name" : "attestation_data_root", | ||
"required" : true, | ||
"in" : "query", | ||
"schema" : { | ||
"type" : "string", | ||
"description" : "`String` HashTreeRoot of AttestationData that validator wants aggregated.", | ||
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2", | ||
"format" : "byte" | ||
} | ||
}, { | ||
"name" : "slot", | ||
"required" : true, | ||
"in" : "query", | ||
"schema" : { | ||
"type" : "string", | ||
"description" : "`uint64` value representing slot", | ||
"example" : "1", | ||
"format" : "uint64" | ||
} | ||
}, { | ||
"name" : "committee_index", | ||
"required" : true, | ||
"in" : "query", | ||
"schema" : { | ||
"type" : "string", | ||
"description" : "`uint64` Committee index to query.", | ||
"example" : "1", | ||
"format" : "uint64" | ||
} | ||
} ], | ||
"responses" : { | ||
"200" : { | ||
"description" : "Request successful", | ||
"content" : { | ||
"application/json" : { | ||
"schema" : { | ||
"$ref" : "#/components/schemas/GetAggregatedAttestationResponseV2" | ||
} | ||
} | ||
} | ||
}, | ||
"404" : { | ||
"description" : "Not found", | ||
"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" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ces/tech/pegasys/teku/beaconrestapi/beacon/schema/GetAggregatedAttestationResponseV2.json
This file contains 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,19 @@ | ||
{ | ||
"title" : "GetAggregatedAttestationResponseV2", | ||
"type" : "object", | ||
"required" : [ "version", "data" ], | ||
"properties" : { | ||
"version" : { | ||
"type" : "string", | ||
"enum" : [ "phase0", "altair", "bellatrix", "capella", "deneb", "electra" ] | ||
}, | ||
"data" : { | ||
"type" : "object", | ||
"oneOf" : [ { | ||
"$ref" : "#/components/schemas/AttestationElectra" | ||
}, { | ||
"$ref" : "#/components/schemas/AttestationPhase0" | ||
} ] | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
133 changes: 133 additions & 0 deletions
133
...java/tech/pegasys/teku/beaconrestapi/handlers/v2/validator/GetAggregateAttestationV2.java
This file contains 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,133 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* 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.v2.validator; | ||
|
||
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.ATTESTATION_DATA_ROOT_PARAMETER; | ||
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.COMMITTEE_INDEX_PARAMETER; | ||
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.SLOT_PARAMETER; | ||
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.MILESTONE_TYPE; | ||
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_CONSENSUS_VERSION; | ||
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 java.util.Optional; | ||
import java.util.function.Predicate; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import tech.pegasys.teku.api.DataProvider; | ||
import tech.pegasys.teku.api.ValidatorDataProvider; | ||
import tech.pegasys.teku.api.schema.Version; | ||
import tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes; | ||
import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
import tech.pegasys.teku.infrastructure.http.HttpStatusCodes; | ||
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinition; | ||
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinitionBuilder; | ||
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition; | ||
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.datastructures.metadata.ObjectAndMetaData; | ||
import tech.pegasys.teku.spec.datastructures.operations.Attestation; | ||
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache; | ||
|
||
public class GetAggregateAttestationV2 extends RestApiEndpoint { | ||
|
||
public static final String ROUTE = "/eth/v2/validator/aggregate_attestation"; | ||
private final ValidatorDataProvider provider; | ||
|
||
public GetAggregateAttestationV2( | ||
final DataProvider dataProvider, final SchemaDefinitionCache schemaDefinitionCache) { | ||
this(dataProvider.getValidatorDataProvider(), schemaDefinitionCache); | ||
} | ||
|
||
public GetAggregateAttestationV2( | ||
final ValidatorDataProvider provider, final SchemaDefinitionCache schemaDefinitionCache) { | ||
super( | ||
EndpointMetadata.get(ROUTE) | ||
.operationId("getAggregatedAttestationV2") | ||
.summary("Get aggregated attestation") | ||
.description( | ||
"Aggregates all attestations matching given attestation data root, slot and committee index.\n" | ||
+ "A 503 error must be returned if the block identified by the response\n" | ||
+ "`beacon_block_root` is optimistic (i.e. the aggregated attestation attests\n" | ||
+ "to a block that has not been fully verified by an execution engine).\n" | ||
+ "A 404 error must be returned if no attestation is available for the requested\n" | ||
+ "`attestation_data_root`.") | ||
.tags(TAG_VALIDATOR, TAG_VALIDATOR_REQUIRED) | ||
.queryParamRequired(ATTESTATION_DATA_ROOT_PARAMETER) | ||
.queryParamRequired(SLOT_PARAMETER) | ||
.queryParamRequired(COMMITTEE_INDEX_PARAMETER) | ||
.response( | ||
HttpStatusCodes.SC_OK, "Request successful", getResponseType(schemaDefinitionCache)) | ||
.withNotFoundResponse() | ||
.build()); | ||
this.provider = provider; | ||
} | ||
|
||
@Override | ||
public void handleRequest(final RestApiRequest request) throws JsonProcessingException { | ||
final Bytes32 beaconBlockRoot = request.getQueryParameter(ATTESTATION_DATA_ROOT_PARAMETER); | ||
final UInt64 slot = request.getQueryParameter(SLOT_PARAMETER); | ||
final UInt64 committeeIndex = request.getQueryParameter(COMMITTEE_INDEX_PARAMETER); | ||
|
||
final SafeFuture<Optional<ObjectAndMetaData<Attestation>>> future = | ||
provider.createAggregateAndMetaData(slot, beaconBlockRoot, committeeIndex); | ||
|
||
request.respondAsync( | ||
future.thenApply( | ||
maybeAttestation -> | ||
maybeAttestation | ||
.map( | ||
attestationAndMetaData -> { | ||
request.header( | ||
HEADER_CONSENSUS_VERSION, | ||
Version.fromMilestone(attestationAndMetaData.getMilestone()).name()); | ||
return AsyncApiResponse.respondOk(attestationAndMetaData); | ||
}) | ||
.orElseGet(AsyncApiResponse::respondNotFound))); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static SerializableTypeDefinition<ObjectAndMetaData<Attestation>> getResponseType( | ||
final SchemaDefinitionCache schemaDefinitionCache) { | ||
|
||
final SerializableOneOfTypeDefinition<Attestation> oneOfTypeDefinition = | ||
new SerializableOneOfTypeDefinitionBuilder<Attestation>() | ||
.withType( | ||
electraAttestationPredicate(), | ||
BeaconRestApiTypes.electraAttestationTypeDef(schemaDefinitionCache)) | ||
.withType( | ||
phase0AttestationPredicate(), | ||
BeaconRestApiTypes.phase0AttestationTypeDef(schemaDefinitionCache)) | ||
.build(); | ||
|
||
return SerializableTypeDefinition.<ObjectAndMetaData<Attestation>>object() | ||
.name("GetAggregatedAttestationResponseV2") | ||
.withField("version", MILESTONE_TYPE, ObjectAndMetaData::getMilestone) | ||
.withField("data", oneOfTypeDefinition, ObjectAndMetaData::getData) | ||
.build(); | ||
} | ||
|
||
private static Predicate<Attestation> phase0AttestationPredicate() { | ||
// Before Electra attestations do not require committee bits | ||
return attestation -> !attestation.requiresCommitteeBits(); | ||
} | ||
|
||
private static Predicate<Attestation> electraAttestationPredicate() { | ||
// Only once we are in Electra attestations will have committee bits | ||
return Attestation::requiresCommitteeBits; | ||
} | ||
} |
Oops, something went wrong.