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)
https://github.com/goodboy/tractor/pull/429#pullrequestreview-4010456884

(this patch was generated in some part by [`claude-code`][claude-code-gh])
[claude-code-gh]: https://github.com/anthropics/claude-code
subint_spawner_backend
Gud Boi 2026-03-25 19:38:21 -04:00
parent 926e861f52
commit c72d495d68
2 changed files with 12 additions and 8 deletions

View File

@ -1,4 +1,6 @@
from pathlib import Path
from multiaddr import Multiaddr from multiaddr import Multiaddr
# construct from a string # construct from a string
m1 = Multiaddr("/ip4/127.0.0.1/udp/1234") m1 = Multiaddr("/ip4/127.0.0.1/udp/1234")
m2 = Multiaddr("/unix/run/user/1000/sway-ipc.1000.1557.sock") m2 = Multiaddr("/unix/run/user/1000/sway-ipc.1000.1557.sock")

View File

@ -50,7 +50,14 @@ def mk_maddr(
multiaddr-spec-compliant protocol path. multiaddr-spec-compliant protocol path.
''' '''
match addr.proto_key: proto_key: str = addr.proto_key
maddr_proto: str|None = _tpt_proto_to_maddr.get(proto_key)
if maddr_proto is None:
raise ValueError(
f'Unsupported proto_key: {proto_key!r}'
)
match proto_key:
case 'tcp': case 'tcp':
host, port = addr.unwrap() host, port = addr.unwrap()
ip = ipaddress.ip_address(host) ip = ipaddress.ip_address(host)
@ -59,17 +66,12 @@ def mk_maddr(
else 'ip6' else 'ip6'
) )
return Multiaddr( return Multiaddr(
f'/{net_proto}/{host}/tcp/{port}' f'/{net_proto}/{host}/{maddr_proto}/{port}'
) )
case 'uds': case 'uds':
filedir, filename = addr.unwrap() filedir, filename = addr.unwrap()
filepath = Path(filedir) / filename filepath = Path(filedir) / filename
return Multiaddr( return Multiaddr(
f'/unix/{filepath}' f'/{maddr_proto}/{filepath}'
)
case _:
raise ValueError(
f'Unsupported proto_key: {addr.proto_key!r}'
) )