From 9c2560d00041fe8322d9ee933cfb0a5accea352b Mon Sep 17 00:00:00 2001 From: acul71 <34693171+acul71@users.noreply.github.com> Date: Sun, 13 Jul 2025 21:28:50 +0000 Subject: [PATCH 1/2] fix: added valid CID and fix typecheck --- libp2p/peer/peerinfo.py | 5 ++++- tests/core/peer/test_peerinfo.py | 9 ++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libp2p/peer/peerinfo.py b/libp2p/peer/peerinfo.py index 29ce4e66c..568bb25dc 100644 --- a/libp2p/peer/peerinfo.py +++ b/libp2p/peer/peerinfo.py @@ -3,9 +3,11 @@ ) from typing import ( Any, + cast, ) import multiaddr +from multiaddr.protocols import Protocol from .id import ( ID, @@ -42,7 +44,8 @@ def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo: p2p_protocols = p2p_part.protocols() if not p2p_protocols: raise InvalidAddrError("The last part of the address has no protocols") - last_protocol = p2p_protocols[0] + last_protocol = cast(Protocol, p2p_part.protocols()[0]) + if last_protocol is None: raise InvalidAddrError("The last protocol is None") diff --git a/tests/core/peer/test_peerinfo.py b/tests/core/peer/test_peerinfo.py index 5e67d0226..20b6705c2 100644 --- a/tests/core/peer/test_peerinfo.py +++ b/tests/core/peer/test_peerinfo.py @@ -13,7 +13,9 @@ ) ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" -VALID_MULTI_ADDR_STR = "/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ" # noqa: E501 +VALID_MULTI_ADDR_STR = ( + "/ip4/127.0.0.1/tcp/8000/p2p/QmWQqHcMi6Cay5M6KWSNVYSDnxzfqWb1aGFQFSRzBNe49t" +) def test_init_(): @@ -50,9 +52,6 @@ def test_info_from_p2p_addr_invalid(addr): def test_info_from_p2p_addr_valid(): m_addr = multiaddr.Multiaddr(VALID_MULTI_ADDR_STR) info = info_from_p2p_addr(m_addr) - assert ( - info.peer_id.pretty() - == "3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ" - ) + assert info.peer_id.pretty() == "QmWQqHcMi6Cay5M6KWSNVYSDnxzfqWb1aGFQFSRzBNe49t" assert len(info.addrs) == 1 assert str(info.addrs[0]) == "/ip4/127.0.0.1/tcp/8000" From e013e80689f19958f867c396999ab5313a1864d9 Mon Sep 17 00:00:00 2001 From: acul71 <34693171+acul71@users.noreply.github.com> Date: Tue, 15 Jul 2025 15:43:48 +0000 Subject: [PATCH 2/2] doc: newsfragments --- newsfragments/757.bugfix.rst | 1 + newsfragments/757.internal.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 newsfragments/757.bugfix.rst create mode 100644 newsfragments/757.internal.rst diff --git a/newsfragments/757.bugfix.rst b/newsfragments/757.bugfix.rst new file mode 100644 index 000000000..ff4d1619b --- /dev/null +++ b/newsfragments/757.bugfix.rst @@ -0,0 +1 @@ +fixed malformed PeerId in test_peerinfo diff --git a/newsfragments/757.internal.rst b/newsfragments/757.internal.rst new file mode 100644 index 000000000..5745168f2 --- /dev/null +++ b/newsfragments/757.internal.rst @@ -0,0 +1 @@ +fixed a typecheck error using cast in peerinfo.py