Bittensor SDK

repository·master·Indexed 23 days ago

https://github.com/raofoundation/bittensor

An open-source platform for producing competitive digital commodities rewarded in TAO. The Bittensor SDK (v10.5.0) provides Python tools for interacting with the ecosystem, including writing subnet mechanisms, miners, and validators, as well as querying the subtensor blockchain. It includes a development test framework with the TestSubnet class for subnet orchestration and support for synchronous and asynchronous execution of setup steps.

Tokens
13.7K
Snippets
37
Records
72
Agent score
81%

What's inside bittensor

  1. Important notice regarding Bittensor SDK maintenance

    master

    DEPRECATION NOTICE

    This repository is archived and no longer maintained.

    Development has moved to the subtensor monorepo (sdk/python), which ships as Bittensor 11. Bittensor 11 is a single package containing the SDK, btcli, and the wallet. While existing v10 releases remain installable from PyPI, no further releases will be made from this repository.

  2. Handle structured extrinsic responses with ExtrinsicResponse

    master

    In Bittensor 10.0, functions that submit extrinsics now return an ExtrinsicResponse object instead of previous return types. This is a breaking change designed to provide standardized, rich data for all outgoing requests and incoming on-chain results.

    An ExtrinsicResponse includes:

    • Success flag: Indicates if the operation succeeded.
    • Message: A human-readable status message.
    • Network fee: The blockchain transaction fee.
    • Application-level swap fee(s): Fees related to token swaps (e.g., TAO to Alpha).
    • Receipts: Inclusion or finalization receipts.
    • Operation-specific data: Data unique to the specific extrinsic called.
  3. Follow naming conventions

    master

    Use the following naming patterns for Python code:

    • Classes: CamelCase.
    • Functions and Variables: snake_case (lowercase with underscores).
    • Constants: UPPER_CASE_WITH_UNDERSCORES (defined at module level).
    • Non-public Methods and Instance Variables: Use a single leading underscore (e.g., _internal_var).
    • Strongly "private" Methods and Variables: Use a double leading underscore (e.g., __private_var) to trigger Python name mangling.
  4. Understand Bittensor's Branching Model

    master

    Bittensor uses two primary branches to manage code lifecycle:

    • master: The live production branch. It is protected and should only be updated by the core development team.
    • staging: The active development branch. This is where all new features, fixes, and proposed changes are merged and tested before being promoted to production.

    Developers should perform most work on branches derived from staging and target staging for Pull Requests.

  5. Use the correct branch type for your changes

    master

    Depending on the nature of your work, use one of the following branching patterns:

    Branch TypeSource BranchMerge TargetNaming Convention
    Featurestagingstagingfeat/<ticket>/<descriptive-sentence>
    Fixstagingstagingfix/<ticket>/<descriptive-sentence>
    Releasestagingstaging & masterrelease/<version>/<descriptive-message>/<creator's-name>
    Hotfixmaster or stagingstaging & masterhotfix/<version>/<descriptive-message>/<creator's-name>

    Note on Hotfixes: If a release branch currently exists, merge hotfix changes into that release branch instead of staging. This ensures the fix is included in the upcoming release. If the fix is needed in staging immediately, you may merge into staging as well.

  6. Use mechid to support multiple incentive mechanisms

    master

    Bittensor v10.0 introduces support for multiple incentive mechanisms per subnet. Many methods that interact with weights, bonds, or metagraphs now require a mechid (mechanism ID) parameter to specify which mechanism to query.

    By default, mechid=0 refers to the first (or only) incentive mechanism in a subnet.

    Methods requiring mechid (parameter order: netuid, mechid=0, block=None):

    • subtensor.bonds(netuid, mechid=0, block=None)
    • subtensor.weights(netuid, mechid=0, block=None)
    • subtensor.metagraph(netuid, mechid=0, lite=True, block=None)
    • subtensor.get_metagraph_info(netuid, mechid=0, ...)
    • subtensor.get_timelocked_weight_commits(netuid, mechid=0, block=None)
    • subtensor.commit_weights(wallet, netuid, uids, weights, mechid=0, ...)
    • subtensor.reveal_weights(wallet, netuid, uids, weights, mechid=0, ...)
    • subtensor.set_weights(wallet, netuid, uids, weights, mechid=0, ...)

    Note on MetagraphInfo: The MetagraphInfo class now requires mechid as a mandatory parameter.

  7. Use multiple incentive mechanisms within subnets via mechid

    master

    Bittensor 10.0 supports multiple independent incentive mechanisms within a single subnet. Each mechanism has its own weight matrices, bond pools, and emission distributions.

    • mechid (Mechanism ID): An integer used to identify the mechanism. The first mechanism is 0, the second is 1, etc.
    • Default Behavior: All SDK methods default to mechid=0. Existing single-mechanism subnets remain backward compatible and require no changes.

    Setting Weights for a Specific Mechanism

    Validators must specify the mechid when setting weights to ensure they target the correct mechanism:

    # Set weights for a specific mechanism (mechid)
    response = subtensor.set_weights(
        wallet,
        netuid=1,
        uids=[0, 1, 2],
        weights=[0.5, 0.3, 0.2],
        mechid=0  # Mechanism ID (default: 0)
    )
    
    # For subnets with multiple mechanisms, set weights for each:
    mechanism1_response = subtensor.set_weights(wallet, netuid=1, uids, weights1, mechid=0)
    mechanism2_response = subtensor.set_weights(wallet, netuid=1, uids, weights2, mechid=1)

    Querying Mechanism-Specific Data

    All metagraph queries now accept a mechid parameter to retrieve data for a specific mechanism:

    # Get metagraph for specific mechanism
    metagraph = subtensor.metagraph(netuid=1, mechid=0)
    
    # Get weights for specific mechanism
    weights = subtensor.weights(netuid=1, mechid=0)
    
    # Get bonds for specific mechanism
    bonds = subtensor.bonds(netuid=1, mechid=0)
    
    # Get timelocked weight commits for a mechanism
    commits = subtensor.get_timelocked_weight_commits(netuid=1, mechid=0)
  8. Handle ExtrinsicResponse return types

    master

    In Bittensor 10.0, all functions that submit extrinsics to the Subtensor blockchain now return an ExtrinsicResponse object instead of a bool or tuple.

    Key fields in ExtrinsicResponse:

    • success: True if transaction succeeded, False otherwise.
    • message: User-friendly status string.
    • extrinsic_fee: Network fee paid to validators.
    • transaction_tao_fee: 0.05% TAO fee for swap-based staking.
    • transaction_alpha_fee: 0.05% Alpha fee for swap-based staking.
    • extrinsic_receipt: Contains block number, hash, and events (if wait_for_inclusion=True).
    • data: Operation-specific results (e.g., uid for registration, reveal_round for weight commits).
    • error: Python exception (if raise_error=False).

    Note: If raise_error=False, the function will not raise exceptions; you must check the success or error fields. If raise_error=True, exceptions are raised directly.

  9. Create and finish a release branch

    master

    Release branches prepare a new production release by allowing minor bug fixes and metadata updates (like version numbers).

    1. Create the branch from staging:
    git checkout -b release/3.4.0/descriptive-message/creators_name staging
    1. Update the version: Use the provided script to bump the version:
    ./scripts/update_version.sh major
    # OR
    ./scripts/update_version.sh minor
    1. Commit version changes:
    git commit -a -m "Updated version to 3.4.0"
    1. Finish the release (Merge to master and tag):
    git checkout master
    git merge --no-ff release/3.4.0/optional-descriptive-message
    git tag -a v3.4.0 -m "Releasing v3.4.0: some comment about it"
    git push origin master
    git push origin --tags
    1. Sync back to staging: To ensure any fixes made in the release branch are preserved, merge it back into staging:
    git checkout staging
    git merge --no-ff release/3.4.0/optional-descriptive-message
    git checkout -b release/3.4.0/descriptive-message/creators_name staging
    ./scripts/update_version.sh major|minor
    git commit -a -m "Updated version to 3.4.0"
  10. Follow the six rules of a great commit

    master

    To maintain a clean and understandable project history, follow these six rules for Git commits:

    1. Atomic Commits: Each commit should revolve around one single task or fix. Avoid joint commits for unrelated changes, but you may commit a layout file, its code-behind file, and associated resources together if they constitute a single complete feature/task.
    2. Separate subject from body with a blank line: Use a single line for simple changes. For complex changes, use a proper text editor to write a body that provides context, separated from the subject by one blank line.
    3. Limit the subject line to 50 characters: Keep subject lines concise. While not a hard limit, it ensures readability and prevents truncation in Git logs.
    4. Use the imperative mood in the subject line: Write the subject as if giving a command. A good test is: "If applied, this commit will <your subject line here>".
    5. Wrap the body at 72 characters: Manually wrap your commit body text at 72 characters to ensure it remains readable in various Git tools.
    6. Use the body to explain what and why vs. how: Focus the body on the reasoning behind the change (the context, the problem, and the solution) rather than describing the implementation details, which should be evident from the code itself.