From 96ef61b8e97bc3fa5f9f0469de514bd9a7172f12 Mon Sep 17 00:00:00 2001 From: Denis Mazur Date: Wed, 18 Aug 2021 20:00:21 +0300 Subject: [PATCH 1/4] add less for PeerID --- hivemind/p2p/p2p_daemon_bindings/datastructures.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hivemind/p2p/p2p_daemon_bindings/datastructures.py b/hivemind/p2p/p2p_daemon_bindings/datastructures.py index 296c72761..ba908b2b5 100644 --- a/hivemind/p2p/p2p_daemon_bindings/datastructures.py +++ b/hivemind/p2p/p2p_daemon_bindings/datastructures.py @@ -74,6 +74,9 @@ def __eq__(self, other: object) -> bool: else: return False + def __less__(self, other: "PeerID") -> bool: + return self._bytes < other._bytes + def __hash__(self) -> int: return hash(self._bytes) From c3d019fec2a3ce3f755b3e2cd73fc5b9ff93bc42 Mon Sep 17 00:00:00 2001 From: Alexander Borzunov Date: Wed, 18 Aug 2021 23:36:00 +0300 Subject: [PATCH 2/4] Support non-PeerID types, use .to_base58() instead of ._bytes since only it is visible to humans --- hivemind/p2p/p2p_daemon_bindings/datastructures.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hivemind/p2p/p2p_daemon_bindings/datastructures.py b/hivemind/p2p/p2p_daemon_bindings/datastructures.py index ba908b2b5..c9933ea5f 100644 --- a/hivemind/p2p/p2p_daemon_bindings/datastructures.py +++ b/hivemind/p2p/p2p_daemon_bindings/datastructures.py @@ -74,8 +74,11 @@ def __eq__(self, other: object) -> bool: else: return False - def __less__(self, other: "PeerID") -> bool: - return self._bytes < other._bytes + def __less__(self, other: object) -> bool: + if not isinstance(other, PeerID): + raise ValueError(f"Can't order PeerID and {type(other)}") + + return self.to_base58() < other.to_base58() def __hash__(self) -> int: return hash(self._bytes) From 8c014f2e66a3d648eca47c9ab5683e07aafcfb9d Mon Sep 17 00:00:00 2001 From: Alexander Borzunov Date: Wed, 18 Aug 2021 23:41:16 +0300 Subject: [PATCH 3/4] Use TypeError instead of ValueError --- hivemind/p2p/p2p_daemon_bindings/datastructures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hivemind/p2p/p2p_daemon_bindings/datastructures.py b/hivemind/p2p/p2p_daemon_bindings/datastructures.py index c9933ea5f..00238740f 100644 --- a/hivemind/p2p/p2p_daemon_bindings/datastructures.py +++ b/hivemind/p2p/p2p_daemon_bindings/datastructures.py @@ -76,7 +76,7 @@ def __eq__(self, other: object) -> bool: def __less__(self, other: object) -> bool: if not isinstance(other, PeerID): - raise ValueError(f"Can't order PeerID and {type(other)}") + raise TypeError(f"'<' not supported between instances of 'PeerID' and '{type(other)}'") return self.to_base58() < other.to_base58() From 73b5d7ce343c63e4da5b1d1ded2ead29737eb13a Mon Sep 17 00:00:00 2001 From: Alexander Borzunov Date: Wed, 18 Aug 2021 23:58:13 +0300 Subject: [PATCH 4/4] Fix __less__ -> __lt__, add tests --- hivemind/p2p/p2p_daemon_bindings/datastructures.py | 2 +- tests/test_p2p_daemon_bindings.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/hivemind/p2p/p2p_daemon_bindings/datastructures.py b/hivemind/p2p/p2p_daemon_bindings/datastructures.py index 00238740f..b3b8d3eaf 100644 --- a/hivemind/p2p/p2p_daemon_bindings/datastructures.py +++ b/hivemind/p2p/p2p_daemon_bindings/datastructures.py @@ -74,7 +74,7 @@ def __eq__(self, other: object) -> bool: else: return False - def __less__(self, other: object) -> bool: + def __lt__(self, other: object) -> bool: if not isinstance(other, PeerID): raise TypeError(f"'<' not supported between instances of 'PeerID' and '{type(other)}'") diff --git a/tests/test_p2p_daemon_bindings.py b/tests/test_p2p_daemon_bindings.py index 3bc215e2d..fc7d30d61 100644 --- a/tests/test_p2p_daemon_bindings.py +++ b/tests/test_p2p_daemon_bindings.py @@ -144,6 +144,12 @@ def test_peer_id(): peer_id_3 = PeerID.from_base58("QmbmfNDEth7Ucvjuxiw3SP3E4PoJzbk7g4Ge6ZDigbCsNp") assert PEER_ID != peer_id_3 + a = PeerID.from_base58("bob") + b = PeerID.from_base58("eve") + assert a < b and b > a and not (b < a) and not (a > b) + with pytest.raises(TypeError): + assert a < object() + def test_stream_info(): proto = "123"