From 66563906154075724d4e033c2e2b12659a61dea8 Mon Sep 17 00:00:00 2001 From: Stefan Bratanov Date: Mon, 1 Jul 2024 11:52:26 -0700 Subject: [PATCH] Refactor --- .../p2p/discovery/discv5/DiscV5Service.java | 34 +++++++----------- .../p2p/libp2p/LibP2PNetworkBuilder.java | 35 +++++++------------ .../p2p/network/config/NetworkConfig.java | 4 +++ .../services/BeaconNodeServiceController.java | 3 +- 4 files changed, 30 insertions(+), 46 deletions(-) diff --git a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/discovery/discv5/DiscV5Service.java b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/discovery/discv5/DiscV5Service.java index c06fd4cafee..13a3975dc43 100644 --- a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/discovery/discv5/DiscV5Service.java +++ b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/discovery/discv5/DiscV5Service.java @@ -15,7 +15,6 @@ import static java.util.Collections.emptyList; -import com.google.common.base.Preconditions; import java.net.InetSocketAddress; import java.time.Duration; import java.util.ArrayList; @@ -88,13 +87,7 @@ public DiscV5Service( this.currentSchemaDefinitionsSupplier = currentSchemaDefinitionsSupplier; this.nodeRecordConverter = nodeRecordConverter; final List networkInterfaces = p2pConfig.getNetworkInterfaces(); - Preconditions.checkState( - networkInterfaces.size() == 1 || networkInterfaces.size() == 2, - "The configured network interfaces must be either 1 or 2"); - if (networkInterfaces.size() == 1) { - discoverySystemBuilder.listen(networkInterfaces.get(0), discoConfig.getListenUdpPort()); - } else { - // IPv4 and IPv6 (dual-stack) + if (p2pConfig.isDualStackConfigured()) { final InetSocketAddress[] listenAddresses = networkInterfaces.stream() .map( @@ -108,6 +101,8 @@ public DiscV5Service( }) .toArray(InetSocketAddress[]::new); discoverySystemBuilder.listen(listenAddresses); + } else { + discoverySystemBuilder.listen(networkInterfaces.get(0), discoConfig.getListenUdpPort()); } final UInt64 seqNo = kvStore.get(SEQ_NO_STORE_KEY).map(UInt64::fromBytes).orElse(UInt64.ZERO).add(1); @@ -118,16 +113,7 @@ public DiscV5Service( new NodeRecordBuilder().secretKey(localNodePrivateKey).seq(seqNo); if (p2pConfig.hasUserExplicitlySetAdvertisedIps()) { final List advertisedIps = p2pConfig.getAdvertisedIps(); - Preconditions.checkState( - advertisedIps.size() == 1 || advertisedIps.size() == 2, - "The configured advertised IPs must be either 1 or 2"); - if (advertisedIps.size() == 1) { - nodeRecordBuilder.address( - advertisedIps.get(0), - discoConfig.getAdvertisedUdpPort(), - p2pConfig.getAdvertisedPort()); - } else { - // IPv4 and IPv6 (dual-stack) + if (p2pConfig.isDualStackConfigured()) { advertisedIps.forEach( advertisedIp -> { final IPVersion ipVersion = IPVersionResolver.resolve(advertisedIp); @@ -143,6 +129,11 @@ public DiscV5Service( }; nodeRecordBuilder.address(advertisedIp, advertisedUdpPort, advertisedTcpPort); }); + } else { + nodeRecordBuilder.address( + advertisedIps.get(0), + discoConfig.getAdvertisedUdpPort(), + p2pConfig.getAdvertisedPort()); } } final NodeRecord localNodeRecord = nodeRecordBuilder.build(); @@ -172,15 +163,14 @@ private NewAddressHandler maybeUpdateNodeRecord(final NetworkConfig p2pConfig) { } else { return (oldRecord, newAddress) -> { final int newTcpPort; - if (p2pConfig.getNetworkInterfaces().size() == 1) { - newTcpPort = p2pConfig.getAdvertisedPort(); - } else { - // IPv4 and IPv6 (dual-stack) + if (p2pConfig.isDualStackConfigured()) { newTcpPort = switch (IPVersionResolver.resolve(newAddress)) { case IP_V4 -> p2pConfig.getAdvertisedPort(); case IP_V6 -> p2pConfig.getAdvertisedPortIpv6(); }; + } else { + newTcpPort = p2pConfig.getAdvertisedPort(); } return Optional.of( oldRecord.withNewAddress(newAddress, Optional.of(newTcpPort), localNodePrivateKey)); diff --git a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/LibP2PNetworkBuilder.java b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/LibP2PNetworkBuilder.java index 5b809f77d14..b9880ec4c75 100644 --- a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/LibP2PNetworkBuilder.java +++ b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/LibP2PNetworkBuilder.java @@ -16,7 +16,6 @@ import static tech.pegasys.teku.networking.p2p.libp2p.LibP2PNetwork.REMOTE_OPEN_STREAMS_RATE_LIMIT; import static tech.pegasys.teku.networking.p2p.libp2p.LibP2PNetwork.REMOTE_PARALLEL_OPEN_STREAMS_COUNT_LIMIT; -import com.google.common.base.Preconditions; import identify.pb.IdentifyOuterClass; import io.libp2p.core.Host; import io.libp2p.core.PeerId; @@ -109,16 +108,7 @@ public P2PNetwork build() { final List advertisedAddresses; final List advertisedIps = config.getAdvertisedIps(); - Preconditions.checkState( - advertisedIps.size() == 1 || advertisedIps.size() == 2, - "The configured advertised IPs must be either 1 or 2"); - if (advertisedIps.size() == 1) { - advertisedAddresses = - Collections.singletonList( - MultiaddrUtil.fromInetSocketAddress( - new InetSocketAddress(advertisedIps.get(0), config.getAdvertisedPort()), nodeId)); - } else { - // IPv4 and IPv6 (dual-stack) + if (config.isDualStackConfigured()) { advertisedAddresses = advertisedIps.stream() .map( @@ -132,12 +122,17 @@ public P2PNetwork build() { new InetSocketAddress(advertisedIp, advertisedPort), nodeId); }) .toList(); + } else { + advertisedAddresses = + Collections.singletonList( + MultiaddrUtil.fromInetSocketAddress( + new InetSocketAddress(advertisedIps.get(0), config.getAdvertisedPort()), nodeId)); } host = createHost(privKey, advertisedAddresses); final List listenPorts = - advertisedAddresses.size() == 2 + config.isDualStackConfigured() ? List.of(config.getListenPort(), config.getListenPortIpv6()) : List.of(config.getListenPort()); @@ -181,16 +176,7 @@ protected PeerManager createPeerManager() { protected Host createHost(final PrivKey privKey, final List advertisedAddresses) { final String[] listenAddrs; final List networkInterfaces = config.getNetworkInterfaces(); - Preconditions.checkState( - networkInterfaces.size() == 1 || networkInterfaces.size() == 2, - "The configured network interfaces must be either 1 or 2"); - if (networkInterfaces.size() == 1) { - final Multiaddr addr = - MultiaddrUtil.fromInetSocketAddress( - new InetSocketAddress(networkInterfaces.get(0), config.getListenPort())); - listenAddrs = new String[] {addr.toString()}; - } else { - // IPv4 and IPv6 (dual-stack) + if (config.isDualStackConfigured()) { listenAddrs = networkInterfaces.stream() .map( @@ -206,6 +192,11 @@ protected Host createHost(final PrivKey privKey, final List advertise return addr.toString(); }) .toArray(String[]::new); + } else { + final Multiaddr addr = + MultiaddrUtil.fromInetSocketAddress( + new InetSocketAddress(networkInterfaces.get(0), config.getListenPort())); + listenAddrs = new String[] {addr.toString()}; } return BuilderJKt.hostJ( diff --git a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfig.java b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfig.java index 2dce9abd3d5..2fd1541f98f 100644 --- a/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfig.java +++ b/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfig.java @@ -103,6 +103,10 @@ public List getNetworkInterfaces() { return networkInterfaces; } + public boolean isDualStackConfigured() { + return networkInterfaces.size() == 2; + } + public List getAdvertisedIps() { return advertisedIps.orElse(networkInterfaces).stream() .map(this::resolveAnyLocalAddress) diff --git a/teku/src/main/java/tech/pegasys/teku/services/BeaconNodeServiceController.java b/teku/src/main/java/tech/pegasys/teku/services/BeaconNodeServiceController.java index 0e33d4839dc..e28629b55b8 100644 --- a/teku/src/main/java/tech/pegasys/teku/services/BeaconNodeServiceController.java +++ b/teku/src/main/java/tech/pegasys/teku/services/BeaconNodeServiceController.java @@ -64,8 +64,7 @@ public BeaconNodeServiceController( new NatService( tekuConfig.natConfiguration(), networkConfig.getListenPort(), - // // IPv4 and IPv6 (dual-stack) - networkConfig.getNetworkInterfaces().size() == 2 + networkConfig.isDualStackConfigured() ? Optional.of(networkConfig.getListenPortIpv6()) : Optional.empty(), tekuConfig.discovery().isDiscoveryEnabled()));