Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -38,6 +38,7 @@
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.networking.eth2.peers.SyncSource;
import tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BlocksByRangeResponseInvalidResponseException;
import tech.pegasys.teku.networking.eth2.rpc.core.RpcException.MalformedDataException;
import tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException;
import tech.pegasys.teku.networking.p2p.rpc.RpcResponseListener;
import tech.pegasys.teku.spec.Spec;
Expand Down Expand Up @@ -328,6 +329,9 @@ private void handleRequestErrors(final Throwable error) {
} else if (rootCause instanceof BlocksByRangeResponseInvalidResponseException) {
LOG.debug("Inconsistent blocks returned from blocks by range request", error);
markAsInvalid();
} else if (rootCause instanceof MalformedDataException) {
LOG.debug("Malformed response received while requesting batch data", error);
markAsInvalid();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now any MalformedDataException causes up to 12 hours peer ban which includes for example MessageTruncatedException. I'd carefully review list affected exception and also add JavaDoc to markAsInvalid() so everyone knows which kind of penalty is it.

} else {
LOG.debug("Error while requesting blocks", error);
currentSyncSource = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static tech.pegasys.teku.beacon.sync.forward.multipeer.batches.BatchAssert.assertThatBatch;
import static tech.pegasys.teku.beacon.sync.forward.multipeer.chains.TargetChainTestUtil.chainWith;
import static tech.pegasys.teku.networking.eth2.rpc.core.RpcResponseStatus.SERVER_ERROR_CODE;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -44,6 +46,8 @@
import tech.pegasys.teku.networking.eth2.peers.StubSyncSource;
import tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BlocksByRangeResponseInvalidResponseException;
import tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BlocksByRangeResponseInvalidResponseException.InvalidResponseType;
import tech.pegasys.teku.networking.eth2.rpc.core.RpcException;
import tech.pegasys.teku.networking.eth2.rpc.core.RpcException.DeserializationFailedException;
import tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
Expand Down Expand Up @@ -209,6 +213,34 @@ void shouldBeInvalidWhenInconsistentResponseReceived() {
assertThatBatch(batch).isNotComplete();
}

@Test
void shouldBeInvalidWhenMalformedResponseReceived() {
final Runnable callback = mock(Runnable.class);
final Batch batch = createBatch(10, 10);
batch.requestMoreBlocks(callback);

requestError(batch, new DeserializationFailedException());

verify(conflictResolutionStrategy).reportInvalidBatch(batch, getSyncSource(batch));
verify(callback).run();
assertThatBatch(batch).isEmpty();
assertThatBatch(batch).isNotComplete();
}

@Test
void shouldNotBeInvalidWhenPeerRespondsWithError() {
final Runnable callback = mock(Runnable.class);
final Batch batch = createBatch(10, 10);
batch.requestMoreBlocks(callback);

requestError(batch, new RpcException(SERVER_ERROR_CODE, "peer failed"));

verify(conflictResolutionStrategy, never()).reportInvalidBatch(any(), any());
verify(callback).run();
assertThatBatch(batch).isEmpty();
assertThatBatch(batch).isNotComplete();
}

@Test
void shouldReportAsInvalidToConflictResolutionStrategyWhenMarkedAsInvalid() {
final Batch batch = createBatch(10, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
package tech.pegasys.teku.spec.datastructures.blocks;

import it.unimi.dsi.fastutil.longs.LongList;
import java.util.Optional;
import java.util.OptionalLong;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema2;
import tech.pegasys.teku.infrastructure.ssz.schema.SszNetworkValidator;
import tech.pegasys.teku.infrastructure.ssz.tree.GIndexUtil;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
Expand All @@ -27,21 +29,24 @@ public class SignedBeaconBlockSchema
implements SignedBlockContainerSchema<SignedBeaconBlock> {

private final OptionalLong networkSszLengthBytesUpperBound;
private final Optional<SszNetworkValidator<SignedBeaconBlock>> networkSszValidator;

public SignedBeaconBlockSchema(
final BeaconBlockSchema beaconBlockSchema, final String containerName) {
this(beaconBlockSchema, containerName, OptionalLong.empty());
this(beaconBlockSchema, containerName, OptionalLong.empty(), Optional.empty());
}

public SignedBeaconBlockSchema(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add Javadocs to different constructors, something like validation required starting from Gloas or smth like that

final BeaconBlockSchema beaconBlockSchema,
final String containerName,
final OptionalLong networkSszLengthBytesUpperBound) {
final OptionalLong networkSszLengthBytesUpperBound,
final Optional<SszNetworkValidator<SignedBeaconBlock>> networkSszValidator) {
super(
containerName,
namedSchema(SignedBeaconBlockFields.MESSAGE, beaconBlockSchema),
namedSchema(SignedBeaconBlockFields.SIGNATURE, SszSignatureSchema.INSTANCE));
this.networkSszLengthBytesUpperBound = networkSszLengthBytesUpperBound;
this.networkSszValidator = networkSszValidator;
validateNetworkSszLengthBytesUpperBound();
}

Expand All @@ -50,6 +55,11 @@ public OptionalLong getNetworkSszLengthBytesUpperBound() {
return networkSszLengthBytesUpperBound;
}

@Override
public Optional<SszNetworkValidator<SignedBeaconBlock>> getNetworkSszValidator() {
return networkSszValidator;
}

public SignedBeaconBlock create(final BeaconBlock message, final BLSSignature signature) {
return new SignedBeaconBlock(this, message, signature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.EXECUTION_PAYLOAD_ENVELOPE_SCHEMA;

import java.util.Optional;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema2;
import tech.pegasys.teku.infrastructure.ssz.schema.SszNetworkValidator;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;
Expand All @@ -26,11 +28,21 @@ public class SignedExecutionPayloadEnvelopeSchema
extends ContainerSchema2<
SignedExecutionPayloadEnvelope, ExecutionPayloadEnvelope, SszSignature> {

public SignedExecutionPayloadEnvelopeSchema(final SchemaRegistry schemaRegistry) {
private final Optional<SszNetworkValidator<SignedExecutionPayloadEnvelope>> networkSszValidator;

public SignedExecutionPayloadEnvelopeSchema(
final SchemaRegistry schemaRegistry,
final Optional<SszNetworkValidator<SignedExecutionPayloadEnvelope>> networkSszValidator) {
super(
"SignedExecutionPayloadEnvelope",
namedSchema("message", schemaRegistry.get(EXECUTION_PAYLOAD_ENVELOPE_SCHEMA)),
namedSchema("signature", SszSignatureSchema.INSTANCE));
this.networkSszValidator = networkSszValidator;
}

@Override
public Optional<SszNetworkValidator<SignedExecutionPayloadEnvelope>> getNetworkSszValidator() {
return networkSszValidator;
}

public SignedExecutionPayloadEnvelope create(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* Copyright Consensys Software Inc., 2026
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.util;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.ssz.schema.SszNetworkValidator;
import tech.pegasys.teku.infrastructure.ssz.sos.SszDeserializeException;
import tech.pegasys.teku.spec.config.SpecConfigGloas;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.gloas.BeaconBlockBodyGloas;
import tech.pegasys.teku.spec.datastructures.epbs.versions.gloas.ExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.epbs.versions.gloas.SignedExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload;
import tech.pegasys.teku.spec.datastructures.execution.versions.capella.ExecutionPayloadCapella;
import tech.pegasys.teku.spec.datastructures.execution.versions.gloas.ExecutionRequestsGloas;

/**
* Count limits that Gloas no longer expresses in SSZ (EIP-7688 progressive lists) and that the spec
* allows to be enforced when deserializing network messages. Shared by the network decoders
* (through the wire-level schema validators) and by the gossip validators, which also see locally
* published messages that never crossed the wire.
*/
public final class GloasNetworkLimits {

/** Subject used in violation messages for a block's {@code parent_execution_requests}. */
public static final String PARENT_EXECUTION_REQUESTS_SUBJECT = "Parent execution requests";

/** Subject used in violation messages for an execution payload envelope. */
public static final String EXECUTION_PAYLOAD_ENVELOPE_SUBJECT = "Execution payload envelope";

private GloasNetworkLimits() {}

/**
* Validator for blocks decoded from the network: spec {@code verify_block_body_operation_limits}
* and {@code verify_execution_requests_limits} on the parent execution requests.
*/
public static SszNetworkValidator<SignedBeaconBlock> signedBeaconBlockNetworkValidator(
final SpecConfigGloas config) {
return block -> {
final BeaconBlockBodyGloas body = BeaconBlockBodyGloas.required(block.getMessage().getBody());
verifyBlockBodyOperationLimits(body, config)
.or(
() ->
verifyExecutionRequestsLimits(
PARENT_EXECUTION_REQUESTS_SUBJECT,
ExecutionRequestsGloas.required(body.getParentExecutionRequests()),
config))
.ifPresent(GloasNetworkLimits::rejectDecoded);
};
}

/**
* Validator for execution payload envelopes decoded from the network: the execution request and
* withdrawal count limits of {@code validate_execution_payload_envelope_gossip}.
*/
public static SszNetworkValidator<SignedExecutionPayloadEnvelope>
signedExecutionPayloadEnvelopeNetworkValidator(final SpecConfigGloas config) {
return signedEnvelope -> {
final ExecutionPayloadEnvelope envelope = signedEnvelope.getMessage();
verifyExecutionRequestsLimits(
EXECUTION_PAYLOAD_ENVELOPE_SUBJECT,
ExecutionRequestsGloas.required(envelope.getExecutionRequests()),
config)
.or(
() ->
verifyWithdrawalsLimit(
EXECUTION_PAYLOAD_ENVELOPE_SUBJECT, envelope.getPayload(), config))
.ifPresent(GloasNetworkLimits::rejectDecoded);
};
}

private static void rejectDecoded(final LimitViolation violation) {
throw new SszDeserializeException(violation.describe());
}

/** The first count that exceeds its limit. A zero limit means the list must be empty. */
public record LimitViolation(String subject, String description, int count, int limit) {
public String describe() {
if (limit == 0) {
return String.format("%s must not contain %s, found %d", subject, description, count);
}
return String.format("%s has %d %s, max allowed %d", subject, count, description, limit);
}
}

/** Spec {@code verify_block_body_operation_limits}. */
public static Optional<LimitViolation> verifyBlockBodyOperationLimits(
final BeaconBlockBodyGloas body, final SpecConfigGloas config) {
final String subject = "Block";
return check(
subject,
"proposer slashings",
body.getProposerSlashings().size(),
config.getMaxProposerSlashings())
.or(
() ->
check(
subject,
"attester slashings",
body.getAttesterSlashings().size(),
config.getMaxAttesterSlashingsElectra()))
.or(
() ->
check(
subject,
"attestations",
body.getAttestations().size(),
config.getMaxAttestationsElectra()))
.or(() -> check(subject, "deposits", body.getDeposits().size(), 0))
.or(
() ->
check(
subject,
"voluntary exits",
body.getVoluntaryExits().size(),
config.getMaxVoluntaryExits()))
.or(
() ->
check(
subject,
"bls to execution changes",
body.getBlsToExecutionChanges().size(),
config.getMaxBlsToExecutionChanges()))
.or(
() ->
check(
subject,
"payload attestations",
body.getPayloadAttestations().size(),
config.getMaxPayloadAttestations()));
}

/** Spec {@code verify_execution_requests_limits}. Deposit requests have no Gloas limit. */
public static Optional<LimitViolation> verifyExecutionRequestsLimits(
final String subject, final ExecutionRequestsGloas requests, final SpecConfigGloas config) {
return check(
subject,
"withdrawal requests",
requests.getWithdrawals().size(),
config.getMaxWithdrawalRequestsPerPayload())
.or(
() ->
check(
subject,
"consolidation requests",
requests.getConsolidations().size(),
config.getMaxConsolidationRequestsPerPayload()))
.or(
() ->
check(
subject,
"builder deposit requests",
requests.getBuilderDeposits().size(),
config.getMaxBuilderDepositRequestsPerPayload()))
.or(
() ->
check(
subject,
"builder exit requests",
requests.getBuilderExits().size(),
config.getMaxBuilderExitRequestsPerPayload()));
}

/**
* The {@code MAX_WITHDRAWALS_PER_PAYLOAD} rule of {@code
* validate_execution_payload_envelope_gossip}.
*/
public static Optional<LimitViolation> verifyWithdrawalsLimit(
final String subject, final ExecutionPayload payload, final SpecConfigGloas config) {
return check(
subject,
"withdrawals",
ExecutionPayloadCapella.required(payload).getWithdrawals().size(),
config.getMaxWithdrawalsPerPayload());
}

private static Optional<LimitViolation> check(
final String subject, final String description, final int count, final int limit) {
return count > limit
? Optional.of(new LimitViolation(subject, description, count, limit))
: Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@

import com.google.common.annotations.VisibleForTesting;
import java.util.HashSet;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Set;
import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema;
Expand Down Expand Up @@ -263,6 +264,7 @@
import tech.pegasys.teku.spec.datastructures.state.versions.gloas.BuilderPendingPaymentSchema;
import tech.pegasys.teku.spec.datastructures.state.versions.gloas.BuilderPendingWithdrawalSchema;
import tech.pegasys.teku.spec.datastructures.state.versions.gloas.PtcWindowSchema;
import tech.pegasys.teku.spec.datastructures.util.GloasNetworkLimits;
import tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SchemaId;

// TODO Error Prone's JavaCase check doesn't yet recognize Java 25 unnamed lambda parameters.
Expand Down Expand Up @@ -693,7 +695,10 @@ private static SchemaProvider<?> createSignedBeaconBlockSchemaProvider() {
new SignedBeaconBlockSchema(
registry.get(BEACON_BLOCK_SCHEMA),
schemaName,
OptionalLong.of(specConfig.getMaxPayloadSize())))
OptionalLong.of(specConfig.getMaxPayloadSize()),
Optional.of(
GloasNetworkLimits.signedBeaconBlockNetworkValidator(
SpecConfigGloas.required(specConfig)))))
.build();
}

Expand Down Expand Up @@ -1374,7 +1379,11 @@ private static SchemaProvider<?> createSignedExecutionPayloadEnvelopeSchemaProvi
.withCreator(
GLOAS,
(registry, specConfig, schemaName) ->
new SignedExecutionPayloadEnvelopeSchema(registry))
new SignedExecutionPayloadEnvelopeSchema(
registry,
Optional.of(
GloasNetworkLimits.signedExecutionPayloadEnvelopeNetworkValidator(
SpecConfigGloas.required(specConfig)))))
.build();
}

Expand Down
Loading
Loading