6
6
import numpy as np
7
7
import scalecodec
8
8
from async_substrate_interface .errors import SubstrateRequestException
9
+ from async_substrate_interface .substrate_addons import RetrySyncSubstrate
9
10
from async_substrate_interface .sync_substrate import SubstrateInterface
10
11
from async_substrate_interface .types import ScaleObj
11
12
from bittensor_drand import get_encrypted_commitment
@@ -116,17 +117,21 @@ def __init__(
116
117
self ,
117
118
network : Optional [str ] = None ,
118
119
config : Optional ["Config" ] = None ,
119
- _mock : bool = False ,
120
120
log_verbose : bool = False ,
121
+ fallback_chains : Optional [list [str ]] = None ,
122
+ retry_forever : bool = False ,
123
+ _mock : bool = False ,
121
124
):
122
125
"""
123
126
Initializes an instance of the Subtensor class.
124
127
125
128
Arguments:
126
129
network (str): The network name or type to connect to.
127
130
config (Optional[Config]): Configuration object for the AsyncSubtensor instance.
128
- _mock: Whether this is a mock instance. Mainly just for use in testing.
129
131
log_verbose (bool): Enables or disables verbose logging.
132
+ fallback_chains (list): List of fallback chains endpoints 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.
130
135
131
136
Raises:
132
137
Any exceptions raised during the setup, configuration, or connection process.
@@ -143,13 +148,8 @@ def __init__(
143
148
f"Connecting to network: [blue]{ self .network } [/blue], "
144
149
f"chain_endpoint: [blue]{ self .chain_endpoint } [/blue]> ..."
145
150
)
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
153
153
)
154
154
if self .log_verbose :
155
155
logging .info (
@@ -163,11 +163,45 @@ def __exit__(self, exc_type, exc_val, exc_tb):
163
163
self .close ()
164
164
165
165
def close (self ):
166
- """
167
- Closes the websocket connection
168
- """
166
+ """Closes the websocket connection."""
169
167
self .substrate .close ()
170
168
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 endpoints 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
+ ss58_format = SS58_FORMAT ,
189
+ type_registry = TYPE_REGISTRY ,
190
+ use_remote_preset = True ,
191
+ chain_name = "Bittensor" ,
192
+ fallback_chains = fallback_chains ,
193
+ retry_forever = retry_forever ,
194
+ _mock = _mock ,
195
+ )
196
+ return SubstrateInterface (
197
+ url = self .chain_endpoint ,
198
+ ss58_format = SS58_FORMAT ,
199
+ type_registry = TYPE_REGISTRY ,
200
+ use_remote_preset = True ,
201
+ chain_name = "Bittensor" ,
202
+ _mock = _mock ,
203
+ )
204
+
171
205
# Subtensor queries ===========================================================================================
172
206
173
207
def query_constant (
0 commit comments