Skip to content

Commit 6426c0d

Browse files
committed
chore: more concise comments
1 parent ff85328 commit 6426c0d

5 files changed

Lines changed: 17 additions & 46 deletions

File tree

libp2p/services/nat/plum_mapper.nim

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33

44
## NAT port mapper backed by libplum (PCP / NAT-PMP / UPnP-IGD).
55
##
6-
## libplum owns a single process-wide client with its own internal thread that
7-
## discovers the gateway, selects a working protocol, and keeps mappings
8-
## refreshed on its own — so there is no per-mapping lease and no refresh loop
9-
## on our side. The C `plum_init`/`plum_cleanup` pair is a global singleton
10-
## (a second `plum_init` fails while a client is live), so this module
11-
## ref-counts init across every live `PlumMapper` and keeps the protocol filter
12-
## of the first mapper that initialized it.
13-
##
14-
## Precondition: every `PlumMapper` is created and closed from the same chronos
15-
## event-loop thread. The ref-count and active-filter globals below are plain
16-
## (unsynchronized) vars, so driving mappers from multiple threads would race
17-
## them and double-init libplum.
6+
## `plum_init`/`plum_cleanup` is a process-global singleton, so this module
7+
## ref-counts init across every live `PlumMapper` and keeps the first mapper's
8+
## protocol filter. The ref-count/filter globals are unsynchronized: create and
9+
## close every `PlumMapper` from the same chronos event-loop thread.
1810

1911
{.push raises: [].}
2012

@@ -29,8 +21,7 @@ logScope:
2921
topics = "libp2p natservice plum"
3022

3123
const
32-
# Module-private fallbacks for PlumMapper.new()'s own default args; the public
33-
# NAT timeout knobs live in natservice (DefaultDiscoveryTimeout/MappingTimeout).
24+
# Private defaults for new(); the public NAT knobs live in natservice.
3425
DefaultDiscoverTimeout = 10.seconds
3526
DefaultMappingTimeout = 10.seconds
3627

@@ -40,9 +31,7 @@ type
4031
PlumMapper* = ref object of PortMapper
4132
filter: ProtocolFilter
4233
closed: bool
43-
mappings: Table[MappingKey, cint]
44-
## (externalPort, proto) -> libplum mapping id, recorded on a successful
45-
## map() so unmap()/close() can find the opaque handle to destroy.
34+
mappings: Table[MappingKey, cint] ## -> libplum mapping id, to destroy later
4635

4736
var
4837
plumRefCount = 0
@@ -103,8 +92,7 @@ method map*(
10392
externalPort = externalPort.uint16,
10493
)
10594

106-
# close() may have run during the await above and torn libplum down; drop the
107-
# mapping we just created rather than recording an id for a dead handle.
95+
# close() may have torn libplum down during the await; drop the new mapping.
10896
if self.closed:
10997
destroyMapping(res.id)
11098
return err("PlumMapper closed")

libp2p/services/nat/portmapper.nim

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@ type
1111
mpTcp
1212
mpUdp
1313

14-
MappedPort* = object
15-
## Outcome of a successful mapping: the external address the NAT device
16-
## assigned. libplum returns the external host and port together with the
17-
## mapping, so there is a single `map` call rather than the old
18-
## discover-external-IP-then-map split.
14+
MappedPort* = object ## External address the NAT device assigned.
1915
externalIp*: IpAddress
2016
externalPort*: Port
2117

22-
PortMapper* = ref object of RootObj
23-
## Abstract base for a NAT port-mapping client (libplum-backed / mock).
24-
## All operations are async: the libplum backend hands the request to the
25-
## library's internal thread and awaits its completion.
18+
PortMapper* = ref object of RootObj ## Abstract async NAT port-mapping client.
2619

