Skip to content

Commit

Permalink
use schema definition cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Aug 14, 2024
1 parent 6682ef7 commit 184eae7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}

Expand All @@ -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();

Expand Down Expand Up @@ -100,8 +102,7 @@ public void shouldGetAggregateAttestation() {
: attestation.getData().getIndex();

final Optional<ObjectAndMetaData<Attestation>> maybeAttestationAndMetaData =
createAggregateAttestationRequestV2.createAggregate(
attestation.hashTreeRoot(), committeeIndex);
createAggregateAttestationRequestV2.submit(attestation.hashTreeRoot(), committeeIndex);
assertThat(maybeAttestationAndMetaData).isPresent();
assertThat(maybeAttestationAndMetaData.get().getData())
.isEqualTo(getAggregateAttestationResponse.getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,14 @@ public SafeFuture<Optional<Attestation>> createAggregate(
final UInt64 slot,
final Bytes32 attestationHashTreeRoot,
final Optional<UInt64> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -274,12 +272,12 @@ public Optional<Attestation> createAggregate(
return createAggregateAttestationRequest.submit(slot, attestationHashTreeRoot);
}

public Optional<ObjectAndMetaData<Attestation>> createAggregateV2(
final UInt64 slot, final Bytes32 attestationHashTreeRoot, final UInt64 committeeIndex) {
public Optional<ObjectAndMetaData<Attestation>> 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<List<ValidatorLivenessAtEpoch>> sendValidatorsLiveness(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attestation> attestationSchema;
final UInt64 slot;

private final ResponseHandler<GetAggregateAttestationResponse> responseHandler;

private final DeserializableTypeDefinition<GetAggregateAttestationResponse>
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<ObjectAndMetaData<Attestation>> createAggregate(
public Optional<ObjectAndMetaData<Attestation>> submit(
final Bytes32 attestationHashTreeRoot, final UInt64 committeeIndex) {
final Map<String, String> 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<Attestation> attestationSchema =
schemaDefinitionCache.atSlot(slot).getAttestationSchema().castTypeToAttestationSchema();
final DeserializableTypeDefinition<GetAggregateAttestationResponse>
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<GetAggregateAttestationResponse> responseHandler =
new ResponseHandler<>(getAggregateAttestationTypeDef);

return get(GET_AGGREGATE_V2, queryParams, responseHandler)
.map(
getAggregateAttestationResponse ->
new ObjectAndMetaData<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down

0 comments on commit 184eae7

Please sign in to comment.