Skip to content

Commit d09ecde

Browse files
committed
Use _tpt_proto_to_maddr lookup in mk_maddr()
Address Copilot review: the mapping table was defined but never referenced. Now `mk_maddr()` resolves `proto_key` -> maddr protocol name via the table and rejects unknown keys upfront. Also add missing `Path` import to the `multiaddr` usage snippet. Review: PR #429 (Copilot) #429 (review) (this patch was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code
1 parent 2de1d03 commit d09ecde

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

snippets/multiaddr_ex.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from pathlib import Path
12
from multiaddr import Multiaddr
3+
24
# construct from a string
35
m1 = Multiaddr("/ip4/127.0.0.1/udp/1234")
46
m2 = Multiaddr("/unix/run/user/1000/sway-ipc.1000.1557.sock")

tractor/discovery/_multiaddr.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ def mk_maddr(
5050
multiaddr-spec-compliant protocol path.
5151
5252
'''
53-
match addr.proto_key:
53+
proto_key: str = addr.proto_key
54+
maddr_proto: str|None = _tpt_proto_to_maddr.get(proto_key)
55+
if maddr_proto is None:
56+
raise ValueError(
57+
f'Unsupported proto_key: {proto_key!r}'
58+
)
59+
60+
match proto_key:
5461
case 'tcp':
5562
host, port = addr.unwrap()
5663
ip = ipaddress.ip_address(host)
@@ -59,17 +66,12 @@ def mk_maddr(
5966
else 'ip6'
6067
)
6168
return Multiaddr(
62-
f'/{net_proto}/{host}/tcp/{port}'
69+
f'/{net_proto}/{host}/{maddr_proto}/{port}'
6370
)
6471

6572
case 'uds':
6673
filedir, filename = addr.unwrap()
6774
filepath = Path(filedir) / filename
6875
return Multiaddr(
69-
f'/unix/{filepath}'
70-
)
71-
72-
case _:
73-
raise ValueError(
74-
f'Unsupported proto_key: {addr.proto_key!r}'
76+
f'/{maddr_proto}/{filepath}'
7577
)

0 commit comments

Comments
 (0)