|
7 | 7 | import static com.google.common.base.Preconditions.checkState; |
8 | 8 |
|
9 | 9 | import com.google.common.annotations.VisibleForTesting; |
| 10 | +import java.net.Inet4Address; |
10 | 11 | import java.net.Inet6Address; |
11 | 12 | import java.net.InetSocketAddress; |
12 | 13 | import java.time.Duration; |
@@ -60,8 +61,8 @@ public void onExternalAddressReport( |
60 | 61 | // Select best address per IP family independently to support dual-stack auto-discovery. |
61 | 62 | // Without per-family selection, the dominant IP family (usually IPv4) always wins |
62 | 63 | // and the other family's ENR fields are never populated. |
63 | | - selectExternalAddress(false).ifPresent(this::maybeUpdateAddress); |
64 | | - selectExternalAddress(true).ifPresent(this::maybeUpdateAddress); |
| 64 | + selectExternalIPV4Address().ifPresent(this::maybeUpdateAddress); |
| 65 | + selectExternalIPV6Address().ifPresent(this::maybeUpdateAddress); |
65 | 66 | } |
66 | 67 |
|
67 | 68 | private void removeStaleAddresses(final Instant now) { |
@@ -108,9 +109,17 @@ private void maybeUpdateAddress(final InetSocketAddress selectedAddress) { |
108 | 109 | } |
109 | 110 | } |
110 | 111 |
|
111 | | - private Optional<InetSocketAddress> selectExternalAddress(final boolean ipv6) { |
| 112 | + private Optional<InetSocketAddress> selectExternalIPV4Address() { |
112 | 113 | return reportedAddresses.entrySet().stream() |
113 | | - .filter(entry -> (entry.getKey().getAddress() instanceof Inet6Address) == ipv6) |
| 114 | + .filter(entry -> (entry.getKey().getAddress() instanceof Inet4Address)) |
| 115 | + .filter(entry -> entry.getValue().getReportCount() >= MIN_CONFIRMATIONS) |
| 116 | + .max(Map.Entry.comparingByValue(Comparator.comparing(ReportData::getReportCount))) |
| 117 | + .map(Map.Entry::getKey); |
| 118 | + } |
| 119 | + |
| 120 | + private Optional<InetSocketAddress> selectExternalIPV6Address() { |
| 121 | + return reportedAddresses.entrySet().stream() |
| 122 | + .filter(entry -> (entry.getKey().getAddress() instanceof Inet6Address)) |
114 | 123 | .filter(entry -> entry.getValue().getReportCount() >= MIN_CONFIRMATIONS) |
115 | 124 | .max(Map.Entry.comparingByValue(Comparator.comparing(ReportData::getReportCount))) |
116 | 125 | .map(Map.Entry::getKey); |
|
0 commit comments