Skip to content

Commit

Permalink
update post attestation interface for electra
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone committed Dec 6, 2024
1 parent fbf7359 commit e7a9b11
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,20 @@ void shouldPartiallyPostAttestations_ReturnsErrors() throws Exception {
final SubmitDataError secondSubmitDataError =
new SubmitDataError(UInt64.ONE, "Very bad attestation");

final List<Attestation> attestations =
List.of(
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation());
final List<Attestation> attestations;
if (specMilestone.isGreaterThanOrEqualTo(SpecMilestone.ELECTRA)) {
attestations =
List.of(
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation());
} else {
attestations =
List.of(
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation());
}

when(validatorApiChannel.sendSignedAttestations(attestations))
.thenReturn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"oneOf" : [ {
"$ref" : "#/components/schemas/AttestationPhase0"
}, {
"$ref" : "#/components/schemas/AttestationElectra"
"$ref" : "#/components/schemas/SingleAttestation"
} ]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@

import static tech.pegasys.teku.api.ValidatorDataProvider.PARTIAL_PUBLISH_FAILURE_MESSAGE;
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.ETH_CONSENSUS_VERSION_TYPE;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllSupportedMilestones;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.headerBasedSelector;
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.RestApiConstants.HEADER_CONSENSUS_VERSION;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_EXPERIMENTAL;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR_REQUIRED;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
import tech.pegasys.teku.api.exceptions.BadRequestException;
import tech.pegasys.teku.beaconrestapi.schema.ErrorListBadRequest;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition;
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;
Expand All @@ -40,7 +43,7 @@
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;
import tech.pegasys.teku.validator.api.SubmitDataError;

public class PostAttestationsV2 extends RestApiEndpoint {
Expand Down Expand Up @@ -87,20 +90,27 @@ private static EndpointMetadata createMetadata(
.milestoneAtSlot(attestation.getData().getSlot())
.equals(milestone);

final SerializableOneOfTypeDefinitionBuilder<Attestation> builder =
new SerializableOneOfTypeDefinitionBuilder<Attestation>().title("SignedAttestation");

builder.withType(
value -> attestationSchemaPredicate.test(value, SpecMilestone.PHASE0),
schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.PHASE0)
.getAttestationSchema()
.getJsonTypeDefinition());
builder.withType(
value -> attestationSchemaPredicate.test(value, SpecMilestone.ELECTRA),
SchemaDefinitionsElectra.required(
schemaDefinitionCache.getSchemaDefinition(SpecMilestone.ELECTRA))
.getSingleAttestationSchema()
.getJsonTypeDefinition());
final SerializableOneOfTypeDefinition<Attestation> attestationSchemaDefinition =
getSchemaDefinitionForAllSupportedMilestones(
schemaDefinitionCache,
"SignedAttestation",
SchemaDefinitions::getAttestationSchema,
attestationSchemaPredicate);
builder.build();

final OneOfArrayJsonRequestContentTypeDefinition.BodyTypeSelector<Attestation>
attestationBodySelector =
context ->
headerBasedSelector(
context.getHeaders(),
schemaDefinitionCache,
SchemaDefinitions::getAttestationSchema);
context -> headerBasedSelector(context.getHeaders(), schemaDefinitionCache);

return EndpointMetadata.post(ROUTE)
.operationId("submitPoolAttestationsV2")
Expand All @@ -123,4 +133,31 @@ private static EndpointMetadata createMetadata(
.withChainDataResponses()
.build();
}

public static DeserializableTypeDefinition<? extends Attestation> headerBasedSelector(
final Map<String, String> headers, final SchemaDefinitionCache schemaDefinitionCache) {
if (!headers.containsKey(HEADER_CONSENSUS_VERSION)) {
throw new BadRequestException(
String.format("Missing required header value for (%s)", HEADER_CONSENSUS_VERSION));
}
try {
final SpecMilestone milestone = SpecMilestone.forName(headers.get(HEADER_CONSENSUS_VERSION));
if (milestone.isLessThanOrEqualTo(SpecMilestone.DENEB)) {
return schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.PHASE0)
.getAttestationSchema()
.getJsonTypeDefinition();
} else {
return SchemaDefinitionsElectra.required(
schemaDefinitionCache.getSchemaDefinition(SpecMilestone.ELECTRA))
.getSingleAttestationSchema()
.getJsonTypeDefinition();
}
} catch (Exception e) {
throw new BadRequestException(
String.format(
"Invalid value for (%s) header: %s",
HEADER_CONSENSUS_VERSION, headers.get(HEADER_CONSENSUS_VERSION)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[
{
"aggregation_bits": "0x4a9278690f62e1a353f1abf2b9701e13e8cdf4d9ac6b032ba43b05b25f713540ce24f3192819e752fb091217ff34b68e934a06d316b6060696f8f24749574c4b3ac2e4ccb6914c434b09a81fff523e12acbe299fcdad715593298d72ca70e1ffc743ad7ce89587fbb4b4c57db7856b7082db70ebddcbebe264f886236df2dd51539e10d4dcd5950e6cea7c993d3e999a5589a4c71669ffb1390987e43a8c4790a70275364f96cbee34b0b5a9d1b3da4322ad12e07c81c6e6430d2528f19bb1727c3f63f414885bd97505283b0bb6773712096d5feb67c43d67f2fbf17bf796ed5080aece6b968d532d985ad2553daf31ad4022aa49d7a92ada719c9f93ab4b6f0d09f127c8d47b9ab80e95a2e72257013e933113029994778c23dfa313b689e08c58979148ac541159b8eb601eee74e985a3b5b9c2dfe0ce3145794d84647136865fabf5814e8a013e236e9b740a6c18229838f3022e1aa2afe5fe48caff6e1470e4458ebcbf152462dc300b07a3d0b102a29196b0a8d444871868408fe80e1dcecd216fe8022ea70326081e516c48bd1b8a18003322738642b189013c3ed8ad64185cf1a44cb1f6265cc40450b5caea7c29b135b5145b4f3c5f14bc76f27442d5a180909ec2e144be68711737211e8d70bda6502a88a4a6558d9c857d6028b1fdfdf2d7df9d2a415b8754d194d17b29d09444d786a0478e62141c31410eda02abcd05769473e5fa75496d49aad564d139af8efee156d8089a253f4cd49814ed34fa9346701d66738938cbc5d54ba2adeb11dbe76f828dec46b82a6ff51dcf17e49771a6d88ad61996a5552809f78746562eba9d7aa9d4525d969c662628b857133d024ead8205bd3f367f3523c6ee9ff9b1784f47de41a1c196a73b178fce869b445c9a1b872a83ba946f2ca41232cdea11c53b7652dcfe615e9b3f9f0153f706eeee404e88e8736b3712e8ab9da9c9b75e419a615c3c1d1357886f77c8eaebbf4501dc1fef854fc5cc7f2f071c0a7411eb78bc14b25307cc7e4bde334ee3df0c53d6159751e82248f280434e466712dfe33981e0171e67352cdd86838eff3acd6e05592e2d1e441ddae9450a144da5c7926ee673458a59bc98c9e4d68f8b04134cc24ffdc2034c4ac3b46d6dd98ecca28dcaa7857f0c3f73a0809ae3c5dd2205555fd7cc6444024869d4f6d7dfe043917660119433c76239b17cdc8fad6c92f3756d206d67800e4e2566a73b27bb7a51dac62bc8411cdab0c5920a821e8ae6bb7779afb69f53452ad0b33c60c41a2be2c3aa94d46e97ffb5b2ebd9adc99eab85d5a3a73d4935f7ea6867d8277040c49f3ac822a4c7c7d67aba4f45765a46fccb7f79c332ac708b58911dc000c49d54fe6137be87df7f364a94fa5642338b6eeaf4152f7410ba97b0169aad82a9c4dd2d353cc3a8f57aaa90b5335f325b3e6e01",
"signature": "0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505",
"committee_bits": "0x08",
"data": {
"slot": "1",
"index": "1",
"beacon_block_root": "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"source": {
"epoch": "1",
"root": "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"
"committee_index" : "1063251753",
"attester_index" : "1640609292",
"data" : {
"slot" : "4669978815449698508",
"index" : "4668326327938047084",
"beacon_block_root" : "0x6fdfab408c56b6105a76eff5c0435d09fc6ed7a938e7f946cf74fbbb9416428f",
"source" : {
"epoch" : "542310465",
"root" : "0x499db7404cbff78670f0209f1932346fef68d985cb55a8d27472742bdf54d379"
},
"target": {
"epoch": "1",
"root": "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"
"target" : {
"epoch" : "542695214",
"root" : "0x1f86d83f0bf91cc0d7e07410828140e0dddbb331dc20b6743f9f79e549b50b11"
}
}
},
"signature" : "0xb3a22ab9ec46aec35a9dacfb9036375ea1528041a926cb9d2d315ab964e82be5d6990e7fef2343f2dbb4c2b7dd74687f11144beaeb5758ebe349762b4dbde5e67bbc8d89a95a803c6610631d178249917cbf0d8b11bd8740f3cb767c843aa88c"
}
]

0 comments on commit e7a9b11

Please sign in to comment.