Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Discovery not taking nat manager port-mapping into account #6578

Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b982c35
fix: Discovery not taking nat-manager portmapping into account
daanporon Feb 15, 2024
dbdc98d
fix: only take the udp port from the PingPacketData if also the host …
daanporon Feb 15, 2024
c2c87cc
fix imports + fix mapping and fix spotless
daanporon Feb 15, 2024
e6c3eee
fix returned discoveryPort which will be needed for port forwarding
daanporon Feb 21, 2024
5c37483
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
daanporon Feb 21, 2024
d2f5202
run spottles apply
daanporon Feb 21, 2024
307805f
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
daanporon Feb 23, 2024
61e2bb8
try to fix the unitTests, maybe externalPort is 0
daanporon Feb 23, 2024
b2f5225
try to fix the unitTests, maybe externalPort is 0
daanporon Feb 23, 2024
b729ca8
try this to see what is wrong
daanporon Feb 23, 2024
a7739e6
run spottless
daanporon Feb 23, 2024
8abe8d8
don't use NAT manager else docker will be used
daanporon Feb 23, 2024
ad6c9d7
just to make sure it didn't advertise 0
daanporon Feb 23, 2024
3145717
add extra checks just to make sure we are not giving the discovery po…
daanporon Feb 23, 2024
20e387e
just to check what it would do
daanporon Feb 23, 2024
cb8c083
add nat method again to avoid confusion + spotless
daanporon Feb 23, 2024
ad23606
Update ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/d…
daanporon Mar 18, 2024
767a4ef
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
daanporon Mar 18, 2024
d308000
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
macfarla Mar 21, 2024
318e653
Merge branch 'hyperledger:main' into fix/discovery-not-taking-nat-man…
daanporon Apr 11, 2024
6664648
Merge branch 'hyperledger:main' into fix/discovery-not-taking-nat-man…
daanporon Apr 25, 2024
aa5089a
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
daanporon Apr 30, 2024
5befd82
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
macfarla May 16, 2024
c7cc1a5
Merge branch 'hyperledger:main' into fix/discovery-not-taking-nat-man…
daanporon May 22, 2024
348ff4a
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
jflo Jun 4, 2024
7d0744d
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
jframe Jun 10, 2024
7461be0
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
macfarla Jul 23, 2024
3d44664
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
pinges Aug 20, 2024
784dbad
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
pinges Aug 20, 2024
c168d1c
Merge remote-tracking branch 'origin/main' into fix/discovery-not-tak…
daanporon Oct 3, 2024
f6574e6
Merge branch 'main' into fix/discovery-not-taking-nat-manager--portma…
jflo Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() {
.p2pAdvertisedHost(p2pAdvertisedHost)
.p2pEnabled(true)
.discovery(false)
.natMethod(NatMethod.NONE)
.besuController(besuController)
.ethNetworkConfig(mock(EthNetworkConfig.class))
.metricsSystem(mock(ObservableMetricsSystem.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import org.hyperledger.besu.ethereum.p2p.rlpx.RlpxAgent;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.nat.NatService;
import org.hyperledger.besu.nat.core.domain.NatPortMapping;
import org.hyperledger.besu.nat.core.domain.NatServiceType;
import org.hyperledger.besu.nat.core.domain.NetworkProtocol;
import org.hyperledger.besu.plugin.data.EnodeURL;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.util.NetworkUtility;
Expand Down Expand Up @@ -160,21 +163,30 @@ public CompletableFuture<Integer> start(final int tcpPort) {
.thenApply(
(InetSocketAddress localAddress) -> {
// Once listener is set up, finish initializing
final int discoveryPort = localAddress.getPort();
final int localDiscoveryPort = localAddress.getPort();
final int externalDiscoveryPort =
localDiscoveryPort != 0
? natService
.getPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.UDP)
.map(NatPortMapping::getExternalPort)
.filter((externalPort) -> externalPort != 0)
.orElse(localDiscoveryPort)
: localDiscoveryPort;

final DiscoveryPeer ourNode =
DiscoveryPeer.fromEnode(
EnodeURLImpl.builder()
.nodeId(id)
.ipAddress(advertisedAddress)
.listeningPort(tcpPort)
.discoveryPort(discoveryPort)
.discoveryPort(externalDiscoveryPort)
.build());
this.localNode = Optional.of(ourNode);
isActive = true;
daanporon marked this conversation as resolved.
Show resolved Hide resolved
LOG.info("P2P peer discovery agent started and listening on {}", localAddress);
updateNodeRecord();
startController(ourNode);
return discoveryPort;
return localDiscoveryPort;
});
} else {
this.isActive = false;
Expand Down Expand Up @@ -278,16 +290,24 @@ protected boolean validatePacketSize(final int packetSize) {
}

protected void handleIncomingPacket(final Endpoint sourceEndpoint, final Packet packet) {
final int udpPort = sourceEndpoint.getUdpPort();
final String host = deriveHost(sourceEndpoint, packet);

final int udpPort =
packet
.getPacketData(PingPacketData.class)
.flatMap(PingPacketData::getFrom)
.filter(endpoint -> endpoint.getHost().equals(host))
.map(Endpoint::getUdpPort)
.filter((packetDataUdpPort) -> packetDataUdpPort != 0)
.orElseGet(sourceEndpoint::getUdpPort);

final int tcpPort =
packet
.getPacketData(PingPacketData.class)
.flatMap(PingPacketData::getFrom)
.flatMap(Endpoint::getTcpPort)
.orElse(udpPort);

final String host = deriveHost(sourceEndpoint, packet);

// Notify the peer controller.
final DiscoveryPeer peer =
DiscoveryPeer.fromEnode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.hyperledger.besu.nat.NatMethod;
import org.hyperledger.besu.nat.NatService;
import org.hyperledger.besu.nat.core.NatManager;
import org.hyperledger.besu.nat.core.domain.NatPortMapping;
import org.hyperledger.besu.nat.core.domain.NatServiceType;
import org.hyperledger.besu.nat.core.domain.NetworkProtocol;
import org.hyperledger.besu.nat.upnp.UpnpNatManager;
Expand Down Expand Up @@ -469,13 +470,21 @@ private void setLocalNode(

// override advertised host if we detect an external IP address via NAT manager
final String advertisedAddress = natService.queryExternalIPAddress(address);
final int advertisedDiscoveryPort =
discoveryPort != 0
? natService
.getPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.UDP)
.map(NatPortMapping::getExternalPort)
.filter((externalPort) -> externalPort != 0)
.orElse(discoveryPort)
: discoveryPort;

final EnodeURL localEnode =
EnodeURLImpl.builder()
.nodeId(nodeId)
.ipAddress(advertisedAddress)
.listeningPort(listeningPort)
.discoveryPort(discoveryPort)
.discoveryPort(advertisedDiscoveryPort)
.build();

LOG.info("Enode URL {}", localEnode.toString());
Expand Down
Loading