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

feat: Enable Hole Punching service #1766

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
4 changes: 3 additions & 1 deletion waku/common/utils/nat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ proc setupNat*(natConf, clientId: string,
endpoint.udpPort = some(extUdpPort)

else: # NatNone
if natConf == "none":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt this redundant? its the else case of if strategy != NatNone: so it will always be none?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but before it executed the code below:

    if not natConf.startsWith("extip:"):
      return err("not a valid NAT mechanism: " & $natConf)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we just return an error in this case (else: # NatNone)?

I have the impression that the next is always run:

return err("not a valid NAT mechanism: " & $natConf)

return ok((none(ValidIpAddress), none(Port), none(Port)))

if not natConf.startsWith("extip:"):
return err("not a valid NAT mechanism: " & $natConf)

Expand All @@ -64,4 +67,3 @@ proc setupNat*(natConf, clientId: string,
return err("not a valid IP address: " & $natConf[6..^1])

return ok(endpoint)

11 changes: 9 additions & 2 deletions waku/node/builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import
libp2p/crypto/crypto,
libp2p/builders,
libp2p/nameresolving/nameresolver,
libp2p/transports/wstransport
libp2p/transports/wstransport,
libp2p/protocols/connectivity/relay/client,
libp2p/services/[autorelayservice, hpservice]
import
../waku_enr,
../waku_discv5,
Expand Down Expand Up @@ -145,6 +147,10 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] =
if builder.record.isNone():
return err("node record is required")

let relayClient = RelayClient.new()
let autoRelayService = AutoRelayService.new(1, relayClient, nil, rng)
let autonatService = getAutonatService(rng)
let hpservice = HPService.new(autonatService, autoRelayService)
var switch: Switch
try:
switch = newWakuSwitch(
Expand All @@ -161,7 +167,8 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] =
sendSignedPeerRecord = builder.switchSendSignedPeerRecord.get(false),
agentString = builder.switchAgentString,
peerStoreCapacity = builder.peerStorageCapacity,
services = @[Service(getAutonatService(rng))],
services = @[Service(hpservice)],
relay = relayClient,
)
except CatchableError:
return err("failed to create switch: " & getCurrentExceptionMsg())
Expand Down
9 changes: 7 additions & 2 deletions waku/node/waku_switch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import
std/[options, math],
chronos, chronicles,
eth/keys,
libp2p/protocols/connectivity/relay/relay,
libp2p/crypto/crypto,
libp2p/protocols/pubsub/gossipsub,
libp2p/protocols/rendezvous,
Expand Down Expand Up @@ -80,7 +81,7 @@ proc newWakuSwitch*(
peerStoreCapacity = none(int), # defaults to 1.25 maxConnections
services: seq[switch.Service] = @[],
rendezvous: RendezVous = nil,
): Switch
relay: Relay = nil): Switch
{.raises: [Defect, IOError, LPError].} =

var b = SwitchBuilder
Expand All @@ -95,9 +96,13 @@ proc newWakuSwitch*(
.withTcpTransport(transportFlags)
.withNameResolver(nameResolver)
.withSignedPeerRecord(sendSignedPeerRecord)
.withCircuitRelay()
.withAutonat()

if relay != nil:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little nitpick:

Suggested change
if relay != nil:
if not isNil(relay):

b = b.withCircuitRelay(relay)
else:
b = b.withCircuitRelay()

if peerStoreCapacity.isSome():
b = b.withPeerStore(peerStoreCapacity.get())
else:
Expand Down
Loading