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/only explorer if known network #1037

Merged
merged 10 commits into from
Jan 19, 2023
9 changes: 9 additions & 0 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ def turn_console_off():
__local_entrypoint__ = "127.0.0.1:9944"


# Block Explorers map network to explorer url
## Must all be polkadotjs explorer urls
__network_explorer_map__ = {
'local': "https://explorer.nakamoto.opentensor.ai",
'nakamoto': "https://explorer.nakamoto.opentensor.ai",
'endpoint': "https://explorer.nakamoto.opentensor.ai",
'nobunaga': "https://staging.opentensor.ai",
}

# Avoid collisions with other processes
from .utils.test_utils import get_random_unused_port
mock_subtensor_port = get_random_unused_port()
Expand Down
9 changes: 7 additions & 2 deletions bittensor/_subtensor/subtensor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,8 +1129,11 @@ def transfer(
bittensor.__console__.print(":white_heavy_check_mark: [green]Finalized[/green]")
block_hash = response.block_hash
bittensor.__console__.print("[green]Block Hash: {}[/green]".format( block_hash ))
explorer_url = "https://explorer.nakamoto.opentensor.ai/#/explorer/query/{block_hash}".format( block_hash = block_hash )
bittensor.__console__.print("[green]Explorer Link: {}[/green]".format( explorer_url ))

explorer_url = bittensor.utils.get_explorer_url_for_network( self.network, block_hash )
if explorer_url is not None:
bittensor.__console__.print("[green]Explorer Link: {}[/green]".format( explorer_url ))

else:
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: error:{}".format(response.error_message))

Expand All @@ -1142,6 +1145,8 @@ def transfer(

return False



def unstake (
self,
wallet: 'bittensor.wallet',
Expand Down
41 changes: 41 additions & 0 deletions bittensor/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,44 @@ def strtobool(val: str) -> bool:
else:
raise ValueError("invalid truth value %r" % (val,))

def get_explorer_root_url_by_network_from_map(network: str, network_map: Dict[str, str]) -> Optional[str]:
r"""
Returns the explorer root url for the given network name from the given network map.

Args:
network(str): The network to get the explorer url for.
network_map(Dict[str, str]): The network map to get the explorer url from.

Returns:
The explorer url for the given network.
Or None if the network is not in the network map.
"""
explorer_url: Optional[str] = None
if network in network_map:
explorer_url = network_map[network]

return explorer_url


def get_explorer_url_for_network(network: str, block_hash: str) -> Optional[str]:
r"""
Returns the explorer url for the given block hash and network.

Args:
network(str): The network to get the explorer url for.
block_hash(str): The block hash to get the explorer url for.

Returns:
The explorer url for the given block hash and network.
Or None if the network is not known.
"""

explorer_url: Optional[str] = None
# Will be None if the network is not known. i.e. not in bittensor.__network_explorer_map__
explorer_root_url: Optional[str] = get_explorer_root_url_by_network_from_map(network, bittensor.__network_explorer_map__)

if explorer_root_url is not None:
# We are on a known network.
explorer_url = "{root_url}/#/explorer/query/{block_hash}".format( root_url=explorer_root_url, block_hash = block_hash )

return explorer_url
23 changes: 21 additions & 2 deletions tests/unit_tests/bittensor_tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
import unittest
from sys import platform
from types import SimpleNamespace
from typing import Dict
from unittest.mock import MagicMock, patch

import bittensor
import pytest
import torch
from _pytest.fixtures import fixture
from bittensor.utils import CUDASolver
from loguru import logger
from substrateinterface.base import Keypair

import bittensor
from bittensor.utils import CUDASolver


@fixture(scope="function")
def setup_chain():
Expand Down Expand Up @@ -530,5 +532,22 @@ class MockException(Exception):
self.assertEqual(nonce_start_after_iteration, (initial_nonce_start + update_interval * TPB) % nonce_limit, "nonce_start was not updated by the correct amount")


class TestExplorerURL(unittest.TestCase):
network_map: Dict[str, str] = {
"nakamoto": "https://polkadot.js.org/apps/?rpc=wss://archivelb.nakamoto.opentensor.ai:9943/explorer",
"example": "https://polkadot.js.org/apps/?rpc=wss://example.example.com/explorer",
# "bad": None # no explorer for this network
camfairchild marked this conversation as resolved.
Show resolved Hide resolved
}

@pytest.mark.parametrize("network, expected", [
("nobunaga", "https://polkadot.js.org/apps/?rpc=wss://nobunaga.bittensor.com:9943/explorer"),
("example", "https://polkadot.js.org/apps/?rpc=wss://example.example.com/explorer"),
("bad", None),
("", None),
("networknamewithoutexplorer", None)
])
def get_explorer_root_url_by_network_from_map(self, network: str, expected: str) -> str:
self.assertEqual(bittensor.utils.get_explorer_root_url_by_network_from_map(network, self.network_map), expected)

if __name__ == "__main__":
unittest.main()