Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jun 24, 2024
1 parent cd049cd commit de8691e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.ethereum.beacon.discovery.liveness.LivenessChecker.Pinger;
import org.ethereum.beacon.discovery.message.handler.DefaultExternalAddressSelector;
import org.ethereum.beacon.discovery.message.handler.ExternalAddressSelector;
import org.ethereum.beacon.discovery.network.DiscoveryServer;
import org.ethereum.beacon.discovery.network.NettyDiscoveryServer;
import org.ethereum.beacon.discovery.network.NettyDiscoveryServerImpl;
import org.ethereum.beacon.discovery.scheduler.ExpirationSchedulerFactory;
Expand Down Expand Up @@ -87,17 +86,7 @@ public DiscoverySystemBuilder listen(final InetSocketAddress... listenAddresses)
Preconditions.checkArgument(
listenAddresses.length == 1 || listenAddresses.length == 2,
"Can define only 1 or 2 listen addresses - IPv4/IPv6 or IPv4 and IPv6");
if (listenAddresses.length == 2) {
final Set<InternetProtocolFamily> ipFamilies =
Arrays.stream(listenAddresses)
.map(InetSocketAddress::getAddress)
.map(InternetProtocolFamily::of)
.collect(Collectors.toSet());
if (ipFamilies.size() != 2) {
throw new IllegalArgumentException(
String.format("Expected an IPv4 and an IPv6 address but only %s was set", ipFamilies));
}
}
validateListenAddresses(Arrays.stream(listenAddresses).collect(Collectors.toList()));
this.listenAddresses = Optional.of(Arrays.asList(listenAddresses));
return this;
}
Expand Down Expand Up @@ -175,18 +164,10 @@ public DiscoverySystemBuilder discoveryServers(final NettyDiscoveryServer... dis
Preconditions.checkArgument(
discoveryServers.length == 1 || discoveryServers.length == 2,
"Can define only 1 or 2 discovery servers - IPv4/IPv6 or IPv4 and IPv6");
if (discoveryServers.length == 2) {
final Set<InternetProtocolFamily> ipFamilies =
Arrays.stream(discoveryServers)
.map(DiscoveryServer::getListenAddress)
.map(InetSocketAddress::getAddress)
.map(InternetProtocolFamily::of)
.collect(Collectors.toSet());
if (ipFamilies.size() != 2) {
throw new IllegalArgumentException(
String.format("Expected an IPv4 and an IPv6 address but only %s was set", ipFamilies));
}
}
validateListenAddresses(
Arrays.stream(discoveryServers)
.map(NettyDiscoveryServer::getListenAddress)
.collect(Collectors.toList()));
this.discoveryServers = Arrays.asList(discoveryServers);
return this;
}
Expand All @@ -202,6 +183,20 @@ public DiscoverySystemBuilder addressAccessPolicy(final AddressAccessPolicy addr
return this;
}

private void validateListenAddresses(final List<InetSocketAddress> listenAddresses) {
if (listenAddresses.size() == 2) {
final Set<InternetProtocolFamily> ipFamilies =
listenAddresses.stream()
.map(InetSocketAddress::getAddress)
.map(InternetProtocolFamily::of)
.collect(Collectors.toSet());
if (ipFamilies.size() != 2) {
throw new IllegalArgumentException(
String.format("Expected an IPv4 and an IPv6 address but only %s was set", ipFamilies));
}
}
}

private void createDefaults() {
newAddressHandler =
requireNonNullElseGet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* should be in request field and stores it in {@link Field#SESSION} field.
*/
public class NodeSessionManager implements EnvelopeHandler {

private static final int SESSION_CLEANUP_DELAY_SECONDS = 180;
private static final int REQUEST_CLEANUP_DELAY_SECONDS = 60;
private static final Logger LOG = LogManager.getLogger(NodeSessionManager.class);
Expand Down Expand Up @@ -172,8 +171,8 @@ private Optional<InetSocketAddress> getRemoteSocketAddress(final Envelope envelo
.or(
() -> {
final NodeRecord nodeRecord = envelope.get(Field.NODE);
final NodeRecord localNodeRecord = localNodeRecordStore.getLocalNodeRecord();
if (localNodeRecord.getUdp6Address().isPresent()) {
final NodeRecord homeNodeRecord = localNodeRecordStore.getLocalNodeRecord();
if (homeNodeRecord.getUdp6Address().isPresent()) {
// use IPv6
return nodeRecord.getUdp6Address();
} else {
Expand All @@ -190,7 +189,6 @@ public Stream<NodeRecord> streamActiveSessions() {
}

private static class SessionKey {

private final Bytes nodeId;
private final InetSocketAddress remoteSocketAddress;

Expand Down

0 comments on commit de8691e

Please sign in to comment.