Skip to content

Commit

Permalink
Using GOSSIP_MAX_SIZE from network spec on LibP2PGossipNetworkBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha committed Dec 9, 2024
1 parent 1ea4f0a commit 373b638
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public Bytes uncompress(
}
}

public Bytes uncompress(final Bytes compressedData, final SszLengthBounds lengthBounds)
throws DecodingException {
return uncompress(compressedData, lengthBounds, lengthBounds.getMaxBytes());
}

public Bytes compress(final Bytes data) {
try {
return Bytes.wrap(Snappy.compress(data.toArrayUnsafe()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@
import tech.pegasys.teku.networking.p2p.gossip.config.GossipScoringConfig;
import tech.pegasys.teku.networking.p2p.gossip.config.GossipTopicScoringConfig;
import tech.pegasys.teku.networking.p2p.libp2p.LibP2PNodeId;
import tech.pegasys.teku.spec.config.NetworkingSpecConfig;

public class LibP2PParamsFactory {

public static final int MAX_SUBSCRIPTIONS_PER_MESSAGE = 200;
public static final int MAX_COMPRESSED_GOSSIP_SIZE = 10 * (1 << 20);

public static GossipParams createGossipParams(final GossipConfig gossipConfig) {
public static GossipParams createGossipParams(
final GossipConfig gossipConfig, final NetworkingSpecConfig networkingSpecConfig) {
final GossipParamsBuilder builder = GossipParams.builder();
addGossipParamsDValues(gossipConfig, builder);
addGossipParamsMiscValues(gossipConfig, builder);
addGossipParamsMaxValues(builder);
addGossipParamsMaxValues(networkingSpecConfig, builder);
return builder.build();
}

Expand All @@ -65,9 +66,10 @@ private static void addGossipParamsDValues(
.DOut(Math.min(gossipConfig.getD() / 2, Math.max(0, gossipConfig.getDLow() - 1)));
}

private static void addGossipParamsMaxValues(final GossipParamsBuilder builder) {
private static void addGossipParamsMaxValues(
final NetworkingSpecConfig networkingSpecConfig, final GossipParamsBuilder builder) {
builder
.maxGossipMessageSize(MAX_COMPRESSED_GOSSIP_SIZE)
.maxGossipMessageSize(networkingSpecConfig.getGossipMaxSize())
.maxPublishedMessages(1000)
.maxTopicsPerPublishedMessage(1)
.maxSubscriptions(MAX_SUBSCRIPTIONS_PER_MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public LibP2PGossipNetwork build() {
validate();
final GossipTopicHandlers topicHandlers = new GossipTopicHandlers();
final Gossip gossip =
createGossip(gossipConfig, logWireGossip, gossipTopicFilter, topicHandlers);
createGossip(
gossipConfig, networkingSpecConfig, logWireGossip, gossipTopicFilter, topicHandlers);
final PubsubPublisherApi publisher = gossip.createPublisher(null, NULL_SEQNO_GENERATOR);

return new LibP2PGossipNetwork(metricsSystem, gossip, publisher, topicHandlers);
Expand All @@ -100,10 +101,11 @@ private void assertNotNull(final String fieldName, final Object fieldValue) {

protected GossipRouter createGossipRouter(
final GossipConfig gossipConfig,
final NetworkingSpecConfig networkingSpecConfig,
final GossipTopicFilter gossipTopicFilter,
final GossipTopicHandlers topicHandlers) {

final GossipParams gossipParams = LibP2PParamsFactory.createGossipParams(gossipConfig);
final GossipParams gossipParams =
LibP2PParamsFactory.createGossipParams(gossipConfig, networkingSpecConfig);
final GossipScoreParams scoreParams =
LibP2PParamsFactory.createGossipScoreParams(gossipConfig.getScoringConfig());

Expand Down Expand Up @@ -145,7 +147,7 @@ protected GossipRouter createGossipRouter(
.map(handler -> handler.prepareMessage(payload, arrivalTimestamp))
.orElse(
defaultMessageFactory.create(
topic, payload, networkingSpecConfig, arrivalTimestamp));
topic, payload, this.networkingSpecConfig, arrivalTimestamp));

return new PreparedPubsubMessage(msg, preparedMessage);
});
Expand All @@ -155,11 +157,13 @@ protected GossipRouter createGossipRouter(

protected Gossip createGossip(
final GossipConfig gossipConfig,
final NetworkingSpecConfig networkingSpecConfig,
final boolean gossipLogsEnabled,
final GossipTopicFilter gossipTopicFilter,
final GossipTopicHandlers topicHandlers) {

final GossipRouter router = createGossipRouter(gossipConfig, gossipTopicFilter, topicHandlers);
final GossipRouter router =
createGossipRouter(gossipConfig, networkingSpecConfig, gossipTopicFilter, topicHandlers);

if (gossipLogsEnabled) {
if (debugGossipHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,42 @@
package tech.pegasys.teku.networking.p2p.libp2p.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import io.libp2p.pubsub.gossip.GossipParams;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.networking.p2p.gossip.config.GossipConfig;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.config.NetworkingSpecConfig;

public class LibP2PParamsFactoryTest {

private final Spec spec = TestSpecFactory.createMinimalPhase0();

@Test
void createGossipParams_checkZeroDsSucceed() {
GossipConfig gossipConfig = GossipConfig.builder().d(0).dLow(0).dHigh(0).build();
final GossipConfig gossipConfig = GossipConfig.builder().d(0).dLow(0).dHigh(0).build();

GossipParams gossipParams = LibP2PParamsFactory.createGossipParams(gossipConfig);
final GossipParams gossipParams =
LibP2PParamsFactory.createGossipParams(gossipConfig, spec.getNetworkingConfig());

assertThat(gossipParams.getDOut()).isEqualTo(0);
}

@Test
public void createGossipParams_setGossipMaxSizeFromNetworkSpecConfig() {
final GossipConfig gossipConfig = GossipConfig.builder().build();
final NetworkingSpecConfig networkingSpecConfig = spy(spec.getNetworkingConfig());
final int expectedGossipMaxSize = networkingSpecConfig.getGossipMaxSize();
reset(networkingSpecConfig);

GossipParams gossipParams =
LibP2PParamsFactory.createGossipParams(gossipConfig, networkingSpecConfig);

assertThat(gossipParams.getMaxGossipMessageSize()).isEqualTo(expectedGossipMaxSize);
verify(networkingSpecConfig).getGossipMaxSize();
}
}

0 comments on commit 373b638

Please sign in to comment.