Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug in the get_subnets function #2240

Open
wants to merge 21 commits into
base: staging
Choose a base branch
from

Conversation

niljub
Copy link

@niljub niljub commented Aug 15, 2024

Bug

The get_subnets function was truncating to max. 100 results due to list comprehensions handling of iterators. The bug has been described in #2208.

Description of the Change

Replaced list comprehension with a manual iteration to ensure all subnet UIDs are retrieved, addressing the issue of truncated results.

Alternate Designs

Considered increasing fetch limits but chose manual iteration for more reliable results.

Possible Drawbacks

No significant drawbacks, just a minor increase in code complexity.

Verification Process

Added a test to check the length of the result list, ensuring all records are processed correctly. Ran all existing tests to confirm no regressions.

Release Notes

Fixed an issue in get_subnets where the result list was being truncated, ensuring complete retrieval of subnets.

ibraheem-opentensor and others added 6 commits August 14, 2024 09:22
Fixed a bug in the get_subnets function where list comprehension caused truncation of results. Replaced it with a manual iteration to ensure all subnets are retrieved.
"""Test get_subnets returns a list of the correct length."""
# Prep
block = 123
num_records = 50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this test for length > 100?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right—I missed a zero. I've added it and pushed a changed version along with the updated test, but the test still doesn't catch the issue. I also updated get_subnets again and verified it by running your snippet.

import bittensor

sub = bittensor.subtensor("wss://test.chain.opentensor.ai:443")
subnets = sub.get_subnets()
n_subnets = sub.get_total_subnets()

len(subnets) == n_subnets
# True; Expected: True

@camfairchild
Copy link
Collaborator

camfairchild commented Aug 19, 2024

Thanks for the contribution!
How was this fix tested?
The issue can be reproduced on the testnet and I don't believe this fixes it.

See example:

import bittensor
subtensor = bittensor.subtensor(network="wss://test.chain.opentensor.ai:443")
s = subtensor.get_subnets()
len(s)
# 100; Expected > 100

I would consider using the paging functionality of pysub-interface

"Fixed a bug in the get_subnets function where pagination led to truncated results. The function now iterates through the entire result set, loading all items into memory without pagination issues.
@niljub niljub force-pushed the hotfix/7.3.0/get-subnets-fix/niljub branch from a9cb336 to ad1c35a Compare August 20, 2024 09:05
@niljub
Copy link
Author

niljub commented Aug 20, 2024

Thanks for the contribution! How was this fix tested?

I initially tested the fix using your snippet from the bug report issue. However, during the cleanup process, I introduced an issue that wasn't caught even with 500 networks. I've now pushed a working version and thoroughly tested it with the snippet, though the solution is a bit more barebones.

I would consider using the paging functionality of pysub-interface.

I can certainly rewrite the function to use pysub-interface. However, this would also require modifying the query_map_subtensor function and incorporating a start_key. This way, we can leverage the substrate to retrieve results page by page as QueryMapResults. If this approach is preferred, let me know and I will implement it.

@thewhaleking
Copy link
Contributor

Hi. Taking a look at this today.

Copy link
Contributor

@thewhaleking thewhaleking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great feature to add. Thanks!

But the logic used in the test is bad. You can see that the old function logic passes the new test:
Screenshot 2024-08-28 at 19 20 40

TL;DR: improve the tests please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants