Skip to content

Commit

Permalink
Merge pull request #1961 from opentensor/bugfix/opendansor/fix_ip_ver…
Browse files Browse the repository at this point in the history
…sion

Fix return of ip version.
  • Loading branch information
gus-opentensor committed May 31, 2024
2 parents 116cb8a + a5a527a commit c38da2b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
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 @@
InternalServerError,
)
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
55 changes: 53 additions & 2 deletions tests/unit_tests/test_axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# Standard Lib
import pytest
import unittest
from typing import Any
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, MagicMock, patch

# Third Party
import netaddr

# Standard Lib
import pytest
from starlette.requests import Request

# Bittensor
Expand Down Expand Up @@ -341,6 +343,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
8 changes: 4 additions & 4 deletions tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

# Application
import bittensor
from bittensor.subtensor import subtensor as Subtensor, _logger
from bittensor import subtensor_module
from bittensor.subtensor import subtensor as Subtensor, _logger


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 @@ -72,7 +72,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

0 comments on commit c38da2b

Please sign in to comment.