diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 1cb0af1e1..58c79d43b 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -87,11 +87,15 @@ def _ensure_connected(func): @wraps(func) def wrapper(self, *args, **kwargs): # Check the socket state before method execution - if self.substrate.websocket.sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) != 0: + if ( + self.substrate.websocket.sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) + != 0 + ): logging.info("Reconnection substrate...") self._get_substrate() # Execute the method if the connection is active or after reconnecting return func(self, *args, **kwargs) + return wrapper @@ -200,7 +204,6 @@ def __init__( "To get ahead of this change, please run a local subtensor node and point to it." ) - self.substrate = None self.log_verbose = log_verbose self._get_substrate() @@ -218,7 +221,7 @@ def __repr__(self) -> str: def close(self): """Cleans up resources for this subtensor instance like active websocket connection and active extensions.""" self.substrate.close() - + def _get_substrate(self): """Establishes a connection to the Substrate node using configured parameters.""" try: @@ -230,7 +233,9 @@ def _get_substrate(self): type_registry=settings.TYPE_REGISTRY, ) if self.log_verbose: - logging.info(f"Connected to {self.network} network and {self.chain_endpoint}.") + logging.info( + f"Connected to {self.network} network and {self.chain_endpoint}." + ) except ConnectionRefusedError: logging.error( diff --git a/tests/unit_tests/test_subtensor.py b/tests/unit_tests/test_subtensor.py index d605973ce..15129a03c 100644 --- a/tests/unit_tests/test_subtensor.py +++ b/tests/unit_tests/test_subtensor.py @@ -233,7 +233,9 @@ def test_determine_chain_endpoint_and_network( def subtensor(mocker): fake_substrate = mocker.MagicMock() fake_substrate.websocket.sock.getsockopt.return_value = 0 - mocker.patch.object(subtensor_module, "SubstrateInterface", return_value=fake_substrate) + mocker.patch.object( + subtensor_module, "SubstrateInterface", return_value=fake_substrate + ) return Subtensor() @@ -2164,7 +2166,8 @@ def test_get_transfer_fee(subtensor, mocker): ) subtensor.substrate.get_payment_info.assert_called_once_with( - call=subtensor.substrate.compose_call.return_value, keypair=fake_wallet.coldkeypub + call=subtensor.substrate.compose_call.return_value, + keypair=fake_wallet.coldkeypub, ) assert result == 2e10 @@ -2453,7 +2456,9 @@ def test_connect_without_substrate(mocker): # Prep fake_substrate = mocker.MagicMock() fake_substrate.websocket.sock.getsockopt.return_value = 1 - mocker.patch.object(subtensor_module, "SubstrateInterface", return_value=fake_substrate) + mocker.patch.object( + subtensor_module, "SubstrateInterface", return_value=fake_substrate + ) fake_subtensor = Subtensor() spy_get_substrate = mocker.spy(Subtensor, "_get_substrate") @@ -2469,7 +2474,9 @@ def test_connect_with_substrate(mocker): # Prep fake_substrate = mocker.MagicMock() fake_substrate.websocket.sock.getsockopt.return_value = 0 - mocker.patch.object(subtensor_module, "SubstrateInterface", return_value=fake_substrate) + mocker.patch.object( + subtensor_module, "SubstrateInterface", return_value=fake_substrate + ) fake_subtensor = Subtensor() spy_get_substrate = mocker.spy(Subtensor, "_get_substrate") @@ -2478,5 +2485,3 @@ def test_connect_with_substrate(mocker): # Assertions assert spy_get_substrate.call_count == 0 - -