Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Aug 14, 2024
1 parent 9d6c8da commit b0ae3eb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse;
import tech.pegasys.teku.infrastructure.json.JsonUtil;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.teku.validator.api.SubmitDataError;
import tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod;
import tech.pegasys.teku.validator.remote.typedef.AbstractTypeDefRequestTestBase;

Expand All @@ -48,7 +47,7 @@ public class PostAttestationsRequestTest extends AbstractTypeDefRequestTestBase

@BeforeEach
void setupRequest() {
request = new PostAttestationsRequest(spec, mockWebServer.url("/"), okHttpClient);
request = new PostAttestationsRequest(mockWebServer.url("/"), okHttpClient);
}

@TestTemplate
Expand All @@ -58,7 +57,7 @@ public void getAggregateAttestation_makesExpectedRequest() throws Exception {

mockWebServer.enqueue(new MockResponse().setResponseCode(SC_NO_CONTENT));

request.postAttestations(attestations, specMilestone);
request.submit(attestations, specMilestone);

final RecordedRequest request = mockWebServer.takeRequest();
assertThat(request.getMethod()).isEqualTo("POST");
Expand All @@ -84,8 +83,7 @@ public void shouldSubmitAttestations() throws InterruptedException {

final List<Attestation> attestations =
List.of(dataStructureUtil.randomAttestation(), dataStructureUtil.randomAttestation());
final Optional<PostDataFailureResponse> response =
request.postAttestations(attestations, specMilestone);
final List<SubmitDataError> response = request.submit(attestations, specMilestone);
assertThat(response).isEmpty();

final RecordedRequest recordedRequest = mockWebServer.takeRequest();
Expand All @@ -99,7 +97,7 @@ public void shouldHandleBadRequest() throws InterruptedException {

final List<Attestation> attestations =
List.of(dataStructureUtil.randomAttestation(), dataStructureUtil.randomAttestation());
assertThatThrownBy(() -> request.postAttestations(attestations, specMilestone))
assertThatThrownBy(() -> request.submit(attestations, specMilestone))
.isInstanceOf(IllegalArgumentException.class);

final RecordedRequest recordedRequest = mockWebServer.takeRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package tech.pegasys.teku.validator.remote;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -225,19 +224,10 @@ public SafeFuture<Optional<AttestationData>> createAttestationData(
@Override
public SafeFuture<List<SubmitDataError>> sendSignedAttestations(
final List<Attestation> attestations) {
if (attestations.isEmpty()) {
return SafeFuture.completedFuture(emptyList());
}

final SpecMilestone specMilestone = spec.atSlot(attestations.getFirst().getData().getSlot()).getMilestone();

final SpecMilestone specMilestone =
spec.atSlot(attestations.getFirst().getData().getSlot()).getMilestone();
if (specMilestone.isGreaterThanOrEqualTo(SpecMilestone.ELECTRA)) {
return sendRequest(
() ->
typeDefClient
.postSignedAttestations(attestations, specMilestone)
.orElse(emptyList()));

return sendRequest(() -> typeDefClient.postSignedAttestations(attestations, specMilestone));
}
return sendRequest(() -> typeDefClient.sendSignedAttestations(attestations));
}
Expand Down Expand Up @@ -392,6 +382,6 @@ public static RemoteValidatorApiHandler create(
final OkHttpValidatorTypeDefClient typeDefClient =
new OkHttpValidatorTypeDefClient(httpClient, endpoint, spec, preferSszBlockEncoding);
return new RemoteValidatorApiHandler(
endpoint,spec, typeDefClient, asyncRunner, usePostValidatorsEndpoint);
endpoint, spec, typeDefClient, asyncRunner, usePostValidatorsEndpoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.migrated.ValidatorLivenessAtEpoch;
import tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.ethereum.json.types.beacon.StateValidatorData;
import tech.pegasys.teku.ethereum.json.types.node.PeerCount;
Expand Down Expand Up @@ -148,12 +147,6 @@ public SendSignedBlockResult sendSignedBlock(
return sendSignedBlockRequest.submit(blockContainer, broadcastValidationLevel);
}

public Optional<PostDataFailureResponse> postSignedAttestations(
final List<Attestation> attestations, final SpecMilestone specMilestone) {
final PostAttestationsRequest postAttestationsRequest = new PostAttestationsRequest(spec, getBaseEndpoint(), getOkHttpClient());
return postAttestationsRequest.postAttestations(attestations, specMilestone);
}

@Deprecated
public Optional<BlockContainerAndMetaData> createUnsignedBlock(
final UInt64 slot,
Expand Down Expand Up @@ -306,4 +299,11 @@ public List<SubmitDataError> sendSignedAttestations(final List<Attestation> atte
new SendSignedAttestationsRequest(getBaseEndpoint(), getOkHttpClient());
return sendSignedAttestationsRequest.submit(attestations);
}

public List<SubmitDataError> postSignedAttestations(
final List<Attestation> attestations, final SpecMilestone specMilestone) {
final PostAttestationsRequest postAttestationsRequest =
new PostAttestationsRequest(getBaseEndpoint(), getOkHttpClient());
return postAttestationsRequest.submit(attestations, specMilestone);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ protected <T, TObject> Optional<T> postJson(
responseHandler);
}

protected <T, TObject> Optional<T> postJson(
final ValidatorApiMethod apiMethod,
final Map<String, String> urlParams,
final Map<String, String> queryParams,
final TObject requestBodyObj,
final SerializableTypeDefinition<TObject> objectTypeDefinition,
final ResponseHandler<T> responseHandler) {
return postJson(
apiMethod,
urlParams,
queryParams,
emptyMap(),
requestBodyObj,
objectTypeDefinition,
responseHandler);
}

protected <T, TObject> Optional<T> postJson(
final ValidatorApiMethod apiMethod,
final Map<String, String> urlParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,49 @@

import static java.util.Collections.emptyMap;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_CONSENSUS_VERSION;
import static tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition.listOf;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.SEND_SIGNED_ATTESTATION_V2;
import static tech.pegasys.teku.validator.remote.typedef.FailureListResponse.getFailureListResponseResponseHandler;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse;
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.validator.remote.typedef.ResponseHandler;
import tech.pegasys.teku.validator.api.SubmitDataError;
import tech.pegasys.teku.validator.remote.typedef.FailureListResponse;

public class PostAttestationsRequest extends AbstractTypeDefRequest {

private final Spec spec;

public PostAttestationsRequest(
final Spec spec, final HttpUrl baseEndpoint, final OkHttpClient okHttpClient) {
public PostAttestationsRequest(final HttpUrl baseEndpoint, final OkHttpClient okHttpClient) {
super(baseEndpoint, okHttpClient);
this.spec = spec;
}

public Optional<PostDataFailureResponse> postAttestations(
public List<SubmitDataError> submit(
final List<Attestation> attestations, final SpecMilestone specMilestone) {
final DeserializableTypeDefinition<Attestation> attestationTypeDefinition =
spec.forMilestone(specMilestone)
.getSchemaDefinitions()
.getAttestationSchema()
.castTypeToAttestationSchema()
.getJsonTypeDefinition();

final SerializableTypeDefinition<List<Attestation>> attestationsListTypeDefinition =
SerializableTypeDefinition.listOf(attestationTypeDefinition);

if (attestations.isEmpty()) {
return Collections.emptyList();
}
return postJson(
SEND_SIGNED_ATTESTATION_V2,
emptyMap(),
emptyMap(),
Map.of(HEADER_CONSENSUS_VERSION, specMilestone.name().toLowerCase(Locale.ROOT)),
attestations,
attestationsListTypeDefinition,
new ResponseHandler<>());
SEND_SIGNED_ATTESTATION_V2,
emptyMap(),
emptyMap(),
Map.of(HEADER_CONSENSUS_VERSION, specMilestone.name().toLowerCase(Locale.ROOT)),
attestations,
listOf(getTypeDefinition(attestations)),
getFailureListResponseResponseHandler())
.map(FailureListResponse::failures)
.orElse(Collections.emptyList());
}

@SuppressWarnings("unchecked")
private DeserializableTypeDefinition<Attestation> getTypeDefinition(
final List<Attestation> attestations) {
return (DeserializableTypeDefinition<Attestation>)
attestations.getFirst().getSchema().getJsonTypeDefinition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
Expand Down Expand Up @@ -471,23 +470,20 @@ public void createAttestationData_WhenFound_ReturnsAttestation() {
@Test
public void postAttestations_ShouldUseV1ApiPreElectra() {
final List<Attestation> attestations =
List.of(dataStructureUtil.randomAttestation(), dataStructureUtil.randomAttestation());
List.of(dataStructureUtil.randomAttestation(), dataStructureUtil.randomAttestation());

ignoreFuture(apiHandler.sendSignedAttestations(attestations));
asyncRunner.executeQueuedActions();

verify(typeDefClient, never()).postSignedAttestations(anyList(), any());
verify(typeDefClient)
.sendSignedAttestations(attestations);
verify(typeDefClient).sendSignedAttestations(attestations);
}

@Test
public void postAttestations_ShouldUseV2ApiPostElectra() {
final Spec electraSpec = TestSpecFactory.createMainnetElectra();

apiHandler =
new RemoteValidatorApiHandler(
endpoint, electraSpec, typeDefClient, asyncRunner, true);
new RemoteValidatorApiHandler(endpoint, electraSpec, typeDefClient, asyncRunner, true);
final List<Attestation> attestations =
List.of(dataStructureUtil.randomAttestation(), dataStructureUtil.randomAttestation());

Expand All @@ -499,22 +495,6 @@ public void postAttestations_ShouldUseV2ApiPostElectra() {
.postSignedAttestations(attestations, electraSpec.getGenesisSpec().getMilestone());
}

@Test
public void createAggregate_ShouldUseV2ApiPostElectra() {
apiHandler =
new RemoteValidatorApiHandler(
endpoint, spec, typeDefClient, asyncRunner, true);
final List<Attestation> attestations =
List.of(dataStructureUtil.randomAttestation(), dataStructureUtil.randomAttestation());

ignoreFuture(apiHandler.sendSignedAttestations(attestations));
asyncRunner.executeQueuedActions();

verify(typeDefClient, never()).createAggregate(any(), any());
verify(typeDefClient)
.postSignedAttestations(attestations, SpecMilestone.ELECTRA);
}

@Test
public void createUnsignedBlock_WhenNoneFound_ReturnsEmpty() {
final BLSSignature blsSignature = dataStructureUtil.randomSignature();
Expand Down

0 comments on commit b0ae3eb

Please sign in to comment.