Skip to content

Commit 40b73db

Browse files
committed
Add tests
1 parent 6b84f81 commit 40b73db

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/test_old_new.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import asyncio
12
import os
3+
import time
4+
25
import bittensor as bt
6+
from bittensor.core.chain_data import decode_account_id
7+
from bittensor.core.settings import SS58_FORMAT
38
import pytest
9+
import substrateinterface
10+
11+
from async_substrate_interface.async_substrate import AsyncSubstrateInterface
412

513
try:
614
n = int(os.getenv("NUMBER_RUNS"))
@@ -49,3 +57,68 @@ def test_sync():
4957
for i in range(n):
5058
s2 = st.get_stake_for_coldkey(coldkey, block=b_post + i)
5159
print(f"at block {b_post + i}: {s2}")
60+
61+
62+
@pytest.mark.asyncio
63+
async def test_query_map():
64+
async def async_gathering():
65+
async def exhaust(qmr):
66+
r = []
67+
async for k, v in await qmr:
68+
r.append((k, v))
69+
return r
70+
71+
start = time.time()
72+
async with AsyncSubstrateInterface(
73+
"wss://entrypoint-finney.opentensor.ai:443", ss58_format=SS58_FORMAT
74+
) as substrate:
75+
block_hash = await substrate.get_chain_head()
76+
tasks = [
77+
substrate.query_map(
78+
"SubtensorModule",
79+
"TaoDividendsPerSubnet",
80+
[netuid],
81+
block_hash=block_hash,
82+
)
83+
for netuid in range(1, 51)
84+
]
85+
tasks = [exhaust(task) for task in tasks]
86+
print(time.time() - start)
87+
results_dicts_list = []
88+
for future in asyncio.as_completed(tasks):
89+
result = await future
90+
results_dicts_list.extend(
91+
[(decode_account_id(k), v.value) for k, v in result]
92+
)
93+
94+
elapsed = time.time() - start
95+
print(f"time elapsed: {elapsed}")
96+
97+
print("Async Results", len(results_dicts_list))
98+
return results_dicts_list, block_hash
99+
100+
def sync_old_method(block_hash):
101+
results_dicts_list = []
102+
start = time.time()
103+
substrate = substrateinterface.SubstrateInterface(
104+
"wss://entrypoint-finney.opentensor.ai:443"
105+
)
106+
for netuid in range(1, 51):
107+
tao_divs = list(
108+
substrate.query_map(
109+
"SubtensorModule",
110+
"TaoDividendsPerSubnet",
111+
[netuid],
112+
block_hash=block_hash,
113+
)
114+
)
115+
tao_divs = [(k.value, v.value) for k, v in tao_divs]
116+
results_dicts_list.extend(tao_divs)
117+
print(time.time() - start)
118+
print("Sync Results", len(results_dicts_list))
119+
return results_dicts_list
120+
121+
async_, block_hash_ = await async_gathering()
122+
sync_ = sync_old_method(block_hash_)
123+
for k_v in async_:
124+
assert k_v in sync_

0 commit comments

Comments
 (0)