Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jul 1, 2024
1 parent 1b8b456 commit 6656390
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,13 +87,7 @@ public DiscV5Service(
this.currentSchemaDefinitionsSupplier = currentSchemaDefinitionsSupplier;
this.nodeRecordConverter = nodeRecordConverter;
final List<String> 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(
Expand All @@ -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);
Expand All @@ -118,16 +113,7 @@ public DiscV5Service(
new NodeRecordBuilder().secretKey(localNodePrivateKey).seq(seqNo);
if (p2pConfig.hasUserExplicitlySetAdvertisedIps()) {
final List<String> 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);
Expand All @@ -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();
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,16 +108,7 @@ public P2PNetwork<Peer> build() {

final List<Multiaddr> advertisedAddresses;
final List<String> 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(
Expand All @@ -132,12 +122,17 @@ public P2PNetwork<Peer> 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<Integer> listenPorts =
advertisedAddresses.size() == 2
config.isDualStackConfigured()
? List.of(config.getListenPort(), config.getListenPortIpv6())
: List.of(config.getListenPort());

Expand Down Expand Up @@ -181,16 +176,7 @@ protected PeerManager createPeerManager() {
protected Host createHost(final PrivKey privKey, final List<Multiaddr> advertisedAddresses) {
final String[] listenAddrs;
final List<String> 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(
Expand All @@ -206,6 +192,11 @@ protected Host createHost(final PrivKey privKey, final List<Multiaddr> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public List<String> getNetworkInterfaces() {
return networkInterfaces;
}

public boolean isDualStackConfigured() {
return networkInterfaces.size() == 2;
}

public List<String> getAdvertisedIps() {
return advertisedIps.orElse(networkInterfaces).stream()
.map(this::resolveAnyLocalAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 6656390

Please sign in to comment.