Skip to content

Commit

Permalink
Add a test to verify MAX_SUBSCRIBED_TOPICS constant (#9163)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov authored Mar 4, 2025
1 parent 690517e commit 8479214
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

public class Eth2GossipTopicFilter implements GossipTopicFilter {
private static final Logger LOG = LogManager.getLogger();
private final Supplier<Set<String>> relevantTopics;

private final Spec spec;
private final Supplier<Set<String>> relevantTopics;

public Eth2GossipTopicFilter(
final RecentChainData recentChainData, final GossipEncoding gossipEncoding, final Spec spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ public class GossipSubValidationUtil {

public static ValidationResult fromInternalValidationResult(
final InternalValidationResult result) {
switch (result.code()) {
case ACCEPT:
return ValidationResult.Valid;
case SAVE_FOR_FUTURE:
case IGNORE:
return ValidationResult.Ignore;
case REJECT:
return ValidationResult.Invalid;
default:
throw new IllegalArgumentException("Unexpected internal validation result: " + result);
}
return switch (result.code()) {
case ACCEPT -> ValidationResult.Valid;
case SAVE_FOR_FUTURE, IGNORE -> ValidationResult.Ignore;
case REJECT -> ValidationResult.Invalid;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Eth2TopicHandler<MessageT extends SszData> implements TopicHandler
private final String topic;
final TimeProvider timeProvider;

// every slot of mainnet config
// every slot of mainnet config
private final Throttler<Logger> loggerThrottler = new Throttler<>(LOG, UInt64.valueOf(12));

public Eth2TopicHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Comparator;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding;
import tech.pegasys.teku.networking.p2p.libp2p.gossip.LibP2PGossipNetworkBuilder;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.state.Fork;
import tech.pegasys.teku.storage.storageSystem.InMemoryStorageSystemBuilder;
import tech.pegasys.teku.storage.storageSystem.StorageSystem;

public class GossipTopicsTest {
private final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
Expand All @@ -44,4 +53,35 @@ public void extractForkDigest_invalid() {
assertThatThrownBy(() -> GossipTopics.extractForkDigest(topic))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void maxSubscribedTopicsConstantIsLargeEnough() {
final SpecMilestone latestMilestone = SpecMilestone.values()[SpecMilestone.values().length - 1];
final Spec spec = TestSpecFactory.createMainnet(latestMilestone);

final StorageSystem storageSystem = InMemoryStorageSystemBuilder.buildDefault(spec);
storageSystem.chainUpdater().initializeGenesis();
final Bytes32 genesisValidatorsRoot =
storageSystem.recentChainData().getGenesisData().orElseThrow().getGenesisValidatorsRoot();

// sum of all topics for highest fork + (fork-1) + (fork-2)
int exactMaxSubscribedTopics =
SpecMilestone.getMilestonesUpTo(latestMilestone).stream()
.sorted(Comparator.reverseOrder())
.limit(3)
.mapToInt(
milestone -> {
final Fork fork = spec.getForkSchedule().getFork(milestone);
final Bytes4 forkDigest =
spec.forMilestone(milestone)
.miscHelpers()
.computeForkDigest(fork.getCurrentVersion(), genesisValidatorsRoot);
return GossipTopics.getAllTopics(gossipEncoding, forkDigest, spec, milestone)
.size();
})
.sum();

assertThat(exactMaxSubscribedTopics)
.isLessThanOrEqualTo(LibP2PGossipNetworkBuilder.MAX_SUBSCRIBED_TOPICS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static tech.pegasys.teku.networking.p2p.libp2p.gossip.LibP2PGossipNetwork.NULL_SEQNO_GENERATOR;
import static tech.pegasys.teku.networking.p2p.libp2p.gossip.LibP2PGossipNetwork.STRICT_FIELDS_VALIDATOR;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import io.libp2p.core.pubsub.PubsubApi;
import io.libp2p.core.pubsub.PubsubApiKt;
Expand Down Expand Up @@ -56,7 +57,7 @@
public class LibP2PGossipNetworkBuilder {

// Enough to subscribe to three forks simultaneously so testnets can fork in subsequent epochs
public static final int MAX_SUBSCRIBED_TOPICS = 250;
@VisibleForTesting public static final int MAX_SUBSCRIBED_TOPICS = 250;

public static LibP2PGossipNetworkBuilder create() {
return new LibP2PGossipNetworkBuilder();
Expand Down

0 comments on commit 8479214

Please sign in to comment.