Skip to content

Commit

Permalink
Use JsonTypeDef for PostSyncDuties request (#8033)
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh authored Mar 5, 2024
1 parent 7870a5e commit 5989775
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;
import static tech.pegasys.teku.ethereum.json.types.validator.SyncCommitteeDutiesBuilder.SYNC_COMMITTEE_DUTIES_TYPE;
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.json.JsonUtil.parse;
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE;

import it.unimi.dsi.fastutil.ints.IntList;
Expand All @@ -27,8 +29,6 @@
import okhttp3.Response;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.api.response.v1.validator.PostSyncDutiesResponse;
import tech.pegasys.teku.api.schema.BLSPubKey;
import tech.pegasys.teku.beacon.sync.events.SyncState;
import tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest;
import tech.pegasys.teku.beaconrestapi.handlers.v1.validator.PostSyncDuties;
Expand Down Expand Up @@ -68,11 +68,9 @@ void shouldGetSyncCommitteeDuties() throws IOException {
post(PostSyncDuties.ROUTE.replace("{epoch}", "1"), jsonProvider.objectToJSON(validators));

Assertions.assertThat(response.code()).isEqualTo(SC_OK);
final PostSyncDutiesResponse dutiesResponse =
jsonProvider.jsonToObject(response.body().string(), PostSyncDutiesResponse.class);
assertThat(dutiesResponse.data.get(0))
.isEqualTo(
new tech.pegasys.teku.api.response.v1.validator.SyncCommitteeDuty(
new BLSPubKey(VALIDATOR_KEYS.get(1).getPublicKey()), ONE, IntSet.of(11)));
final SyncCommitteeDuties committeeDuties =
parse(response.body().string(), SYNC_COMMITTEE_DUTIES_TYPE);
assertThat(committeeDuties.getDuties().get(0))
.isEqualTo(new SyncCommitteeDuty(VALIDATOR_KEYS.get(1).getPublicKey(), 1, IntSet.of(11)));
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
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.ethereum.json.types.validator.SyncCommitteeDutiesBuilder.SYNC_COMMITTEE_DUTIES_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;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_NO_CONTENT;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
import static tech.pegasys.teku.infrastructure.json.JsonUtil.serialize;
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE;
import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntSet;
import java.util.List;
import java.util.Optional;
import okhttp3.mockwebserver.MockResponse;
Expand All @@ -44,6 +47,8 @@
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.SyncCommitteeDuties;
import tech.pegasys.teku.ethereum.json.types.validator.SyncCommitteeDuty;
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 @@ -94,9 +99,9 @@ void blockProductionFallbacksToNonBlindedFlowIfBlindedEndpointIsNotAvailable()

final BlockContainer blockContainer;
if (specMilestone.isGreaterThanOrEqualTo(SpecMilestone.DENEB)) {
blockContainer = dataStructureUtil.randomBlockContents(UInt64.ONE);
blockContainer = dataStructureUtil.randomBlockContents(ONE);
} else {
blockContainer = dataStructureUtil.randomBeaconBlock(UInt64.ONE);
blockContainer = dataStructureUtil.randomBeaconBlock(ONE);
}

mockWebServer.enqueue(
Expand Down Expand Up @@ -159,7 +164,7 @@ void publishesBlindedBlockJsonEncoded() throws InterruptedException, JsonProcess
final String expectedRequest =
serialize(
signedBeaconBlock,
spec.atSlot(UInt64.ONE)
spec.atSlot(ONE)
.getSchemaDefinitions()
.getSignedBlindedBlockContainerSchema()
.getJsonTypeDefinition());
Expand Down Expand Up @@ -189,8 +194,8 @@ void getsSyncingStatus() {
assertThat(result)
.satisfies(
syncingStatus -> {
assertThat(syncingStatus.getHeadSlot()).isEqualTo(UInt64.ONE);
assertThat(syncingStatus.getSyncDistance()).isEqualTo(UInt64.ONE);
assertThat(syncingStatus.getHeadSlot()).isEqualTo(ONE);
assertThat(syncingStatus.getSyncDistance()).isEqualTo(ONE);
assertThat(syncingStatus.isSyncing()).isTrue();
assertThat(syncingStatus.getIsOptimistic()).hasValue(true);
});
Expand Down Expand Up @@ -415,6 +420,36 @@ private StateValidatorData generateStateValidatorData() {
validator);
}

@TestTemplate
public void postSyncDuties_WhenSuccess_ReturnsResponse()
throws JsonProcessingException, InterruptedException {
final List<SyncCommitteeDuty> duties =
List.of(
new SyncCommitteeDuty(
dataStructureUtil.randomPublicKey(),
dataStructureUtil.randomValidatorIndex().intValue(),
IntSet.of(1, 2)));
final SyncCommitteeDuties response = new SyncCommitteeDuties(true, duties);

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

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

final RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertThat(recordedRequest.getPath()).isEqualTo("/eth/v1/validator/duties/sync/" + 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);
}

@TestTemplate
public void postAttesterDuties_WhenSuccess_ReturnsResponse()
throws JsonProcessingException, InterruptedException {
Expand All @@ -425,7 +460,7 @@ public void postAttesterDuties_WhenSuccess_ReturnsResponse()
final String body = serialize(response, ATTESTER_DUTIES_RESPONSE_TYPE);
mockWebServer.enqueue(new MockResponse().setResponseCode(SC_OK).setBody(body));

final UInt64 epoch = UInt64.ONE;
final UInt64 epoch = ONE;
final IntList validatorIndices = IntList.of(1, 2);
Optional<AttesterDuties> result =
okHttpValidatorTypeDefClient.postAttesterDuties(epoch, validatorIndices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.common.base.Throwables;
import it.unimi.dsi.fastutil.ints.IntCollection;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -39,7 +38,6 @@
import tech.pegasys.teku.api.migrated.ValidatorLivenessAtEpoch;
import tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse;
import tech.pegasys.teku.api.response.v1.beacon.ValidatorStatus;
import tech.pegasys.teku.api.response.v1.validator.PostSyncDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse;
import tech.pegasys.teku.api.schema.altair.ContributionAndProof;
import tech.pegasys.teku.bls.BLSPublicKey;
Expand All @@ -49,7 +47,6 @@
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;
import tech.pegasys.teku.ethereum.json.types.validator.SyncCommitteeDuty;
import tech.pegasys.teku.ethereum.json.types.validator.SyncCommitteeSelectionProof;
import tech.pegasys.teku.infrastructure.async.AsyncRunner;
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingRunnable;
Expand Down Expand Up @@ -213,25 +210,7 @@ public SafeFuture<Optional<AttesterDuties>> getAttestationDuties(
@Override
public SafeFuture<Optional<SyncCommitteeDuties>> getSyncCommitteeDuties(
final UInt64 epoch, final IntCollection validatorIndices) {
return sendRequest(
() ->
apiClient
.getSyncCommitteeDuties(epoch, validatorIndices)
.map(this::responseToSyncCommitteeDuties));
}

private SyncCommitteeDuties responseToSyncCommitteeDuties(final PostSyncDutiesResponse response) {
return new SyncCommitteeDuties(
response.executionOptimistic,
response.data.stream()
.map(
duty ->
new SyncCommitteeDuty(
duty.pubkey.asBLSPublicKey(),
duty.validatorIndex.intValue(),
IntOpenHashSet.toSet(
duty.committeeIndices.stream().mapToInt(UInt64::intValue))))
.toList());
return sendRequest(() -> typeDefClient.postSyncDuties(epoch, validatorIndices));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_GENESIS;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_PROPOSER_DUTIES;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_SYNC_COMMITTEE_CONTRIBUTION;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_SYNC_COMMITTEE_DUTIES;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.GET_VALIDATORS;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.PREPARE_BEACON_PROPOSER;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.SEND_CONTRIBUTION_AND_PROOF;
Expand All @@ -37,7 +36,6 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -62,7 +60,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.PostSyncDutiesResponse;
import tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.SignedAggregateAndProof;
Expand Down Expand Up @@ -212,16 +209,6 @@ public Optional<PostDataFailureResponse> sendSyncCommitteeMessages(
jsonProvider, PostDataFailureResponse.class));
}

@Override
public Optional<PostSyncDutiesResponse> getSyncCommitteeDuties( // todo remove
final UInt64 epoch, final Collection<Integer> validatorIndices) {
return post(
GET_SYNC_COMMITTEE_DUTIES,
Map.of("epoch", epoch.toString()),
validatorIndices.stream().map(UInt64::valueOf).toList(),
createHandler(PostSyncDutiesResponse.class));
}

@Override
public void subscribeToSyncCommitteeSubnets(
final List<SyncCommitteeSubnetSubscription> subnetSubscriptions) {
Expand Down
Loading

0 comments on commit 5989775

Please sign in to comment.