From 184eae7e293e7303f4c237e644fe10a204fcb2c2 Mon Sep 17 00:00:00 2001 From: Mehdi AOUADI Date: Wed, 14 Aug 2024 16:43:10 +0200 Subject: [PATCH] use schema definition cache --- ...eateAggregateAttestationRequestV2Test.java | 9 +-- .../remote/RemoteValidatorApiHandler.java | 4 +- .../typedef/OkHttpValidatorTypeDefClient.java | 12 ++-- .../CreateAggregateAttestationRequestV2.java | 63 +++++++++---------- .../remote/RemoteValidatorApiHandlerTest.java | 8 +-- 5 files changed, 46 insertions(+), 50 deletions(-) diff --git a/validator/remote/src/integration-test/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2Test.java b/validator/remote/src/integration-test/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2Test.java index 986edda256d..46afd2d9d96 100644 --- a/validator/remote/src/integration-test/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2Test.java +++ b/validator/remote/src/integration-test/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2Test.java @@ -37,6 +37,7 @@ import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData; import tech.pegasys.teku.spec.datastructures.operations.Attestation; import tech.pegasys.teku.spec.networks.Eth2Network; +import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache; import tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod; import tech.pegasys.teku.validator.remote.typedef.AbstractTypeDefRequestTestBase; @@ -52,7 +53,8 @@ public class CreateAggregateAttestationRequestV2Test extends AbstractTypeDefRequ @BeforeEach void setupRequest() { createAggregateAttestationRequestV2 = - new CreateAggregateAttestationRequestV2(mockWebServer.url("/"), okHttpClient, spec, slot); + new CreateAggregateAttestationRequestV2( + mockWebServer.url("/"), okHttpClient, slot, new SchemaDefinitionCache(spec)); responseBodyBuffer = new Buffer(); } @@ -69,7 +71,7 @@ public void getAggregateAttestation_makesExpectedRequest() throws Exception { mockWebServer.enqueue(new MockResponse().setResponseCode(SC_NO_CONTENT)); - createAggregateAttestationRequestV2.createAggregate(attestationHashTreeRoot, committeeIndex); + createAggregateAttestationRequestV2.submit(attestationHashTreeRoot, committeeIndex); final RecordedRequest request = mockWebServer.takeRequest(); @@ -100,8 +102,7 @@ public void shouldGetAggregateAttestation() { : attestation.getData().getIndex(); final Optional> maybeAttestationAndMetaData = - createAggregateAttestationRequestV2.createAggregate( - attestation.hashTreeRoot(), committeeIndex); + createAggregateAttestationRequestV2.submit(attestation.hashTreeRoot(), committeeIndex); assertThat(maybeAttestationAndMetaData).isPresent(); assertThat(maybeAttestationAndMetaData.get().getData()) .isEqualTo(getAggregateAttestationResponse.getData()); diff --git a/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandler.java b/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandler.java index c8b03528ae9..bddacbdd8b2 100644 --- a/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandler.java +++ b/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandler.java @@ -273,12 +273,14 @@ public SafeFuture> createAggregate( final UInt64 slot, final Bytes32 attestationHashTreeRoot, final Optional committeeIndex) { + // Use attestation v2 api post Electra only. This logic can be removed once we reach the Electra + // milestone if (spec.atSlot(slot).getMilestone().isGreaterThanOrEqualTo(SpecMilestone.ELECTRA) && committeeIndex.isPresent()) { return sendRequest( () -> typeDefClient - .createAggregateV2(slot, attestationHashTreeRoot, committeeIndex.get()) + .createAggregate(slot, attestationHashTreeRoot, committeeIndex.get()) .map(ObjectAndMetaData::getData)); } diff --git a/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/OkHttpValidatorTypeDefClient.java b/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/OkHttpValidatorTypeDefClient.java index 07279e5ab5a..cfbf2c27b73 100644 --- a/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/OkHttpValidatorTypeDefClient.java +++ b/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/OkHttpValidatorTypeDefClient.java @@ -76,8 +76,6 @@ import tech.pegasys.teku.validator.remote.typedef.handlers.SendSubscribeToSyncCommitteeSubnetsRequest; import tech.pegasys.teku.validator.remote.typedef.handlers.SendSyncCommitteeMessagesRequest; import tech.pegasys.teku.validator.remote.typedef.handlers.SendValidatorLivenessRequest; -import tech.pegasys.teku.validator.remote.typedef.handlers.SendSyncCommitteeMessagesRequest; -import tech.pegasys.teku.validator.remote.typedef.handlers.SendValidatorLivenessRequest; import tech.pegasys.teku.validator.remote.typedef.handlers.SubscribeToBeaconCommitteeRequest; import tech.pegasys.teku.validator.remote.typedef.handlers.SubscribeToPersistentSubnetsRequest; import tech.pegasys.teku.validator.remote.typedef.handlers.SyncCommitteeSelectionsRequest; @@ -274,12 +272,12 @@ public Optional createAggregate( return createAggregateAttestationRequest.submit(slot, attestationHashTreeRoot); } - public Optional> createAggregateV2( - final UInt64 slot, final Bytes32 attestationHashTreeRoot, final UInt64 committeeIndex) { + public Optional> createAggregate( + final UInt64 slot, final Bytes32 attestationHashTreeRoot, final UInt64 committeeIndex) { CreateAggregateAttestationRequestV2 createAggregateAttestationRequestV2 = - new CreateAggregateAttestationRequestV2(getBaseEndpoint(), getOkHttpClient(), spec, slot); - return createAggregateAttestationRequestV2.createAggregate( - attestationHashTreeRoot, committeeIndex); + new CreateAggregateAttestationRequestV2( + getBaseEndpoint(), getOkHttpClient(), slot, schemaDefinitionCache); + return createAggregateAttestationRequestV2.submit(attestationHashTreeRoot, committeeIndex); } public Optional> sendValidatorsLiveness( diff --git a/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2.java b/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2.java index d3110f4e200..d84cfacc623 100644 --- a/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2.java +++ b/validator/remote/src/main/java/tech/pegasys/teku/validator/remote/typedef/handlers/CreateAggregateAttestationRequestV2.java @@ -26,60 +26,57 @@ import org.apache.tuweni.bytes.Bytes32; import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition; import tech.pegasys.teku.infrastructure.unsigned.UInt64; -import tech.pegasys.teku.spec.Spec; import tech.pegasys.teku.spec.SpecMilestone; import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData; import tech.pegasys.teku.spec.datastructures.operations.Attestation; import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema; +import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache; import tech.pegasys.teku.validator.remote.typedef.ResponseHandler; public class CreateAggregateAttestationRequestV2 extends AbstractTypeDefRequest { - final AttestationSchema attestationSchema; - final UInt64 slot; - - private final ResponseHandler responseHandler; - - private final DeserializableTypeDefinition - getAggregateAttestationTypeDef; + private final SchemaDefinitionCache schemaDefinitionCache; + private final UInt64 slot; public CreateAggregateAttestationRequestV2( final HttpUrl baseEndpoint, final OkHttpClient okHttpClient, - final Spec spec, - final UInt64 slot) { + final UInt64 slot, + final SchemaDefinitionCache schemaDefinitionCache) { super(baseEndpoint, okHttpClient); this.slot = slot; - this.attestationSchema = - spec.atSlot(slot) - .getSchemaDefinitions() - .getAttestationSchema() - .castTypeToAttestationSchema(); - this.getAggregateAttestationTypeDef = - DeserializableTypeDefinition.object(GetAggregateAttestationResponse.class) - .initializer(GetAggregateAttestationResponse::new) - .withField( - "version", - DeserializableTypeDefinition.enumOf(SpecMilestone.class), - GetAggregateAttestationResponse::getSpecMilestone, - GetAggregateAttestationResponse::setSpecMilestone) - .withField( - "data", - attestationSchema.getJsonTypeDefinition(), - GetAggregateAttestationResponse::getData, - GetAggregateAttestationResponse::setData) - .build(); - - this.responseHandler = new ResponseHandler<>(getAggregateAttestationTypeDef); + this.schemaDefinitionCache = schemaDefinitionCache; } - public Optional> createAggregate( + public Optional> submit( final Bytes32 attestationHashTreeRoot, final UInt64 committeeIndex) { final Map queryParams = new HashMap<>(); queryParams.put(SLOT, slot.toString()); queryParams.put(ATTESTATION_DATA_ROOT, attestationHashTreeRoot.toHexString()); queryParams.put(COMMITTEE_INDEX, committeeIndex.toString()); - return get(GET_AGGREGATE_V2, queryParams, this.responseHandler) + + final AttestationSchema attestationSchema = + schemaDefinitionCache.atSlot(slot).getAttestationSchema().castTypeToAttestationSchema(); + final DeserializableTypeDefinition + getAggregateAttestationTypeDef = + DeserializableTypeDefinition.object(GetAggregateAttestationResponse.class) + .initializer(GetAggregateAttestationResponse::new) + .withField( + "version", + DeserializableTypeDefinition.enumOf(SpecMilestone.class), + GetAggregateAttestationResponse::getSpecMilestone, + GetAggregateAttestationResponse::setSpecMilestone) + .withField( + "data", + attestationSchema.getJsonTypeDefinition(), + GetAggregateAttestationResponse::getData, + GetAggregateAttestationResponse::setData) + .build(); + + final ResponseHandler responseHandler = + new ResponseHandler<>(getAggregateAttestationTypeDef); + + return get(GET_AGGREGATE_V2, queryParams, responseHandler) .map( getAggregateAttestationResponse -> new ObjectAndMetaData<>( diff --git a/validator/remote/src/test/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandlerTest.java b/validator/remote/src/test/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandlerTest.java index 3b3a96fa149..2b5eb542588 100644 --- a/validator/remote/src/test/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandlerTest.java +++ b/validator/remote/src/test/java/tech/pegasys/teku/validator/remote/RemoteValidatorApiHandlerTest.java @@ -74,7 +74,6 @@ import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration; import tech.pegasys.teku.spec.datastructures.genesis.GenesisData; import tech.pegasys.teku.spec.datastructures.metadata.BlockContainerAndMetaData; -import tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof; import tech.pegasys.teku.spec.datastructures.operations.Attestation; import tech.pegasys.teku.spec.datastructures.operations.AttestationData; import tech.pegasys.teku.spec.datastructures.state.Validator; @@ -613,7 +612,7 @@ public void createAggregate_ShouldUseV1ApiPreElectra() { ignoreFuture(apiHandler.createAggregate(slot, attHashTreeRoot, Optional.empty())); asyncRunner.executeQueuedActions(); - verify(typeDefClient, never()).createAggregateV2(any(), any(), any()); + verify(typeDefClient, never()).createAggregate(any(), any(), any()); verify(typeDefClient).createAggregate(slot, attHashTreeRoot); } @@ -625,13 +624,12 @@ public void createAggregate_ShouldUseV2ApiPostElectra() { final Spec electraSpec = TestSpecFactory.createMainnetElectra(); apiHandler = - new RemoteValidatorApiHandler( - endpoint, electraSpec, typeDefClient, asyncRunner, true); + new RemoteValidatorApiHandler(endpoint, electraSpec, typeDefClient, asyncRunner, true); ignoreFuture(apiHandler.createAggregate(slot, attHashTreeRoot, Optional.of(committeeIndex))); asyncRunner.executeQueuedActions(); verify(typeDefClient, never()).createAggregate(any(), any()); - verify(typeDefClient).createAggregateV2(slot, attHashTreeRoot, committeeIndex); + verify(typeDefClient).createAggregate(slot, attHashTreeRoot, committeeIndex); } @Test