From 373b638c7770afbd76bcfa3abcea140e3cdf8efe Mon Sep 17 00:00:00 2001 From: Lucas Saldanha Date: Tue, 10 Dec 2024 12:16:45 +1300 Subject: [PATCH] Using GOSSIP_MAX_SIZE from network spec on LibP2PGossipNetworkBuilder --- .../encoding/SnappyBlockCompressor.java | 5 ---- .../libp2p/config/LibP2PParamsFactory.java | 12 +++++---- .../gossip/LibP2PGossipNetworkBuilder.java | 14 ++++++---- .../config/LibP2PParamsFactoryTest.java | 27 +++++++++++++++++-- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/gossip/encoding/SnappyBlockCompressor.java b/networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/gossip/encoding/SnappyBlockCompressor.java index 6d190516190..dd9b2bb384a 100644 --- a/networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/gossip/encoding/SnappyBlockCompressor.java +++ b/networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/gossip/encoding/SnappyBlockCompressor.java @@ -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())); diff --git a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactory.java b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactory.java index c24629cd897..ea3abca876e 100644 --- a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactory.java +++ b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactory.java @@ -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(); } @@ -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) diff --git a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/gossip/LibP2PGossipNetworkBuilder.java b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/gossip/LibP2PGossipNetworkBuilder.java index 38fab38a0c2..d582da6eafe 100644 --- a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/gossip/LibP2PGossipNetworkBuilder.java +++ b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/gossip/LibP2PGossipNetworkBuilder.java @@ -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); @@ -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()); @@ -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); }); @@ -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) { diff --git a/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactoryTest.java b/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactoryTest.java index 191fc456242..1025a8db851 100644 --- a/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactoryTest.java +++ b/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/libp2p/config/LibP2PParamsFactoryTest.java @@ -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(); + } }