From ad0829d18927a293d76cf47ed1152a291cd3ee53 Mon Sep 17 00:00:00 2001 From: Dany Gagnon Date: Fri, 31 May 2024 14:28:57 -0400 Subject: [PATCH 1/5] fix: ipv6 is packed with port --- bittensor/chain_data.py | 5 +++-- bittensor/utils/networking.py | 22 ++++++++++++++++++++++ tests/unit_tests/utils/test_networking.py | 8 ++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/bittensor/chain_data.py b/bittensor/chain_data.py index 49f92e5ce6..94b131cdd9 100644 --- a/bittensor/chain_data.py +++ b/bittensor/chain_data.py @@ -283,10 +283,11 @@ def from_neuron_info(cls, neuron_info: dict) -> "AxonInfo": Returns: instance (AxonInfo): An instance of AxonInfo created from the dictionary. """ + ip, port = net.unpack_encoded_ip_port(neuron_info["axon_info"]["ip"], neuron_info["axon_info"]["port"]) return cls( version=neuron_info["axon_info"]["version"], - ip=net.int_to_ip(int(neuron_info["axon_info"]["ip"])), - port=neuron_info["axon_info"]["port"], + ip=ip, + port=port, ip_type=neuron_info["axon_info"]["ip_type"], hotkey=neuron_info["hotkey"], coldkey=neuron_info["coldkey"], diff --git a/bittensor/utils/networking.py b/bittensor/utils/networking.py index 9f1450af81..79d01ab6f3 100644 --- a/bittensor/utils/networking.py +++ b/bittensor/utils/networking.py @@ -44,6 +44,28 @@ def int_to_ip(int_val: int) -> str: return str(netaddr.IPAddress(int_val)) +def unpack_encoded_ip_port(ip_str: str, port: int) -> tuple: + r"""Unpacks an encoded IP and port if they are encoded together. + Args: + ip_str (:type:`str`, `required`): + The encoded IP address string. + port (:type:`int`, `required`): + The port number. + + Returns: + tuple: A tuple containing the IP address string and port number. + + Raises: + netaddr.core.AddrFormatError (Exception): + Raised when the passed IP string is not a valid IP int value. + """ + if ip_str < (1 << 128) + (1 << 16) and port == 0: + port = ip_str & 0xFFFF + ip = ip_str >> 16 + return int_to_ip(ip), port + return int_to_ip(ip), port + + def ip_to_int(str_val: str) -> int: r"""Maps an ip-string to a unique integer. arg: diff --git a/tests/unit_tests/utils/test_networking.py b/tests/unit_tests/utils/test_networking.py index 2037718578..5f7b445d77 100644 --- a/tests/unit_tests/utils/test_networking.py +++ b/tests/unit_tests/utils/test_networking.py @@ -25,6 +25,14 @@ def test_int_to_ip_range(): ) +def test_packed_ip_port(): + """Test packing and unpacking IP and port.""" + assert utils.networking.unpack_encoded_ip_port(184046647580618, 0) == ( + utils.networking.ip_to_int("167.99.179.13"), + 6090, + ) + + def test_int_to_ip4_max(): """Test converting integer to maximum IPv4 address.""" assert utils.networking.int_to_ip(4294967295) == "255.255.255.255" From 18ee92880de9c6c14e419b2d9a7f476d090a305f Mon Sep 17 00:00:00 2001 From: Dany Gagnon Date: Fri, 31 May 2024 14:35:50 -0400 Subject: [PATCH 2/5] refactor: black ran on chain_data --- bittensor/chain_data.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bittensor/chain_data.py b/bittensor/chain_data.py index 94b131cdd9..689f547dc3 100644 --- a/bittensor/chain_data.py +++ b/bittensor/chain_data.py @@ -283,7 +283,9 @@ def from_neuron_info(cls, neuron_info: dict) -> "AxonInfo": Returns: instance (AxonInfo): An instance of AxonInfo created from the dictionary. """ - ip, port = net.unpack_encoded_ip_port(neuron_info["axon_info"]["ip"], neuron_info["axon_info"]["port"]) + ip, port = net.unpack_encoded_ip_port( + neuron_info["axon_info"]["ip"], neuron_info["axon_info"]["port"] + ) return cls( version=neuron_info["axon_info"]["version"], ip=ip, From 2a9c2687e4aa6eeed110f22e88901d6962d8ceff Mon Sep 17 00:00:00 2001 From: Dany Gagnon Date: Fri, 31 May 2024 14:42:22 -0400 Subject: [PATCH 3/5] fix: test int --- tests/unit_tests/utils/test_networking.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit_tests/utils/test_networking.py b/tests/unit_tests/utils/test_networking.py index 5f7b445d77..00e84a0a97 100644 --- a/tests/unit_tests/utils/test_networking.py +++ b/tests/unit_tests/utils/test_networking.py @@ -28,7 +28,7 @@ def test_int_to_ip_range(): def test_packed_ip_port(): """Test packing and unpacking IP and port.""" assert utils.networking.unpack_encoded_ip_port(184046647580618, 0) == ( - utils.networking.ip_to_int("167.99.179.13"), + 2808328973, 6090, ) From cf2885bfb8ddb39ba4f2920427f2259906912505 Mon Sep 17 00:00:00 2001 From: Dany Gagnon Date: Fri, 31 May 2024 14:42:53 -0400 Subject: [PATCH 4/5] fix: test int --- tests/unit_tests/utils/test_networking.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit_tests/utils/test_networking.py b/tests/unit_tests/utils/test_networking.py index 00e84a0a97..6bc89d3f27 100644 --- a/tests/unit_tests/utils/test_networking.py +++ b/tests/unit_tests/utils/test_networking.py @@ -28,7 +28,7 @@ def test_int_to_ip_range(): def test_packed_ip_port(): """Test packing and unpacking IP and port.""" assert utils.networking.unpack_encoded_ip_port(184046647580618, 0) == ( - 2808328973, + "167.99.179.13", 6090, ) From 94a2505d07e16774285b2f18dd5002674cb1f831 Mon Sep 17 00:00:00 2001 From: Dany Gagnon Date: Fri, 31 May 2024 14:55:39 -0400 Subject: [PATCH 5/5] fix: small mistake typo --- bittensor/utils/networking.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bittensor/utils/networking.py b/bittensor/utils/networking.py index 79d01ab6f3..0352cdfaa8 100644 --- a/bittensor/utils/networking.py +++ b/bittensor/utils/networking.py @@ -63,7 +63,7 @@ def unpack_encoded_ip_port(ip_str: str, port: int) -> tuple: port = ip_str & 0xFFFF ip = ip_str >> 16 return int_to_ip(ip), port - return int_to_ip(ip), port + return int_to_ip(ip_str), port def ip_to_int(str_val: str) -> int: