Skip to content

Commit

Permalink
Add HP service
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed May 29, 2023
1 parent 0fce3d8 commit 73083f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
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":
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)

16 changes: 14 additions & 2 deletions waku/v2/node/waku_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import
libp2p/protocols/ping,
libp2p/protocols/pubsub/gossipsub,
libp2p/protocols/pubsub/rpc/messages,
libp2p/protocols/connectivity/autonat/client,
libp2p/protocols/connectivity/autonat/client as aclient,
libp2p/protocols/connectivity/relay/relay,
libp2p/protocols/connectivity/autonat/service,
libp2p/protocols/connectivity/relay/client as rclient,
libp2p/services/autorelayservice,
libp2p/services/hpservice,
libp2p/nameresolving/nameresolver,
libp2p/builders,
libp2p/transports/tcptransport,
Expand All @@ -42,6 +46,8 @@ import
./peer_manager,
./waku_switch

export chronos

when defined(rln):
import
../waku_rln_relay
Expand Down Expand Up @@ -172,6 +178,11 @@ proc new*(T: type WakuNode,

info "Initializing networking", addrs= $netConfig.announcedAddresses

let relayClient = RelayClient.new()
let autoRelayService = AutoRelayService.new(1, relayClient, nil, rng)
let autonatService = getAutonatService(rng)
let hpservice = HPService.new(autonatService, autoRelayService)

let switch = newWakuSwitch(
some(nodekey),
address = netConfig.hostAddress,
Expand All @@ -186,7 +197,8 @@ proc new*(T: type WakuNode,
sendSignedPeerRecord = sendSignedPeerRecord,
agentString = agentString,
peerStoreCapacity = peerStoreCapacity,
services = @[Service(getAutonatService(rng))],
services = @[Service(hpservice)],
relay = relayClient,
)

let nodeEnrRes = getEnr(netConfig, wakuDiscv5, nodekey)
Expand Down
9 changes: 7 additions & 2 deletions waku/v2/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/nameresolving/nameresolver,
Expand Down Expand Up @@ -78,7 +79,7 @@ proc newWakuSwitch*(
agentString = none(string), # defaults to nim-libp2p version
peerStoreCapacity = none(int), # defaults to 1.25 maxConnections
services: seq[switch.Service] = @[],
): Switch
relay: Relay = nil): Switch
{.raises: [Defect, IOError, LPError].} =

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

if relay != nil:
b = b.withCircuitRelay(relay)
else:
b = b.withCircuitRelay()

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

0 comments on commit 73083f8

Please sign in to comment.