Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ssz publishBlock sent without milestone header #8117

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.http.RestApiConstants.HEADER_CONSENSUS_VERSION;
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;
Expand All @@ -35,6 +36,7 @@
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
Expand Down Expand Up @@ -146,6 +148,8 @@ void publishesBlindedBlockSszEncoded() throws InterruptedException {
final RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertThat(recordedRequest.getBody().readByteArray())
.isEqualTo(signedBeaconBlock.sszSerialize().toArrayUnsafe());
assertThat(recordedRequest.getHeader(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
}

@TestTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ protected <T, TObject> Optional<T> postJson(
protected <T> Optional<T> postOctetStream(
final ValidatorApiMethod apiMethod,
final Map<String, String> urlParams,
final Map<String, String> headers,
final byte[] objectBytes,
final ResponseHandler<T> responseHandler) {
final HttpUrl.Builder httpUrlBuilder = urlBuilder(apiMethod, urlParams);
final Request.Builder builder = requestBuilder();
headers.forEach(builder::addHeader);
final Request request =
requestBuilder()
builder
.url(httpUrlBuilder.build())
.post(RequestBody.create(objectBytes, OCTET_STREAM))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private void sendValidatorRegistrationsAsSsz(
postOctetStream(
ValidatorApiMethod.REGISTER_VALIDATOR,
Collections.emptyMap(),
Collections.emptyMap(),
ApiSchemas.SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA
.sszSerialize(validatorRegistrations)
.toArray(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
package tech.pegasys.teku.validator.remote.typedef.handlers;

import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_UNSUPPORTED_MEDIA_TYPE;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_CONSENSUS_VERSION;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.SEND_SIGNED_BLINDED_BLOCK;
import static tech.pegasys.teku.validator.remote.apiclient.ValidatorApiMethod.SEND_SIGNED_BLOCK;

import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import okhttp3.HttpUrl;
Expand All @@ -26,6 +29,7 @@
import okhttp3.Response;
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;
import tech.pegasys.teku.validator.api.SendSignedBlockResult;
Expand Down Expand Up @@ -73,18 +77,23 @@ private SendSignedBlockResult sendSignedBlockAsSszOrFallback(
final ValidatorApiMethod apiMethod,
final SignedBlockContainer signedBlockContainer,
final DeserializableTypeDefinition<SignedBlockContainer> typeDefinition) {
final SendSignedBlockResult result = sendSignedBlockAsSsz(apiMethod, signedBlockContainer);
final SpecMilestone milestone = spec.atSlot(signedBlockContainer.getSlot()).getMilestone();
final SendSignedBlockResult result =
sendSignedBlockAsSsz(apiMethod, signedBlockContainer, milestone);
if (!result.isPublished() && !preferSszBlockEncoding.get()) {
return sendSignedBlockAsJson(apiMethod, signedBlockContainer, typeDefinition);
}
return result;
}

private SendSignedBlockResult sendSignedBlockAsSsz(
final ValidatorApiMethod apiMethod, final SignedBlockContainer signedBlockContainer) {
final ValidatorApiMethod apiMethod,
final SignedBlockContainer signedBlockContainer,
final SpecMilestone milestone) {
return postOctetStream(
apiMethod,
Collections.emptyMap(),
Map.of(HEADER_CONSENSUS_VERSION, milestone.name().toLowerCase(Locale.ROOT)),
signedBlockContainer.sszSerialize().toArray(),
sszResponseHandler)
.map(__ -> SendSignedBlockResult.success(signedBlockContainer.getRoot()))
Expand Down