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 all 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
30 changes: 14 additions & 16 deletions apps/wakunode2/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import
libp2p/crypto/crypto,
libp2p/nameresolving/dnsresolver,
libp2p/protocols/pubsub/gossipsub,
libp2p/discovery/discoverymngr,
libp2p/peerid,
eth/keys,
json_rpc/rpcserver,
Expand Down Expand Up @@ -81,6 +82,9 @@ type
restServer: Option[RestServerRef]
metricsServer: Option[MetricsHttpServerRef]

topics: seq[string]
dm: DiscoveryManager

AppResult*[T] = Result[T, string]


Expand Down Expand Up @@ -160,7 +164,8 @@ proc init*(T: type App, rng: ref HmacDrbgContext, conf: WakuNodeConf): T =
rng: rng,
key: key,
record: record,
node: nil
node: nil,
topics: topics,
)


Expand Down Expand Up @@ -329,14 +334,16 @@ proc setupWakuApp*(app: var App): AppResult[void] =

## Mount protocols

proc setupProtocols(node: WakuNode,
conf: WakuNodeConf,
nodeKey: crypto.PrivateKey):
Future[AppResult[void]] {.async.} =
proc setupProtocols(app: App): Future[AppResult[void]] {.async.} =
## Setup configured protocols on an existing Waku v2 node.
## Optionally include persistent message storage.
## No protocols are started yet.

let
node = app.node
conf = app.conf
key = app.key

# Mount relay on all nodes
var peerExchangeHandler = none(RoutingRecordsHandler)
if conf.relayPeerExchange:
Expand All @@ -355,14 +362,7 @@ proc setupProtocols(node: WakuNode,
peerExchangeHandler = some(handlePeerExchange)

if conf.relay:
let pubsubTopics =
if conf.pubsubTopics.len > 0 or conf.contentTopics.len > 0:
# TODO autoshard content topics only once.
# Already checked for errors in app.init
let shards = conf.contentTopics.mapIt(getShard(it).expect("Valid Shard"))
conf.pubsubTopics & shards
else:
conf.topics
let pubsubTopics = app.topics

try:
await mountRelay(node, pubsubTopics, peerExchangeHandler = peerExchangeHandler)
Expand Down Expand Up @@ -493,9 +493,7 @@ proc setupProtocols(node: WakuNode,

proc setupAndMountProtocols*(app: App): Future[AppResult[void]] {.async.} =
return await setupProtocols(
app.node,
app.conf,
app.key
app
)

## Start node
Expand Down
8 changes: 7 additions & 1 deletion apps/wakunode2/wakunode2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import
metrics,
libbacktrace,
system/ansi_c,
libp2p/crypto/crypto
libp2p/crypto/crypto,
libp2p/discovery/[rendezvousinterface, discoverymngr]
import
../../waku/common/logging,
./external_config,
Expand Down Expand Up @@ -92,6 +93,11 @@ when isMainModule:
error "5/7 Starting node and protocols failed", error=res6.error
quit(QuitFailure)

wakunode2.dm = DiscoveryManager()
wakunode2.dm.add(RendezVousInterface.new(rdv = wakunode2.node.rendezvous, tta = 1.minutes))
for topic in wakunode2.topics:
wakunode2.dm.advertise(RdvNamespace(topic))

debug "6/7 Starting monitoring and external interfaces"

let res7 = wakunode2.setupMonitoringAndExternalInterfaces()
Expand Down
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
2 changes: 2 additions & 0 deletions waku/node/waku_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import
libp2p/protocols/connectivity/autonat/client,
libp2p/protocols/connectivity/autonat/service,
libp2p/protocols/rendezvous,
libp2p/discovery/discoverymngr,
libp2p/nameresolving/nameresolver,
libp2p/builders,
libp2p/transports/tcptransport,
Expand Down Expand Up @@ -98,6 +99,7 @@ type
libp2pPing*: Ping
rng*: ref rand.HmacDrbgContext
rendezvous*: RendezVous
dm*: DiscoveryManager
announcedAddresses* : seq[MultiAddress]
started*: bool # Indicates that node has started listening
topicSubscriptionQueue*: AsyncEventQueue[SubscriptionEvent]
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