diff --git a/CHANGELOG.md b/CHANGELOG.md index 0aa7602676f..b0f024696b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,3 +20,5 @@ the [releases page](https://github.com/Consensys/teku/releases). ### Additions and Improvements ### Bug Fixes + +- Fixed an issue from version 24.6.0 where Teku failed to start on machines with directly assigned public IP addresses (not running under NAT), displaying the error message: `Teku failed to start: java.io.UncheckedIOException: java.net.UnknownHostException: Unable to determine local IPvx Address` 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 3185cb36b9c..b58902591be 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 @@ -17,7 +17,6 @@ import static com.google.common.base.Preconditions.checkState; import static com.google.common.net.InetAddresses.isInetAddress; -import java.io.UncheckedIOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; @@ -153,12 +152,18 @@ private String resolveAnyLocalAddress(final String ipAddress) { return ipAddress; } } catch (final UnknownHostException ex) { - throw new UncheckedIOException(ex); + LOG.error("Failed resolving local address: {}. Trying to use {}", ex.getMessage(), ipAddress); + return ipAddress; } } private String getLocalAddress(final IPVersion ipVersion) throws UnknownHostException { try { + final InetAddress localHostAddress = InetAddress.getLocalHost(); + if (localHostAddress.isAnyLocalAddress() + && IPVersionResolver.resolve(localHostAddress) == ipVersion) { + return localHostAddress.getHostAddress(); + } final Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { diff --git a/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfigTest.java b/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfigTest.java index 61783811ff7..fc3509abead 100644 --- a/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfigTest.java +++ b/networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/network/config/NetworkConfigTest.java @@ -19,7 +19,6 @@ import io.libp2p.core.multiformats.Multiaddr; import java.net.Inet6Address; import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; @@ -62,20 +61,12 @@ void getAdvertisedIps_shouldResolveLocalhostIpWhenInterfaceIpIsAnyLocal() { @Test void getAdvertisedIps_shouldResolveLocalhostIpWhenInterfaceIpIsAnyLocalIpv6() { listenIp = "::0"; - final List result; - try { - result = createConfig().getAdvertisedIps(); - } catch (Exception ex) { - // local IPv6 not supported - assertThat(ex.getCause()).isInstanceOf(UnknownHostException.class); - assertThat(ex.getMessage()).contains("Unable to determine local IPv6 Address"); - return; - } + final List result = createConfig().getAdvertisedIps(); assertThat(result) .hasSize(1) .first() - .isNotEqualTo("::0") - .isNotEqualTo("0.0.0.0") + .asString() + .isNotBlank() .satisfies( ip -> { // check the advertised IP is IPv6