feat(networking): use autonatservice and log if node is reachable (#1472)

This commit is contained in:
Alvaro Revuelta 2023-01-11 10:57:49 +01:00 committed by GitHub
parent 53bf00de69
commit 982cd28265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -17,6 +17,8 @@ import
libp2p/protocols/ping,
libp2p/protocols/pubsub/gossipsub,
libp2p/protocols/pubsub/rpc/messages,
libp2p/protocols/connectivity/autonat/client,
libp2p/protocols/connectivity/autonat/service,
libp2p/nameresolving/nameresolver,
libp2p/builders,
libp2p/multihash,
@ -204,6 +206,26 @@ proc new*(T: type WakuNode,
wakuFlags,
enrMultiaddrs)
## AutonatService request other peers to dial us back
## flagging us as Reachable or NotReachable.
## minConfidence is used as threshold to determine the state.
## If maxQueueSize > numPeersToAsk past samples are considered
## in the calculation.
let autonatService = AutonatService.new(
autonatClient = AutonatClient.new(),
rng = rng,
scheduleInterval = some(chronos.seconds(120)),
askNewConnectedPeers = false,
numPeersToAsk = 3,
maxQueueSize = 3,
minConfidence = 0.7)
proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Option[float]) {.gcsafe, async.} =
if confidence.isSome():
info "Peer reachability status", networkReachability=networkReachability, confidence=confidence.get()
autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler)
info "Initializing networking", addrs=announcedAddresses
let switch = newWakuSwitch(
@ -220,6 +242,7 @@ proc new*(T: type WakuNode,
sendSignedPeerRecord = sendSignedPeerRecord,
agentString = agentString,
peerStoreCapacity = peerStoreCapacity,
services = @[Service(autonatservice)],
)
let wakuNode = WakuNode(

View File

@ -12,6 +12,7 @@ import
libp2p/protocols/pubsub/gossipsub,
libp2p/nameresolving/nameresolver,
libp2p/builders,
libp2p/switch,
libp2p/transports/[transport, tcptransport, wstransport]
# override nim-libp2p default value (which is also 1)
@ -74,8 +75,9 @@ proc newWakuSwitch*(
wssEnabled: bool = false,
secureKeyPath: string = "",
secureCertPath: string = "",
agentString = none(string), # defaults to nim-libp2p version,
agentString = none(string), # defaults to nim-libp2p version
peerStoreCapacity = none(int), # defaults to nim-libp2p max size
services: seq[switch.Service] = @[],
): Switch
{.raises: [Defect, IOError, LPError].} =
@ -111,4 +113,7 @@ proc newWakuSwitch*(
else :
b = b.withAddress(address)
if services.len > 0:
b = b.withServices(services)
b.build()