Skip to content

Commit

Permalink
only add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Mar 4, 2025
1 parent 602915c commit 05c6ced
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.state.Fork;
import tech.pegasys.teku.spec.datastructures.state.ForkInfo;
import tech.pegasys.teku.storage.client.RecentChainData;

Expand All @@ -35,14 +34,12 @@ public class Eth2GossipTopicFilter implements GossipTopicFilter {

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

public Eth2GossipTopicFilter(
final RecentChainData recentChainData, final GossipEncoding gossipEncoding, final Spec spec) {
this.spec = spec;
relevantTopics =
Suppliers.memoize(() -> computeRelevantTopics(recentChainData, gossipEncoding));
maxSubscribedTopics = computeMaxSubscribedTopics(recentChainData, gossipEncoding);
}

@Override
Expand All @@ -54,11 +51,6 @@ public boolean isRelevantTopic(final String topic) {
return allowed;
}

@Override
public int getMaxSubscribedTopics() {
return maxSubscribedTopics;
}

private Set<String> computeRelevantTopics(
final RecentChainData recentChainData, final GossipEncoding gossipEncoding) {
final ForkInfo forkInfo = recentChainData.getCurrentForkInfo().orElseThrow();
Expand All @@ -82,17 +74,4 @@ private Set<String> computeRelevantTopics(
});
return topics;
}

private int computeMaxSubscribedTopics(
final RecentChainData recentChainData, final GossipEncoding gossipEncoding) {
final SpecMilestone highestSupportedMilestone =
spec.getForkSchedule().getHighestSupportedMilestone();
final Fork highestSupportedFork = spec.getForkSchedule().getFork(highestSupportedMilestone);
final Bytes4 forkDigest =
spec.computeForkDigest(
highestSupportedFork.getCurrentVersion(),
recentChainData.getGenesisData().orElseThrow().getGenesisValidatorsRoot());
// Enough to subscribe to three forks simultaneously so testnets can fork in subsequent epochs
return getAllTopics(gossipEncoding, forkDigest, spec, highestSupportedMilestone).size() * 3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,6 @@ void shouldNotAllowTopicsWithUnknownForkDigest() {
assertThat(filter.isRelevantTopic(irrelevantTopic)).isFalse();
}

@TestTemplate
void shouldComputeMaxSubscribedTopics() {
switch (nextSpecMilestone) {
case DENEB -> assertThat(filter.getMaxSubscribedTopics()).isEqualTo(243);
case ELECTRA -> assertThat(filter.getMaxSubscribedTopics()).isEqualTo(252);
default -> throw new IllegalArgumentException("Unexpected milestone: " + nextSpecMilestone);
}
}

private String getTopicName(final GossipTopicName name) {
return GossipTopics.getTopic(currentForkInfo.getForkDigest(spec), name, SSZ_SNAPPY);
}
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 @@ -13,8 +13,7 @@

package tech.pegasys.teku.networking.p2p.libp2p.gossip;

@FunctionalInterface
public interface GossipTopicFilter {
boolean isRelevantTopic(String topic);

int getMaxSubscribedTopics();
}
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 @@ -55,6 +56,9 @@
*/
public class LibP2PGossipNetworkBuilder {

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

public static LibP2PGossipNetworkBuilder create() {
return new LibP2PGossipNetworkBuilder();
}
Expand Down Expand Up @@ -109,7 +113,7 @@ protected GossipRouter createGossipRouter(
final TopicSubscriptionFilter subscriptionFilter =
new MaxCountTopicSubscriptionFilter(
MAX_SUBSCRIPTIONS_PER_MESSAGE,
gossipTopicFilter.getMaxSubscribedTopics(),
MAX_SUBSCRIBED_TOPICS,
gossipTopicFilter::isRelevantTopic);

final GossipRouterBuilder builder = new GossipRouterBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import tech.pegasys.teku.networking.p2p.discovery.DiscoveryNetwork;
import tech.pegasys.teku.networking.p2p.discovery.DiscoveryNetworkBuilder;
import tech.pegasys.teku.networking.p2p.libp2p.LibP2PNetworkBuilder;
import tech.pegasys.teku.networking.p2p.libp2p.gossip.GossipTopicFilter;
import tech.pegasys.teku.networking.p2p.network.config.NetworkConfig;
import tech.pegasys.teku.networking.p2p.reputation.DefaultReputationManager;
import tech.pegasys.teku.networking.p2p.reputation.ReputationManager;
Expand Down Expand Up @@ -130,19 +129,7 @@ public DiscoveryNetwork<?> buildAndStart() throws Exception {
(topic, payload, networkingSpecConfig, arrivalTimestamp) -> {
throw new UnsupportedOperationException();
})
.gossipTopicFilter(
new GossipTopicFilter() {
@Override
public boolean isRelevantTopic(final String topic) {
return true;
}

// safe value for testing
@Override
public int getMaxSubscribedTopics() {
return 250;
}
})
.gossipTopicFilter(topic -> true)
.timeProvider(StubTimeProvider.withTimeInMillis(0))
.build())
.peerPools(peerPools)
Expand Down

0 comments on commit 05c6ced

Please sign in to comment.