Skip to content

Commit e85e269

Browse files
author
Roman
committed
improve subtensors
1 parent 68c3472 commit e85e269

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

bittensor/core/async_subtensor.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
import scalecodec
1111
from async_substrate_interface import AsyncSubstrateInterface
12+
from async_substrate_interface.substrate_addons import RetryAsyncSubstrate
1213
from bittensor_drand import get_encrypted_commitment
1314
from bittensor_wallet.utils import SS58_FORMAT
1415
from numpy.typing import NDArray
@@ -114,17 +115,21 @@ def __init__(
114115
self,
115116
network: Optional[str] = None,
116117
config: Optional["Config"] = None,
117-
_mock: bool = False,
118118
log_verbose: bool = False,
119+
fallback_chains: Optional[list[str]] = None,
120+
retry_forever: bool = False,
121+
_mock: bool = False,
119122
):
120123
"""
121124
Initializes an instance of the AsyncSubtensor class.
122125
123126
Arguments:
124127
network (str): The network name or type to connect to.
125128
config (Optional[Config]): Configuration object for the AsyncSubtensor instance.
126-
_mock: Whether this is a mock instance. Mainly just for use in testing.
127129
log_verbose (bool): Enables or disables verbose logging.
130+
fallback_chains (list): List of fallback chains to use if no network is specified. Defaults to `None`.
131+
retry_forever (bool): Whether to retry forever on connection errors. Defaults to `False`.
132+
_mock: Whether this is a mock instance. Mainly just for use in testing.
128133
129134
Raises:
130135
Any exceptions raised during the setup, configuration, or connection process.
@@ -151,6 +156,9 @@ def __init__(
151156
chain_name="Bittensor",
152157
_mock=_mock,
153158
)
159+
self.substrate = self._get_substrate(
160+
fallback_chains=fallback_chains, retry_forever=retry_forever, _mock=_mock
161+
)
154162
if self.log_verbose:
155163
logging.info(
156164
f"Connected to {self.network} network and {self.chain_endpoint}."
@@ -283,6 +291,38 @@ async def get_hyperparameter(
283291

284292
return getattr(result, "value", result)
285293

294+
def _get_substrate(
295+
self,
296+
fallback_chains: Optional[list[str]] = None,
297+
retry_forever: bool = False,
298+
_mock: bool = False,
299+
) -> Union[AsyncSubstrateInterface, RetryAsyncSubstrate]:
300+
"""Creates the Substrate instance based on provided arguments.
301+
302+
Arguments:
303+
fallback_chains (list): List of fallback chains to use if no network is specified. Defaults to `None`.
304+
retry_forever (bool): Whether to retry forever on connection errors. Defaults to `False`.
305+
_mock: Whether this is a mock instance. Mainly just for use in testing.
306+
307+
Returns:
308+
the instance of the SubstrateInterface or RetrySyncSubstrate class.
309+
"""
310+
if fallback_chains or retry_forever:
311+
return RetryAsyncSubstrate(
312+
url=self.chain_endpoint,
313+
fallback_chains=fallback_chains,
314+
retry_forever=retry_forever,
315+
_mock=_mock,
316+
)
317+
return AsyncSubstrateInterface(
318+
url=self.chain_endpoint,
319+
ss58_format=SS58_FORMAT,
320+
type_registry=TYPE_REGISTRY,
321+
use_remote_preset=True,
322+
chain_name="Bittensor",
323+
_mock=_mock,
324+
)
325+
286326
# Subtensor queries ===========================================================================================
287327

288328
async def query_constant(

bittensor/core/subtensor.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
import scalecodec
88
from async_substrate_interface.errors import SubstrateRequestException
9+
from async_substrate_interface.substrate_addons import RetrySyncSubstrate
910
from async_substrate_interface.sync_substrate import SubstrateInterface
1011
from async_substrate_interface.types import ScaleObj
1112
from bittensor_drand import get_encrypted_commitment
@@ -116,17 +117,21 @@ def __init__(
116117
self,
117118
network: Optional[str] = None,
118119
config: Optional["Config"] = None,
119-
_mock: bool = False,
120120
log_verbose: bool = False,
121+
fallback_chains: Optional[list[str]] = None,
122+
retry_forever: bool = False,
123+
_mock: bool = False,
121124
):
122125
"""
123126
Initializes an instance of the Subtensor class.
124127
125128
Arguments:
126129
network (str): The network name or type to connect to.
127130
config (Optional[Config]): Configuration object for the AsyncSubtensor instance.
128-
_mock: Whether this is a mock instance. Mainly just for use in testing.
129131
log_verbose (bool): Enables or disables verbose logging.
132+
fallback_chains (list): List of fallback chains to use if no network is specified. Defaults to `None`.
133+
retry_forever (bool): Whether to retry forever on connection errors. Defaults to `False`.
134+
_mock: Whether this is a mock instance. Mainly just for use in testing.
130135
131136
Raises:
132137
Any exceptions raised during the setup, configuration, or connection process.
@@ -143,13 +148,8 @@ def __init__(
143148
f"Connecting to network: [blue]{self.network}[/blue], "
144149
f"chain_endpoint: [blue]{self.chain_endpoint}[/blue]> ..."
145150
)
146-
self.substrate = SubstrateInterface(
147-
url=self.chain_endpoint,
148-
ss58_format=SS58_FORMAT,
149-
type_registry=TYPE_REGISTRY,
150-
use_remote_preset=True,
151-
chain_name="Bittensor",
152-
_mock=_mock,
151+
self.substrate = self._get_substrate(
152+
fallback_chains=fallback_chains, retry_forever=retry_forever, _mock=_mock
153153
)
154154
if self.log_verbose:
155155
logging.info(
@@ -163,11 +163,41 @@ def __exit__(self, exc_type, exc_val, exc_tb):
163163
self.close()
164164

165165
def close(self):
166-
"""
167-
Closes the websocket connection
168-
"""
166+
"""Closes the websocket connection."""
169167
self.substrate.close()
170168

169+
def _get_substrate(
170+
self,
171+
fallback_chains: Optional[list[str]] = None,
172+
retry_forever: bool = False,
173+
_mock: bool = False,
174+
) -> Union[SubstrateInterface, RetrySyncSubstrate]:
175+
"""Creates the Substrate instance based on provided arguments.
176+
177+
Arguments:
178+
fallback_chains (list): List of fallback chains to use if no network is specified. Defaults to `None`.
179+
retry_forever (bool): Whether to retry forever on connection errors. Defaults to `False`.
180+
_mock: Whether this is a mock instance. Mainly just for use in testing.
181+
182+
Returns:
183+
the instance of the SubstrateInterface or RetrySyncSubstrate class.
184+
"""
185+
if fallback_chains or retry_forever:
186+
return RetrySyncSubstrate(
187+
url=self.chain_endpoint,
188+
fallback_chains=fallback_chains,
189+
retry_forever=retry_forever,
190+
_mock=_mock,
191+
)
192+
return SubstrateInterface(
193+
url=self.chain_endpoint,
194+
ss58_format=SS58_FORMAT,
195+
type_registry=TYPE_REGISTRY,
196+
use_remote_preset=True,
197+
chain_name="Bittensor",
198+
_mock=_mock,
199+
)
200+
171201
# Subtensor queries ===========================================================================================
172202

173203
def query_constant(

0 commit comments

Comments
 (0)