Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return of ip version #1964

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bittensor/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
SynapseException,
)
from bittensor.threadpool import PriorityThreadPoolExecutor
from bittensor.utils import networking


class FastAPIThreadedServer(uvicorn.Server):
Expand Down Expand Up @@ -392,7 +393,7 @@ def info(self) -> "bittensor.AxonInfo":
return bittensor.AxonInfo(
version=bittensor.__version_as_int__,
ip=self.external_ip,
ip_type=4,
ip_type=networking.ip_version(self.external_ip),
port=self.external_port,
hotkey=self.wallet.hotkey.ss58_address,
coldkey=self.wallet.coldkeypub.ss58_address,
Expand Down
50 changes: 50 additions & 0 deletions tests/unit_tests/test_axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from unittest.mock import AsyncMock, MagicMock, patch

# Third Party
import netaddr
import pytest
from starlette.requests import Request
from fastapi.testclient import TestClient
Expand Down Expand Up @@ -349,6 +350,55 @@ def test_to_string(info_return, expected_output, test_id):
assert output == expected_output, f"Test ID: {test_id}"


@pytest.mark.parametrize(
"ip, port, expected_ip_type, test_id",
[
# Happy path
(
"127.0.0.1",
8080,
4,
"valid_ipv4",
),
(
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
3030,
6,
"valid_ipv6",
),
],
)
def test_valid_ipv4_and_ipv6_address(ip, port, expected_ip_type, test_id):
# Arrange
axon = Axon()
axon.ip = ip
axon.external_ip = ip
axon.port = port

# Act
ip_type = axon.info().ip_type

# Assert
assert ip_type == expected_ip_type, f"Test ID: {test_id}"


@pytest.mark.parametrize(
"ip, port, expected_exception",
[
(
"This Is not a valid address",
65534,
netaddr.core.AddrFormatError,
),
],
ids=["failed to detect a valid IP " "address from %r"],
)
def test_invalid_ip_address(ip, port, expected_exception):
# Assert
with pytest.raises(expected_exception):
Axon(ip=ip, external_ip=ip, port=port).info()


@pytest.mark.parametrize(
"ip, port, ss58_address, started, forward_fns, expected_str, test_id",
[
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@


def test_serve_axon_with_external_ip_set():
internal_ip: str = "this is an internal ip"
external_ip: str = "this is an external ip"
internal_ip: str = "192.0.2.146"
external_ip: str = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"

mock_serve_axon = MagicMock(return_value=True)

Expand Down Expand Up @@ -76,7 +76,7 @@ def test_serve_axon_with_external_ip_set():


def test_serve_axon_with_external_port_set():
external_ip: str = "this is an external ip"
external_ip: str = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"

internal_port: int = 1234
external_port: int = 5678
Expand Down
Loading