Skip to content

Commit

Permalink
Use JsonTypeDef for PostAttesterDuties request (#8023)
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh authored Mar 5, 2024
1 parent 70b7980 commit 7870a5e
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 258 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

package tech.pegasys.teku.validator.remote.typedef;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assumptions.assumeThat;
import static tech.pegasys.teku.ethereum.json.types.beacon.StateValidatorDataBuilder.STATE_VALIDATORS_RESPONSE_TYPE;
import static tech.pegasys.teku.ethereum.json.types.validator.AttesterDutiesBuilder.ATTESTER_DUTIES_RESPONSE_TYPE;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_METHOD_NOT_ALLOWED;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_NOT_FOUND;
Expand All @@ -28,6 +30,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.unimi.dsi.fastutil.ints.IntList;
import java.util.List;
import java.util.Optional;
import okhttp3.mockwebserver.MockResponse;
Expand All @@ -39,6 +42,8 @@
import tech.pegasys.teku.api.exceptions.RemoteServiceNotAvailableException;
import tech.pegasys.teku.api.response.v1.beacon.ValidatorStatus;
import tech.pegasys.teku.ethereum.json.types.beacon.StateValidatorData;
import tech.pegasys.teku.ethereum.json.types.validator.AttesterDuties;
import tech.pegasys.teku.ethereum.json.types.validator.AttesterDuty;
import tech.pegasys.teku.infrastructure.ssz.SszDataAssert;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
Expand Down Expand Up @@ -410,6 +415,43 @@ private StateValidatorData generateStateValidatorData() {
validator);
}

@TestTemplate
public void postAttesterDuties_WhenSuccess_ReturnsResponse()
throws JsonProcessingException, InterruptedException {
final List<AttesterDuty> duties = List.of(randomAttesterDuty(), randomAttesterDuty());
final AttesterDuties response =
new AttesterDuties(true, dataStructureUtil.randomBytes32(), duties);

final String body = serialize(response, ATTESTER_DUTIES_RESPONSE_TYPE);
mockWebServer.enqueue(new MockResponse().setResponseCode(SC_OK).setBody(body));

final UInt64 epoch = UInt64.ONE;
final IntList validatorIndices = IntList.of(1, 2);
Optional<AttesterDuties> result =
okHttpValidatorTypeDefClient.postAttesterDuties(epoch, validatorIndices);

final RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertThat(recordedRequest.getPath()).isEqualTo("/eth/v1/validator/duties/attester/" + epoch);
assertThat(recordedRequest.getMethod()).isEqualTo("POST");
assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo(JSON_CONTENT_TYPE);
assertThat(recordedRequest.getBody().readByteArray())
.isEqualTo("[\"1\",\"2\"]".getBytes(UTF_8));

assertThat(result).isPresent();
assertThat(result.get()).isEqualTo(response);
}

private AttesterDuty randomAttesterDuty() {
return new AttesterDuty(
dataStructureUtil.randomPublicKey(),
dataStructureUtil.randomValidatorIndex().intValue(),
dataStructureUtil.randomPositiveInt(),
dataStructureUtil.randomPositiveInt(),
dataStructureUtil.randomPositiveInt(),
dataStructureUtil.randomPositiveInt(),
dataStructureUtil.randomSlot());
}

