Skip to content

Commit

Permalink
Release/8.5.2 (#2584)
Browse files Browse the repository at this point in the history
* Updates tests for btwallet 3.0.0

* fix get_delegates result decoding

* Feat/use tx pool for set weights (#2534)

* use the tx pool for setting/committing weights

* add supports rpc method with cache

* handle when state call is not possible (unlikely)

* increment next index by one for correct nonce

* use for set sync set weights also

* oops, define in subtensor for sync methods

* make tests less specific

* try e2e test for commit weights

* modify comments on test

* add root set test helper

* try upping submit timeout

* add set weights e2e test

* use high timeout on tests

* add awaits and log netuid

* chore: ruff

* use sudo_set_admin_utils instead

* don't increment next index for nonce

* dont await sudo_set_admin_utils

* lower test ext timeout to 12

* enable reg during test

* use set hyp values instead

* sleep after setting reg allowed

* fix test_set_weights_uses_next_nonce

* Use asyncstdlib for lru_cache of `AsyncSubstrateInterface.supports_rpc_method`

* Increases sleep for txs in test_set_weights_uses_next_nonce

---------

Co-authored-by: Benjamin Himes <[email protected]>
Co-authored-by: ibraheem-opentensor <[email protected]>
Co-authored-by: Benjamin Himes <[email protected]>

* fix tests

* add connection limit error handler

* add test

* Backmerge master to staging post 851 (#2557)

Bumps version and changelog

* improve handler

* add async commit reveal impl

* add new logic to async_subtensor

* add unit tests

* ruff

* add uid check before processing

* fix test

* Use apt-get instead of apt for scripts (#2571)

apt --> apt-get

* fix _do_stake incorrect arguments error in staking.py

add_stake sdk (after pybytes error is fixed in delegate_info.py) another error occurs, because _do_stake is incorrectly called on line 124 in staking.py returning an invalid arguments error, must add self (subtensor) param to fix this error.

* Updates subtensor branch for e2e

* tests fix

* Bumps cr3 ffi

* Bumps version and changelog

* Updates changelog

---------

Co-authored-by: Roman <[email protected]>
Co-authored-by: Cameron Fairchild <[email protected]>
Co-authored-by: Benjamin Himes <[email protected]>
Co-authored-by: Benjamin Himes <[email protected]>
Co-authored-by: Roman <[email protected]>
Co-authored-by: Assh-codes <[email protected]>
  • Loading branch information
7 people authored Jan 17, 2025
1 parent cd2ccc2 commit 313521d
Show file tree
Hide file tree
Showing 27 changed files with 1,121 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-subtensor-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:

- name: Setup subtensor repo
working-directory: ${{ github.workspace }}/subtensor
run: git checkout testnet
run: git checkout main

- name: Run tests
run: |
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 8.5.2 /2025-01-17

## What's Changed
* Feat/use tx pool for set weights by @camfairchild in https://github.com/opentensor/bittensor/pull/2534
* fix get_delegates result decoding by @roman-opentensor in https://github.com/opentensor/bittensor/pull/2551
* [SDK] Handle server connection limit by @roman-opentensor in https://github.com/opentensor/bittensor/pull/2553
* Backmerge master to staging post 851 by @ibraheem-opentensor in https://github.com/opentensor/bittensor/pull/2557
* [SDK] Improve InvalidStatus handler by @roman-opentensor in https://github.com/opentensor/bittensor/pull/2558
* [SDK] Add async version of commit reveal v3 by @roman-opentensor in https://github.com/opentensor/bittensor/pull/2560
* Use apt-get instead of apt for scripts by @camfairchild in https://github.com/opentensor/bittensor/pull/2571
* fix _do_stake incorrect arguments error in staking.py by @Assh-codes in https://github.com/opentensor/bittensor/pull/2574
* Updates tests for btwallet 3.0.0 by @ibraheem-opentensor in https://github.com/opentensor/bittensor/pull/2540
* Bumps cr3 FFI by @ibraheem-opentensor in https://github.com/opentensor/bittensor/pull/2583

## New Contributors
* @Assh-codes made their first contribution in https://github.com/opentensor/bittensor/pull/2574

**Full Changelog**: https://github.com/opentensor/bittensor/compare/v8.5.1...v8.5.2

## 8.5.1 /2024-12-16

## What's Changed
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ LABEL bittensor.image.authors="bittensor.com" \
ARG DEBIAN_FRONTEND=noninteractive

# Update the base image
RUN apt update && apt upgrade -y
RUN apt-get update && apt-get upgrade -y
# Install bittensor
## Install dependencies
RUN apt install -y curl sudo nano git htop netcat-openbsd wget unzip tmux apt-utils cmake build-essential
RUN apt-get install -y curl sudo nano git htop netcat-openbsd wget unzip tmux apt-utils cmake build-essential
## Upgrade pip
RUN pip3 install --upgrade pip

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.5.1
8.5.2
43 changes: 35 additions & 8 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
SubnetHyperparameters,
decode_account_id,
)
from bittensor.core.extrinsics.async_commit_reveal import commit_reveal_v3_extrinsic
from bittensor.core.extrinsics.async_registration import register_extrinsic
from bittensor.core.extrinsics.async_root import (
set_root_weights_extrinsic,
Expand Down Expand Up @@ -1596,18 +1597,44 @@ async def set_weights(
This function is crucial in shaping the network's collective intelligence, where each neuron's learning and contribution are influenced by the weights it sets towards others【81†source】.
"""
retries = 0
success = False
if (
uid := await self.get_uid_for_hotkey_on_subnet(
wallet.hotkey.ss58_address, netuid
)
) is None:
return (
False,
f"Hotkey {wallet.hotkey.ss58_address} not registered in subnet {netuid}",
)

if (await self.commit_reveal_enabled(netuid=netuid)) is True:
# go with `commit reveal v3` extrinsic
raise NotImplementedError(
"Not implemented yet for AsyncSubtensor. Coming soon."
)
message = "No attempt made. Perhaps it is too soon to commit weights!"
while (
await self.blocks_since_last_update(netuid, uid)
> await self.weights_rate_limit(netuid)
and retries < max_retries
and success is False
):
logging.info(
f"Committing weights for subnet #{netuid}. Attempt {retries + 1} of {max_retries}."
)
success, message = await commit_reveal_v3_extrinsic(
subtensor=self,
wallet=wallet,
netuid=netuid,
uids=uids,
weights=weights,
version_key=version_key,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)
retries += 1
return success, message
else:
# go with classic `set weights extrinsic`
uid = await self.get_uid_for_hotkey_on_subnet(
wallet.hotkey.ss58_address, netuid
)
retries = 0
success = False
message = "No attempt made. Perhaps it is too soon to set weights!"
while (
retries < max_retries
Expand Down
152 changes: 152 additions & 0 deletions bittensor/core/extrinsics/async_commit_reveal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
from typing import Optional, Union, TYPE_CHECKING

import numpy as np
from bittensor_commit_reveal import get_encrypted_commit
from numpy.typing import NDArray

from bittensor.core.settings import version_as_int
from bittensor.utils import format_error_message
from bittensor.utils.btlogging import logging
from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit

if TYPE_CHECKING:
from bittensor_wallet import Wallet
from bittensor.core.async_subtensor import AsyncSubtensor
from bittensor.utils.registration import torch


async def _do_commit_reveal_v3(
subtensor: "AsyncSubtensor",
wallet: "Wallet",
netuid: int,
commit: bytes,
reveal_round: int,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
) -> tuple[bool, Optional[str]]:
"""
Executes the commit-reveal phase 3 for a given netuid and commit, and optionally waits for extrinsic inclusion or finalization.
Arguments:
subtensor: An instance of the Subtensor class.
wallet: Wallet An instance of the Wallet class containing the user's keypair.
netuid: int The network unique identifier.
commit bytes The commit data in bytes format.
reveal_round: int The round number for the reveal phase.
wait_for_inclusion: bool, optional Flag indicating whether to wait for the extrinsic to be included in a block.
wait_for_finalization: bool, optional Flag indicating whether to wait for the extrinsic to be finalized.
Returns:
A tuple where the first element is a boolean indicating success or failure, and the second element is an optional string containing error message if any.
"""
logging.info(
f"Committing weights hash [blue]{commit.hex()}[/blue] for subnet #[blue]{netuid}[/blue] with "
f"reveal round [blue]{reveal_round}[/blue]..."
)

call = await subtensor.substrate.compose_call(
call_module="SubtensorModule",
call_function="commit_crv3_weights",
call_params={
"netuid": netuid,
"commit": commit,
"reveal_round": reveal_round,
},
)
extrinsic = await subtensor.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
)

response = await subtensor.substrate.submit_extrinsic(
subtensor=subtensor,
extrinsic=extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

if not wait_for_finalization and not wait_for_inclusion:
return True, "Not waiting for finalization or inclusion."

if await response.is_success:
return True, None

return False, format_error_message(await response.error_message)


async def commit_reveal_v3_extrinsic(
subtensor: "AsyncSubtensor",
wallet: "Wallet",
netuid: int,
uids: Union[NDArray[np.int64], "torch.LongTensor", list],
weights: Union[NDArray[np.float32], "torch.FloatTensor", list],
version_key: int = version_as_int,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
) -> tuple[bool, str]:
"""
Commits and reveals weights for given subtensor and wallet with provided uids and weights.
Arguments:
subtensor: The Subtensor instance.
wallet: The wallet to use for committing and revealing.
netuid: The id of the network.
uids: The uids to commit.
weights: The weights associated with the uids.
version_key: The version key to use for committing and revealing. Default is version_as_int.
wait_for_inclusion: Whether to wait for the inclusion of the transaction. Default is False.
wait_for_finalization: Whether to wait for the finalization of the transaction. Default is False.
Returns:
tuple[bool, str]: A tuple where the first element is a boolean indicating success or failure, and the second element is a message associated with the result.
"""
try:
# Convert uids and weights
if isinstance(uids, list):
uids = np.array(uids, dtype=np.int64)
if isinstance(weights, list):
weights = np.array(weights, dtype=np.float32)

# Reformat and normalize.
uids, weights = convert_weights_and_uids_for_emit(uids, weights)

current_block = await subtensor.substrate.get_block_number(None)
subnet_hyperparameters = await subtensor.get_subnet_hyperparameters(netuid)
tempo = subnet_hyperparameters.tempo
subnet_reveal_period_epochs = (
subnet_hyperparameters.commit_reveal_weights_interval
)

# Encrypt `commit_hash` with t-lock and `get reveal_round`
commit_for_reveal, reveal_round = get_encrypted_commit(
uids=uids,
weights=weights,
version_key=version_key,
tempo=tempo,
current_block=current_block,
netuid=netuid,
subnet_reveal_period_epochs=subnet_reveal_period_epochs,
)

success, message = await _do_commit_reveal_v3(
subtensor=subtensor,
wallet=wallet,
netuid=netuid,
commit=commit_for_reveal,
reveal_round=reveal_round,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

if success is not True:
logging.error(message)
return False, message

logging.success(
f"[green]Finalized![/green] Weights commited with reveal round [blue]{reveal_round}[/blue]."
)
return True, f"reveal_round:{reveal_round}"

except Exception as e:
logging.error(f":cross_mark: [red]Failed. Error:[/red] {e}")
return False, str(e)
12 changes: 12 additions & 0 deletions bittensor/core/extrinsics/async_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ async def _do_set_weights(
"version_key": version_key,
},
)

next_nonce = await subtensor.substrate.get_account_next_index(
wallet.hotkey.ss58_address
)

# Period dictates how long the extrinsic will stay as part of waiting pool
extrinsic = await subtensor.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
era={"period": 5},
nonce=next_nonce,
)
response = await subtensor.substrate.submit_extrinsic(
extrinsic,
Expand Down Expand Up @@ -180,9 +186,15 @@ async def _do_commit_weights(
"commit_hash": commit_hash,
},
)

next_nonce = await subtensor.substrate.get_account_next_index(
wallet.hotkey.ss58_address
)

extrinsic = await subtensor.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
nonce=next_nonce,
)
response = await subtensor.substrate.submit_extrinsic(
substrate=subtensor.substrate,
Expand Down
2 changes: 2 additions & 0 deletions bittensor/core/extrinsics/commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ def do_commit_weights(
"commit_hash": commit_hash,
},
)
next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address)
extrinsic = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
nonce=next_nonce,
)
response = submit_extrinsic(
subtensor=self,
Expand Down
2 changes: 2 additions & 0 deletions bittensor/core/extrinsics/set_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ def do_set_weights(
"version_key": version_key,
},
)
next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address)
# Period dictates how long the extrinsic will stay as part of waiting pool
extrinsic = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
era={"period": period},
nonce=next_nonce,
)
response = submit_extrinsic(
self,
Expand Down
1 change: 1 addition & 0 deletions bittensor/core/extrinsics/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __do_add_stake_single(
raise NotDelegateError("Hotkey: {} is not a delegate.".format(hotkey_ss58))

success = _do_stake(
self=subtensor,
wallet=wallet,
hotkey_ss58=hotkey_ss58,
amount=amount,
Expand Down
2 changes: 1 addition & 1 deletion bittensor/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

__version__ = "8.5.1"
__version__ = "8.5.2"

import os
import re
Expand Down
Loading

0 comments on commit 313521d

Please sign in to comment.