Skip to content

Commit

Permalink
Ruff formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-opentensor committed Aug 27, 2024
1 parent 2d8a202 commit bfe64c3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 9 additions & 4 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand All @@ -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(
Expand Down
17 changes: 11 additions & 6 deletions tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -2478,5 +2485,3 @@ def test_connect_with_substrate(mocker):

# Assertions
assert spy_get_substrate.call_count == 0


0 comments on commit bfe64c3

Please sign in to comment.