Skip to content

Conversation

thewhaleking
Copy link
Collaborator

@thewhaleking thewhaleking commented Mar 19, 2025

Now uses bt_decode.decode_list to decode the items in a query map.

Requires feat/list-decode from bt-decode

Depends on opentensor/bt-decode#19

Code example:

import asyncio
from async_substrate_interface.async_substrate import AsyncSubstrateInterface
from bittensor.core.chain_data import decode_account_id
from bittensor.core.settings import SS58_FORMAT
import time

import substrateinterface


async def fish():
    async def exhaust(qmr):
        r = []
        async for k, v in await qmr:
            r.append((k, v))
        return r

    start = time.time()
    async with AsyncSubstrateInterface("wss://entrypoint-finney.opentensor.ai:443",
                                       ss58_format=SS58_FORMAT) as substrate:
        block_hash = await substrate.get_chain_head()
        tasks = [
            substrate.query_map(
                "SubtensorModule",
                "TaoDividendsPerSubnet",
                [netuid],
                block_hash=block_hash
            ) for
            netuid in range(1, 51)
        ]
        tasks = [exhaust(task) for task in tasks]
        print(time.time() - start)
        results_dicts_list = []
        for future in asyncio.as_completed(tasks):
            result = await future
            results_dicts_list.extend([(decode_account_id(k), v.value) for k, v in result])

    elapsed = time.time() - start
    print(f"time elapsed: {elapsed}")

    print("Async Results", len(results_dicts_list))
    return results_dicts_list, block_hash


def fish_sync(block_hash):
    results_dicts_list = []
    start = time.time()
    substrate = substrateinterface.SubstrateInterface("wss://entrypoint-finney.opentensor.ai:443")
    for netuid in range(1, 51):
        tao_divs = list(substrate.query_map("SubtensorModule", "TaoDividendsPerSubnet",
                                            [netuid], block_hash=block_hash))
        tao_divs = [(k.value, v.value) for k, v in tao_divs]
        results_dicts_list.extend(tao_divs)
    print(time.time() - start)
    print("Sync Results", len(results_dicts_list))
    return results_dicts_list


if __name__ == '__main__':
    async_, block_hash = asyncio.run(fish())
    sync_ = fish_sync(block_hash)
    for i, k_v in enumerate(async_):
        assert k_v in sync_

@basfroman basfroman added run-bittensor-sdk-tests Runs Bittensor SDK tests. run-bittensor-cli-tests Runs BTCLI tests. labels Mar 21, 2025
@thewhaleking thewhaleking changed the title Experimental: multiprocessing for speeding up query map decodes. Improved Query Map Decodes Mar 31, 2025
@thewhaleking thewhaleking marked this pull request as ready for review March 31, 2025 20:48
@camfairchild
Copy link
Contributor

Depends on opentensor/bt-decode#19

@thewhaleking thewhaleking requested a review from a team April 4, 2025 12:38
@thewhaleking thewhaleking merged commit 8654fbc into staging Apr 4, 2025
37 checks passed
@thewhaleking thewhaleking deleted the experimental/thewhaleking/cache-decoding branch April 4, 2025 16:01
@ibraheem-abe ibraheem-abe mentioned this pull request Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-bittensor-cli-tests Runs BTCLI tests. run-bittensor-sdk-tests Runs Bittensor SDK tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants