Skip to content
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 6.4.1 / 2023-12-01

## What's Changed
* add helpful messages to signal coming changes in https://github.com/opentensor/bittensor/pull/1600/commits/86c0c3ccfcd91d0e3ff87f53bdc3e9c5e68661da
* revert default subtensor network to finney in https://github.com/opentensor/bittensor/pull/1600/commits/8c69a3c15cd556384d0309e951f0a9b164dd36cb

**Full Changelog**: https://github.com/opentensor/bittensor/compare/v6.0.1...v6.4.1


## 6.4.0 / 2023-11-29

## What's Changed
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.4.0
6.4.1
2 changes: 1 addition & 1 deletion bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
nest_asyncio.apply()

# Bittensor code and protocol version.
__version__ = "6.4.0"
__version__ = "6.4.1"
version_split = __version__.split(".")
__version_as_int__ = (
(100 * int(version_split[0]))
Expand Down
63 changes: 52 additions & 11 deletions bittensor/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class subtensor:
investments.

Attributes:
network (str): The name of the Bittensor network (e.g., 'finney', 'local') the instance is
connected to, determining the blockchain interaction context.
network (str): The name of the Bittensor network (e.g., 'finney', 'test', 'archive', 'local') the instance
is connected to, determining the blockchain interaction context.
chain_endpoint (str): The blockchain node endpoint URL, enabling direct communication
with the Bittensor blockchain for transaction processing and data retrieval.

Expand Down Expand Up @@ -154,18 +154,20 @@ def help(cls):
def add_args(cls, parser: argparse.ArgumentParser, prefix: str = None):
prefix_str = "" if prefix == None else prefix + "."
try:
default_network = os.getenv("BT_SUBTENSOR_NETWORK") or "local"
default_network = os.getenv("BT_SUBTENSOR_NETWORK") or "finney"
default_chain_endpoint = (
os.getenv("BT_SUBTENSOR_CHAIN_ENDPOINT")
or bittensor.__local_entrypoint__
or bittensor.__finney_entrypoint__
)
parser.add_argument(
"--" + prefix_str + "subtensor.network",
default=default_network,
type=str,
help="""The subtensor network flag. The likely choices are:
-- local (local running network)
-- finney (main network)
-- test (test network)
-- archive (archive network +300 blocks)
-- local (local running network)
If this option is set it overloads subtensor.chain_endpoint with
an entry point node from that network.
""",
Expand Down Expand Up @@ -195,6 +197,7 @@ def determine_chain_endpoint_and_network(network: str):
Args:
network (str): The network flag. The likely choices are:
-- finney (main network)
-- archive (archive network +300 blocks)
-- local (local running network)
-- test (test network)
chain_endpoint (str): The chain endpoint flag. If set, overrides the network argument.
Expand Down Expand Up @@ -299,6 +302,13 @@ def __init__(
"""
Initializes a Subtensor interface for interacting with the Bittensor blockchain.

NOTE: Currently subtensor defaults to the finney network. This will change in a future release.

We strongly encourage users to run their own local subtensor node whenever possible. This increases
decentralization and resilience of the network. In a future release, local subtensor will become the
default and the fallback to finney removed. Please plan ahead for this change. We will provide detailed
instructions on how to run a local subtensor node in the documentation in a subsequent release.

Args:
network (str, optional): The network name to connect to (e.g., 'finney', 'local').
Defaults to the main Bittensor network if not specified.
Expand All @@ -320,6 +330,22 @@ def __init__(
# Setup config.subtensor.network and config.subtensor.chain_endpoint
self.chain_endpoint, self.network = subtensor.setup_config(network, config)

if (
self.network == "finney"
or self.chain_endpoint == bittensor.__finney_entrypoint__
):
bittensor.logging.info(
f"You are connecting to {self.network} network with endpoint {self.chain_endpoint}."
)
bittensor.logging.warning(
"We strongly encourage running a local subtensor node whenever possible. "
"This increases decentralization and resilience of the network."
)
bittensor.logging.warning(
"In a future release, local subtensor will become the default endpoint. "
"To get ahead of this change, please run a local subtensor node and point to it."
)

# Returns a mocked connection with a background chain connection.
self.config.subtensor._mock = (
_mock
Expand All @@ -330,12 +356,27 @@ def __init__(
config.subtensor._mock = True
return bittensor.subtensor_mock.MockSubtensor()

# Set up params.
self.substrate = SubstrateInterface(
ss58_format=bittensor.__ss58_format__,
use_remote_preset=True,
url=self.chain_endpoint,
type_registry=bittensor.__type_registry__,
# Attempt to connect to chosen endpoint. Fallback to finney if local unavailable.
try:
# Set up params.
self.substrate = SubstrateInterface(
ss58_format=bittensor.__ss58_format__,
use_remote_preset=True,
url=self.chain_endpoint,
type_registry=bittensor.__type_registry__,
)
except ConnectionRefusedError as e:
bittensor.logging.error(
f"Could not connect to {self.network} network with {self.chain_endpoint} chain endpoint. Exiting..."
)
bittensor.logging.info(
f"You can check if you have connectivity by runing this command: nc -vz localhost {self.chain_endpoint.split(':')[2]}"
)
exit(1)
# TODO (edu/phil): Advise to run local subtensor and point to dev docs.

bittensor.logging.info(
f"Connected to {self.network} network and {self.chain_endpoint}."
)

def __str__(self) -> str:
Expand Down
23 changes: 4 additions & 19 deletions tests/integration_tests/test_subtensor_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,25 +587,10 @@ class ExitEarly(Exception):
msg="only tries to submit once, then exits",
)

@staticmethod
def is_port_open(host, port):
"""Check if a port is open on a given host."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex((host, port)) == 0

def test_defaults_to_local(self):
port = int(
bittensor.__local_entrypoint__.split(":")[-1]
) # Default port for local subtensor
if self.is_port_open("127.0.0.1", port):
# If service is running, check default values
sub = bittensor.subtensor()
self.assertEqual(sub.network, "local")
self.assertEqual(sub.chain_endpoint, bittensor.__local_entrypoint__)
else:
# If service is not running, expect a ConnectionRefusedError
with self.assertRaises(ConnectionRefusedError):
bittensor.subtensor()
def test_defaults_to_finney(self):
sub = bittensor.subtensor()
assert sub.network == "finney"
assert sub.chain_endpoint == bittensor.__finney_entrypoint__


if __name__ == "__main__":
Expand Down