private void verifyRegisterValidatorsPostRequest(
final RecordedRequest recordedRequest, final String expectedContentType) {
assertThat(recordedRequest.getPath()).isEqualTo("/eth/v1/validator/register_validator");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.ethereum.json.types.beacon.StateValidatorData;
import tech.pegasys.teku.ethereum.json.types.validator.AttesterDuties;
import tech.pegasys.teku.ethereum.json.types.validator.AttesterDuty;
import tech.pegasys.teku.ethereum.json.types.validator.BeaconCommitteeSelectionProof;
import tech.pegasys.teku.ethereum.json.types.validator.ProposerDuties;
import tech.pegasys.teku.ethereum.json.types.validator.SyncCommitteeDuties;
Expand Down Expand Up @@ -208,16 +207,7 @@ private <T> Map<BLSPublicKey, T> convertToValidatorMapTypeDef(
@Override
public SafeFuture<Optional<AttesterDuties>> getAttestationDuties(
final UInt64 epoch, final IntCollection validatorIndices) {
return sendRequest(
() ->
apiClient
.getAttestationDuties(epoch, validatorIndices)
.map(
response ->
new AttesterDuties(
response.executionOptimistic,
response.dependentRoot,
response.data.stream().map(this::mapToApiAttesterDuties).toList())));
return sendRequest(() -> typeDefClient.postAttesterDuties(epoch, validatorIndices));
}

@Override
Expand Down Expand Up @@ -249,18 +239,6 @@ public SafeFuture<Optional<ProposerDuties>> getProposerDuties(final UInt64 epoch
return sendRequest(() -> typeDefClient.getProposerDuties(epoch));
}

private AttesterDuty mapToApiAttesterDuties(
final tech.pegasys.teku.api.response.v1.validator.AttesterDuty attesterDuty) {
return new AttesterDuty(
attesterDuty.pubkey.asBLSPublicKey(),
attesterDuty.validatorIndex.intValue(),
attesterDuty.committeeLength.intValue(),
attesterDuty.committeeIndex.intValue(),
attesterDuty.committeesAtSlot.intValue(),
attesterDuty.validatorCommitteeIndex.intValue(),
attesterDuty.slot);
}

@Override
public SafeFuture<Optional<AttestationData>> createAttestationData(
final UInt64 slot, final int committeeIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static java.util.Collections.emptyMap;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_NOT_FOUND;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_AGGREGATE;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_ATTESTATION_DUTIES;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_BLOCK_HEADER;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_GENESIS;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_PROPOSER_DUTIES;
Expand Down Expand Up @@ -63,7 +62,6 @@
import tech.pegasys.teku.api.response.v1.validator.GetAggregatedAttestationResponse;
import tech.pegasys.teku.api.response.v1.validator.GetProposerDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.GetSyncCommitteeContributionResponse;
import tech.pegasys.teku.api.response.v1.validator.PostAttesterDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.PostSyncDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse;
import tech.pegasys.teku.api.schema.Attestation;
Expand Down Expand Up @@ -128,16 +126,6 @@ public Optional<List<ValidatorResponse>> getValidators(final List<String> valida
.map(response -> response.data);
}

@Override
public Optional<PostAttesterDutiesResponse> getAttestationDuties(
final UInt64 epoch, final Collection<Integer> validatorIndices) {
return post(
GET_ATTESTATION_DUTIES,
Map.of("epoch", epoch.toString()),
validatorIndices.stream().map(UInt64::valueOf).toList(),
createHandler(PostAttesterDutiesResponse.class));
}

@Override
public Optional<GetProposerDutiesResponse> getProposerDuties(final UInt64 epoch) {
return get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse;
import tech.pegasys.teku.api.response.v1.beacon.ValidatorResponse;
import tech.pegasys.teku.api.response.v1.validator.GetProposerDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.PostAttesterDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.PostSyncDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse;
import tech.pegasys.teku.api.schema.Attestation;
Expand All @@ -43,9 +42,6 @@ public interface ValidatorRestApiClient {

Optional<List<ValidatorResponse>> getValidators(List<String> validatorIds);

Optional<PostAttesterDutiesResponse> getAttestationDuties(
final UInt64 epoch, final Collection<Integer> validatorIndices);

Optional<GetProposerDutiesResponse> getProposerDuties(final UInt64 epoch);

Optional<PostDataFailureResponse> sendSignedAttestations(List<Attestation> attestation);
Expand Down
Loading

0 comments on commit 7870a5e

Please sign in to comment.