Skip to content

Commit b3c7159

Browse files
author
Roman
committed
Update SubtensorApi
1 parent e85e269 commit b3c7159

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

bittensor/core/subtensor_api/__init__.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class SubtensorApi:
2424
Arguments:
2525
network: The network to connect to. Defaults to `None` -> `finney`.
2626
config: Bittensor configuration object. Defaults to `None`.
27-
log_verbose: If `True`, sets the subtensor to log verbosely. Defaults to `False`.
28-
async_subtensor: If `True`, uses the async subtensor to create the connection. Defaults to `False`.
2927
legacy_methods: If `True`, all methods from the Subtensor class will be added to the root level of this class.
28+
fallback_chains (list): List of fallback chains to use if no network is specified. Defaults to `None`.
29+
retry_forever (bool): Whether to retry forever on connection errors. Defaults to `False`.
30+
log_verbose (bool): Enables or disables verbose logging.
31+
mock: Whether this is a mock instance. Mainly just for use in testing.
3032
3133
Example:
3234
# sync version
@@ -57,16 +59,21 @@ def __init__(
5759
self,
5860
network: Optional[str] = None,
5961
config: Optional["Config"] = None,
60-
log_verbose: bool = False,
6162
async_subtensor: bool = False,
6263
legacy_methods: bool = False,
63-
_mock: bool = False,
64+
fallback_chains: Optional[list[str]] = None,
65+
retry_forever: bool = False,
66+
log_verbose: bool = False,
67+
mock: bool = False,
6468
):
6569
self.network = network
66-
self._mock = _mock
70+
self._fallback_chains = fallback_chains
71+
self._retry_forever = retry_forever
72+
self._mock = mock
6773
self.log_verbose = log_verbose
6874
self.is_async = async_subtensor
6975
self._config = config
76+
7077
# assigned only for async instance
7178
self.initialize = None
7279
self._subtensor = self._get_subtensor()
@@ -99,21 +106,29 @@ def _get_subtensor(self) -> Union["_Subtensor", "_AsyncSubtensor"]:
99106
_subtensor = _AsyncSubtensor(
100107
network=self.network,
101108
config=self._config,
102-
_mock=self._mock,
103109
log_verbose=self.log_verbose,
110+
fallback_chains=self._fallback_chains,
111+
retry_forever=self._retry_forever,
112+
_mock=self._mock,
104113
)
105114
self.initialize = _subtensor.initialize
106115
return _subtensor
107116
else:
108117
return _Subtensor(
109118
network=self.network,
110119
config=self._config,
111-
_mock=self._mock,
112120
log_verbose=self.log_verbose,
121+
fallback_chains=self._fallback_chains,
122+
retry_forever=self._retry_forever,
123+
_mock=self._mock,
113124
)
114125

115126
def __str__(self):
116-
return f"<Network: {self.network}, Chain: {self.chain_endpoint}, {'Async version' if self.is_async else 'Sync version'}>"
127+
return (
128+
f"<Network: {self.network}, "
129+
f"Chain: {self._determine_chain_endpoint()}, "
130+
f"{'Async version' if self.is_async else 'Sync version'}>"
131+
)
117132

118133
def __repr__(self):
119134
return self.__str__()
@@ -130,50 +145,68 @@ async def __aenter__(self):
130145
async def __aexit__(self, exc_type, exc_val, exc_tb):
131146
await self.substrate.close()
132147

148+
def _determine_chain_endpoint(self) -> str:
149+
"""Determines the connection and mock flag."""
150+
if self._mock:
151+
return "Mock"
152+
return self.substrate.url
153+
133154
@property
134155
def block(self):
156+
"""Returns current chain block number."""
135157
return self._subtensor.block
136158

137159
@property
138160
def chain(self):
161+
"""Property to access chain methods."""
139162
return _Chain(self._subtensor)
140163

141164
@property
142165
def commitments(self):
166+
"""Property to access commitments methods."""
143167
return _Commitments(self._subtensor)
144168

145169
@property
146170
def delegates(self):
171+
"""Property to access delegates methods."""
147172
return _Delegates(self._subtensor)
148173

149174
@property
150175
def extrinsics(self):
176+
"""Property to access extrinsics methods."""
151177
return _Extrinsics(self._subtensor)
152178

153179
@property
154180
def metagraphs(self):
181+
"""Property to access metagraphs methods."""
155182
return _Metagraphs(self._subtensor)
156183

157184
@property
158185
def neurons(self):
186+
"""Property to access neurons methods."""
159187
return self._neurons
160188

161189
@neurons.setter
162190
def neurons(self, value):
191+
"""Setter for neurons property."""
163192
self._neurons = value
164193

165194
@property
166195
def queries(self):
196+
"""Property to access queries methods."""
167197
return _Queries(self._subtensor)
168198

169199
@property
170200
def stakes(self):
201+
"""Property to access stakes methods."""
171202
return _Stakes(self._subtensor)
172203

173204
@property
174205
def subnets(self):
206+
"""Property to access subnets methods."""
175207
return _Subnets(self._subtensor)
176208

177209
@property
178210
def wallets(self):
211+
"""Property to access wallets methods."""
179212
return _Wallets(self._subtensor)

0 commit comments

Comments
 (0)