2720
method map*(
2821
self: PortMapper, internalPort: Port, externalPort: Port, proto: MapProto

libp2p/services/natservice.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ proc replaceTransportPort(ma: MultiAddress, port: Port): Opt[MultiAddress] =
249249
Opt.some(res)
250250

251251
proc extractListenPort(ma: MultiAddress): Opt[ListenPort] =
252-
# libplum maps IPv4 only (NAT-PMP/PCP/UPnP-IGD over IPv4), so drop non-IPv4
253-
# listen addresses early.
252+
# libplum maps IPv4 only, so drop non-IPv4 listen addresses early.
254253
let ta = initTAddress(ma).valueOr:
255254
return Opt.none(ListenPort)
256255
if ta.family != AddressFamily.IPv4:
@@ -285,8 +284,7 @@ type MappedEntry =
285284
proc mapOnePort(
286285
self: NATService, lp: ListenPort
287286
): Future[Opt[MappedEntry]] {.async: (raises: [CancelledError]).} =
288-
# libplum hands back the external address together with the mapping, so the
289-
# external IP is derived here rather than in a separate discovery step.
287+
# libplum returns the external address with the mapping; no separate discovery.
290288
let mapped = (await self.mapper.map(lp.port, lp.port, lp.proto)).valueOr:
291289
warn "NAT port mapping failed", port = lp.port, proto = lp.proto, err = error
292290
return Opt.none(MappedEntry)
@@ -356,8 +354,7 @@ proc setupMappings*(
356354
announced
357355

358356
proc validatePortMapperConfig(cfg: PortMappingConfig) {.raises: [ServiceSetupError].} =
359-
# libplum keeps mappings refreshed on its own internal thread, so there is no
360-
# lease or refresh interval to validate — only the discovery/mapping waits.
357+
# libplum refreshes mappings itself; only the discovery/mapping waits need checking.
361358
if cfg.discoveryTimeout <= 0.seconds:
362359
raise newException(
363360
ServiceSetupError,

tests/libp2p/services/test_natservice.nim

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ type
2525

2626
MockPortMapper = ref object of PortMapper
2727
extIp: IpAddress
28-
extPortQueue: seq[Port]
29-
## external ports handed out in order across successive map() calls; once
30-
## exhausted, map() echoes the requested port. Models an IGD assigning (or
31-
## reassigning) a different external port than requested.
28+
extPortQueue: seq[Port] ## ports handed out in order; once empty, echo request
3229
extPortIdx: int
3330
mapErr: Opt[string]
3431
calls: seq[MockCall]
@@ -314,8 +311,7 @@ suite "NATService":
314311
check mock.unmappedPorts().len == 0
315312

316313
asyncTest "non-IPv4 private addrs are skipped":
317-
# libplum maps IPv4 only, so any IPv6 listenAddr (even ULA fc00::/7) must be
318-
# filtered out before map().
314+
# libplum maps IPv4 only, so any IPv6 listenAddr must be filtered before map().
319315
let mock = newMock()
320316
let factory = mapperFactory(mock)
321317

@@ -615,8 +611,7 @@ suite "NATService (setupMappings)":
615611
asyncTest "IGD returning a different external port surfaces in announced":
616612
let
617613
externalIp = parseIpAddress("203.0.113.30")
618-
# queued external port simulates the IGD remapping the request to a
619-
# different port (e.g. because the requested one is already busy).
614+
# queued external port simulates the IGD remapping to a different port.
620615
mapper = newMock(extIp = externalIp, extPorts = @[Port(54321)])
621616
factory = mapperFactory(mapper)
622617
cfg = upnpConfig()

tests/libp2p/services/test_plum_mapper.nim

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ suite "PlumMapper":
3030
check r.error() == "PlumMapper closed"
3131

3232
asyncTest "unmap without a prior map returns 'no known mapping' error":
33-
# libplum identifies a mapping by an opaque id recorded on a successful
34-
# map(); without that record there is nothing to destroy.
33+
# no recorded mapping id, so there is nothing to destroy.
3534
let m = PlumMapper.new().get()
3635
defer:
3736
await m.close()
@@ -41,8 +40,7 @@ suite "PlumMapper":
4140
check r.error() == "plum unmap: no known mapping for external port 9000"
4241

4342
asyncTest "a second mapper shares the ref-counted libplum instance":
44-
# plum_init is a global singleton; overlapping mappers must not double-init
45-
# or tear libplum down while the other is still live.
43+
# overlapping mappers must not double-init or tear down the shared singleton.
4644
let a = PlumMapper.new().get()
4745
let b = PlumMapper.new().get()
4846
await a.close()

0 commit comments

Comments
 (0)