Skip to content

Commit 5df2797

Browse files
author
Roman
committed
Merge branch 'staging' into feat/roman/selective_metagraph_back
2 parents a07eceb + 1b8d5c0 commit 5df2797

File tree

10 files changed

+269
-14
lines changed

10 files changed

+269
-14
lines changed

bittensor/core/async_subtensor.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def __init__(
121121
fallback_endpoints: Optional[list[str]] = None,
122122
retry_forever: bool = False,
123123
_mock: bool = False,
124+
archive_endpoints: Optional[list[str]] = None,
125+
websocket_shutdown_timer: float = 5.0,
124126
):
125127
"""
126128
Initializes an instance of the AsyncSubtensor class.
@@ -133,6 +135,9 @@ def __init__(
133135
Defaults to `None`.
134136
retry_forever: Whether to retry forever on connection errors. Defaults to `False`.
135137
_mock: Whether this is a mock instance. Mainly just for use in testing.
138+
archive_endpoints: Similar to fallback_endpoints, but specifically only archive nodes. Will be used in cases
139+
where you are requesting a block that is too old for your current (presumably lite) node. Defaults to
140+
`None`
136141
137142
Raises:
138143
Any exceptions raised during the setup, configuration, or connection process.
@@ -155,6 +160,8 @@ def __init__(
155160
fallback_endpoints=fallback_endpoints,
156161
retry_forever=retry_forever,
157162
_mock=_mock,
163+
archive_endpoints=archive_endpoints,
164+
ws_shutdown_timer=websocket_shutdown_timer,
158165
)
159166
if self.log_verbose:
160167
logging.info(
@@ -293,6 +300,8 @@ def _get_substrate(
293300
fallback_endpoints: Optional[list[str]] = None,
294301
retry_forever: bool = False,
295302
_mock: bool = False,
303+
archive_endpoints: Optional[list[str]] = None,
304+
ws_shutdown_timer: float = 5.0,
296305
) -> Union[AsyncSubstrateInterface, RetryAsyncSubstrate]:
297306
"""Creates the Substrate instance based on provided arguments.
298307
@@ -301,11 +310,16 @@ def _get_substrate(
301310
Defaults to `None`.
302311
retry_forever: Whether to retry forever on connection errors. Defaults to `False`.
303312
_mock: Whether this is a mock instance. Mainly just for use in testing.
313+
archive_endpoints: Similar to fallback_endpoints, but specifically only archive nodes. Will be used in cases
314+
where you are requesting a block that is too old for your current (presumably lite) node. Defaults to
315+
`None`
316+
ws_shutdown_timer: Amount of time, in seconds, to wait after the last response from the chain to close the
317+
connection.
304318
305319
Returns:
306320
the instance of the SubstrateInterface or RetrySyncSubstrate class.
307321
"""
308-
if fallback_endpoints or retry_forever:
322+
if fallback_endpoints or retry_forever or archive_endpoints:
309323
return RetryAsyncSubstrate(
310324
url=self.chain_endpoint,
311325
fallback_chains=fallback_endpoints,
@@ -315,6 +329,8 @@ def _get_substrate(
315329
use_remote_preset=True,
316330
chain_name="Bittensor",
317331
_mock=_mock,
332+
archive_nodes=archive_endpoints,
333+
ws_shutdown_timer=ws_shutdown_timer,
318334
)
319335
return AsyncSubstrateInterface(
320336
url=self.chain_endpoint,
@@ -323,6 +339,7 @@ def _get_substrate(
323339
use_remote_preset=True,
324340
chain_name="Bittensor",
325341
_mock=_mock,
342+
ws_shutdown_timer=ws_shutdown_timer,
326343
)
327344

328345
# Subtensor queries ===========================================================================================

bittensor/core/axon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ async def preprocess(self, request: "Request") -> "Synapse":
12671267
# Fills the local axon information into the synapse.
12681268
synapse.axon.__dict__.update(
12691269
{
1270-
"version": str(version_as_int),
1270+
"version": int(version_as_int),
12711271
"uuid": str(self.axon.uuid),
12721272
"nonce": time.time_ns(),
12731273
"status_code": 100,
@@ -1276,7 +1276,7 @@ async def preprocess(self, request: "Request") -> "Synapse":
12761276

12771277
# Fills the dendrite information into the synapse.
12781278
synapse.dendrite.__dict__.update(
1279-
{"port": str(request.client.port), "ip": str(request.client.host)} # type: ignore
1279+
{"port": int(request.client.port), "ip": str(request.client.host)} # type: ignore
12801280
)
12811281

12821282
# Signs the synapse from the axon side using the wallet hotkey.

bittensor/core/subtensor.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ class Subtensor(SubtensorMixin):
118118
def __init__(
119119
self,
120120
network: Optional[str] = None,
121-
config: Optional["Config"] = None,
121+
config: Optional[Config] = None,
122122
log_verbose: bool = False,
123123
fallback_endpoints: Optional[list[str]] = None,
124124
retry_forever: bool = False,
125125
_mock: bool = False,
126+
archive_endpoints: Optional[list[str]] = None,
126127
):
127128
"""
128129
Initializes an instance of the Subtensor class.
@@ -135,6 +136,9 @@ def __init__(
135136
Defaults to `None`.
136137
retry_forever: Whether to retry forever on connection errors. Defaults to `False`.
137138
_mock: Whether this is a mock instance. Mainly just for use in testing.
139+
archive_endpoints: Similar to fallback_endpoints, but specifically only archive nodes. Will be used in cases
140+
where you are requesting a block that is too old for your current (presumably lite) node. Defaults to
141+
`None`
138142
139143
Raises:
140144
Any exceptions raised during the setup, configuration, or connection process.
@@ -155,6 +159,7 @@ def __init__(
155159
fallback_endpoints=fallback_endpoints,
156160
retry_forever=retry_forever,
157161
_mock=_mock,
162+
archive_endpoints=archive_endpoints,
158163
)
159164
if self.log_verbose:
160165
logging.info(
@@ -176,19 +181,23 @@ def _get_substrate(
176181
fallback_endpoints: Optional[list[str]] = None,
177182
retry_forever: bool = False,
178183
_mock: bool = False,
184+
archive_endpoints: Optional[list[str]] = None,
179185
) -> Union[SubstrateInterface, RetrySyncSubstrate]:
180186
"""Creates the Substrate instance based on provided arguments.
181187
182188
Arguments:
183-
fallback_endpoints: List of fallback chains endpoints to use if main network isn't available.
184-
Defaults to `None`.
189+
fallback_endpoints: List of fallback chains endpoints to use if main network isn't available. Defaults to
190+
`None`.
185191
retry_forever: Whether to retry forever on connection errors. Defaults to `False`.
186192
_mock: Whether this is a mock instance. Mainly just for use in testing.
193+
archive_endpoints: Similar to fallback_endpoints, but specifically only archive nodes. Will be used in cases
194+
where you are requesting a block that is too old for your current (presumably lite) node. Defaults to
195+
`None`
187196
188197
Returns:
189198
the instance of the SubstrateInterface or RetrySyncSubstrate class.
190199
"""
191-
if fallback_endpoints or retry_forever:
200+
if fallback_endpoints or retry_forever or archive_endpoints:
192201
return RetrySyncSubstrate(
193202
url=self.chain_endpoint,
194203
ss58_format=SS58_FORMAT,
@@ -198,6 +207,7 @@ def _get_substrate(
198207
fallback_chains=fallback_endpoints,
199208
retry_forever=retry_forever,
200209
_mock=_mock,
210+
archive_nodes=archive_endpoints,
201211
)
202212
return SubstrateInterface(
203213
url=self.chain_endpoint,

bittensor/core/subtensor_api/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class SubtensorApi:
2929
retry_forever: Whether to retry forever on connection errors. Defaults to `False`.
3030
log_verbose: Enables or disables verbose logging.
3131
mock: Whether this is a mock instance. Mainly just for use in testing.
32+
archive_endpoints: Similar to fallback_endpoints, but specifically only archive nodes. Will be used in cases
33+
where you are requesting a block that is too old for your current (presumably lite) node. Defaults to `None`
34+
websocket_shutdown_timer: Amount of time, in seconds, to wait after the last response from the chain to close
35+
the connection. Only applicable to AsyncSubtensor.
3236
3337
Example:
3438
# sync version
@@ -75,10 +79,14 @@ def __init__(
7579
retry_forever: bool = False,
7680
log_verbose: bool = False,
7781
mock: bool = False,
82+
archive_endpoints: Optional[list[str]] = None,
83+
websocket_shutdown_timer: float = 5.0,
7884
):
7985
self.network = network
8086
self._fallback_endpoints = fallback_endpoints
87+
self._archive_endpoints = archive_endpoints
8188
self._retry_forever = retry_forever
89+
self._ws_shutdown_timer = websocket_shutdown_timer
8290
self._mock = mock
8391
self.log_verbose = log_verbose
8492
self.is_async = async_subtensor
@@ -119,6 +127,8 @@ def _get_subtensor(self) -> Union["_Subtensor", "_AsyncSubtensor"]:
119127
fallback_endpoints=self._fallback_endpoints,
120128
retry_forever=self._retry_forever,
121129
_mock=self._mock,
130+
archive_endpoints=self._archive_endpoints,
131+
websocket_shutdown_timer=self._ws_shutdown_timer,
122132
)
123133
self.initialize = _subtensor.initialize
124134
return _subtensor
@@ -130,6 +140,7 @@ def _get_subtensor(self) -> Union["_Subtensor", "_AsyncSubtensor"]:
130140
fallback_endpoints=self._fallback_endpoints,
131141
retry_forever=self._retry_forever,
132142
_mock=self._mock,
143+
archive_endpoints=self._archive_endpoints,
133144
)
134145

135146
def _determine_chain_endpoint(self) -> str:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies = [
3636
"uvicorn",
3737
"bittensor-drand>=0.5.0",
3838
"bittensor-wallet>=3.0.8",
39-
"async-substrate-interface>=1.2.0"
39+
"async-substrate-interface>=1.3.1"
4040
]
4141

4242
[project.optional-dependencies]

tests/e2e_tests/test_staking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
409409
wait_for_inclusion=True,
410410
wait_for_finalization=True,
411411
safe_staking=True,
412-
rate_tolerance=0.1, # 10%
412+
rate_tolerance=0.22, # 22%
413413
allow_partial_stake=False,
414414
)
415415
assert success is True
@@ -426,7 +426,7 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
426426
alice_wallet,
427427
bob_wallet.hotkey.ss58_address,
428428
netuid=alice_subnet_netuid,
429-
amount=stake_amount,
429+
amount=full_stake,
430430
wait_for_inclusion=True,
431431
wait_for_finalization=True,
432432
safe_staking=True,

tests/helpers/helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import itertools
23
import json
34
import time
45
from collections import deque
@@ -204,6 +205,9 @@ def recv(self, *args, **kwargs):
204205
response = WEBSOCKET_RESPONSES[self.seed][item["method"]][
205206
json.dumps(item["params"])
206207
]
208+
if isinstance(response, itertools.cycle):
209+
# Allows us to cycle through different responses for the same method/params combo
210+
response = next(response)
207211
response["id"] = _id
208212
return json.dumps(response)
209213
except (KeyError, TypeError):

0 commit comments

Comments
 (0)