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

Make Subtensor.determine_chain_endpoint_and_network more explicit and readable #2561

Open
wants to merge 5 commits into
base: staging
Choose a base branch
from
Open
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
17 changes: 6 additions & 11 deletions bittensor/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@
WALLETS_DIR = USER_BITTENSOR_DIR / "wallets"
MINERS_DIR = USER_BITTENSOR_DIR / "miners"


# Create dirs if they don't exist
WALLETS_DIR.mkdir(parents=True, exist_ok=True)
MINERS_DIR.mkdir(parents=True, exist_ok=True)

# Bittensor networks name
NETWORKS = ["finney", "test", "archive", "local", "subvortex"]

DEFAULT_ENDPOINT = "wss://entrypoint-finney.opentensor.ai:443"
DEFAULT_NETWORK = NETWORKS[0]

# Bittensor endpoints (Needs to use wss://)
FINNEY_ENTRYPOINT = "wss://entrypoint-finney.opentensor.ai:443"
FINNEY_TEST_ENTRYPOINT = "wss://test.finney.opentensor.ai:443"
Expand All @@ -56,13 +52,12 @@
NETWORKS[4]: SUBVORTEX_ENTRYPOINT,
}

REVERSE_NETWORK_MAP = {
FINNEY_ENTRYPOINT: NETWORKS[0],
FINNEY_TEST_ENTRYPOINT: NETWORKS[1],
ARCHIVE_ENTRYPOINT: NETWORKS[2],
LOCAL_ENTRYPOINT: NETWORKS[3],
SUBVORTEX_ENTRYPOINT: NETWORKS[4],
}
# Default Network and Endpoint
DEFAULT_NETWORK = NETWORKS[0]
DEFAULT_ENDPOINT = NETWORK_MAP[DEFAULT_NETWORK]

# Reverse mapping for endpoints to networks
REVERSE_NETWORK_MAP = {v: k for k, v in NETWORK_MAP.items()}

# Currency Symbols Bittensor
TAO_SYMBOL: str = chr(0x03C4)
Expand Down
72 changes: 34 additions & 38 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,40 @@ def add_args(cls, parser: "argparse.ArgumentParser", prefix: Optional[str] = Non
# re-parsing arguments.
pass

@staticmethod
def determine_chain_endpoint_and_network(
network: str,
) -> tuple[Optional[str], Optional[str]]:
"""Determines the chain endpoint and network from the passed network or chain_endpoint.

Args:
network (str): The network flag. The choices are: ``finney`` (main network), ``archive`` (archive network +300 blocks), ``local`` (local running network), ``test`` (test network).

Returns:
tuple[Optional[str], Optional[str]]: The network and chain endpoint flag. If passed, overrides the ``network`` argument.
"""

if network is None:
return None, None
if network in settings.NETWORKS:
return network, settings.NETWORK_MAP[network]

substrings_map = {
"entrypoint-finney.opentensor.ai": ("finney", settings.FINNEY_ENTRYPOINT),
"test.finney.opentensor.ai": ("test", settings.FINNEY_TEST_ENTRYPOINT),
"archive.chain.opentensor.ai": ("archive", settings.ARCHIVE_ENTRYPOINT),
"subvortex": ("subvortex", network),
"subvortex.info": ("subvortex", network),
"127.0.0.1": ("local", network),
"localhost": ("local", network),
}

for substring, result in substrings_map.items():
if substring in network:
return result

return "unknown", network

# Inner private functions
@networking.ensure_connected
def _encode_params(
Expand Down Expand Up @@ -709,44 +743,6 @@ def metagraph(

return metagraph

@staticmethod
def determine_chain_endpoint_and_network(
network: str,
) -> tuple[Optional[str], Optional[str]]:
"""Determines the chain endpoint and network from the passed network or chain_endpoint.

Args:
network (str): The network flag. The choices are: ``finney`` (main network), ``archive`` (archive network +300 blocks), ``local`` (local running network), ``test`` (test network).

Returns:
tuple[Optional[str], Optional[str]]: The network and chain endpoint flag. If passed, overrides the ``network`` argument.
"""

if network is None:
return None, None
if network in settings.NETWORKS:
return network, settings.NETWORK_MAP[network]
else:
if (
network == settings.FINNEY_ENTRYPOINT
or "entrypoint-finney.opentensor.ai" in network
):
return "finney", settings.FINNEY_ENTRYPOINT
elif (
network == settings.FINNEY_TEST_ENTRYPOINT
or "test.finney.opentensor.ai" in network
):
return "test", settings.FINNEY_TEST_ENTRYPOINT
elif (
network == settings.ARCHIVE_ENTRYPOINT
or "archive.chain.opentensor.ai" in network
):
return "archive", settings.ARCHIVE_ENTRYPOINT
elif "127.0.0.1" in network or "localhost" in network:
return "local", network
else:
return "unknown", network

def get_netuids_for_hotkey(
self, hotkey_ss58: str, block: Optional[int] = None
) -> list[int]:
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def mock_add_argument(*args, **kwargs):
"archive",
settings.ARCHIVE_ENTRYPOINT,
),
("subvortex", "subvortex", settings.SUBVORTEX_ENTRYPOINT),
("127.0.0.1", "local", "127.0.0.1"),
("localhost", "local", "localhost"),
# Edge cases
Expand Down
Loading