|
| 1 | +import asyncio |
1 | 2 | import os
|
| 3 | +import time |
| 4 | + |
2 | 5 | import bittensor as bt
|
| 6 | +from bittensor.core.chain_data import decode_account_id |
| 7 | +from bittensor.core.settings import SS58_FORMAT |
3 | 8 | import pytest
|
| 9 | +import substrateinterface |
| 10 | + |
| 11 | +from async_substrate_interface.async_substrate import AsyncSubstrateInterface |
4 | 12 |
|
5 | 13 | try:
|
6 | 14 | n = int(os.getenv("NUMBER_RUNS"))
|
@@ -49,3 +57,68 @@ def test_sync():
|
49 | 57 | for i in range(n):
|
50 | 58 | s2 = st.get_stake_for_coldkey(coldkey, block=b_post + i)
|
51 | 59 | 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