Skip to content

Commit c29e11d

Browse files
authored
Bringing back lost methods for setting weights (#2412)
* add methods to fix set_weights extrinsic * replace `[color]...[/color]`
1 parent b3f7e85 commit c29e11d

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

bittensor/core/async_subtensor.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __init__(self, network: str = DEFAULT_NETWORK):
117117
self.network = network
118118
if network == "local":
119119
logging.warning(
120-
"[yellow]Warning[/yellow]: Verify your local subtensor is running on port 9944."
120+
"Warning: Verify your local subtensor is running on port 9944."
121121
)
122122
else:
123123
is_valid, _ = validate_chain_endpoint(network)
@@ -1271,19 +1271,19 @@ async def get_delegate_identities(
12711271

12721272
async def is_hotkey_registered(self, netuid: int, hotkey_ss58: str) -> bool:
12731273
"""Checks to see if the hotkey is registered on a given netuid"""
1274-
_result = await self.substrate.query(
1274+
result = await self.substrate.query(
12751275
module="SubtensorModule",
12761276
storage_function="Uids",
12771277
params=[netuid, hotkey_ss58],
12781278
)
1279-
if _result is not None:
1279+
if result is not None:
12801280
return True
12811281
else:
12821282
return False
12831283

12841284
async def get_uid_for_hotkey_on_subnet(
12851285
self, hotkey_ss58: str, netuid: int, block_hash: Optional[str] = None
1286-
):
1286+
) -> Optional[int]:
12871287
"""
12881288
Retrieves the unique identifier (UID) for a neuron's hotkey on a specific subnet.
12891289
@@ -1297,12 +1297,42 @@ async def get_uid_for_hotkey_on_subnet(
12971297
12981298
The UID is a critical identifier within the network, linking the neuron's hotkey to its operational and governance activities on a particular subnet.
12991299
"""
1300-
return self.substrate.query(
1300+
result = await self.substrate.query(
13011301
module="SubtensorModule",
13021302
storage_function="Uids",
13031303
params=[netuid, hotkey_ss58],
13041304
block_hash=block_hash,
13051305
)
1306+
return result
1307+
1308+
async def weights_rate_limit(self, netuid: int) -> Optional[int]:
1309+
"""
1310+
Returns network WeightsSetRateLimit hyperparameter.
1311+
1312+
Args:
1313+
netuid (int): The unique identifier of the subnetwork.
1314+
1315+
Returns:
1316+
Optional[int]: The value of the WeightsSetRateLimit hyperparameter, or ``None`` if the subnetwork does not exist or the parameter is not found.
1317+
"""
1318+
call = await self.get_hyperparameter(
1319+
param_name="WeightsSetRateLimit", netuid=netuid
1320+
)
1321+
return None if call is None else int(call)
1322+
1323+
async def blocks_since_last_update(self, netuid: int, uid: int) -> Optional[int]:
1324+
"""
1325+
Returns the number of blocks since the last update for a specific UID in the subnetwork.
1326+
1327+
Args:
1328+
netuid (int): The unique identifier of the subnetwork.
1329+
uid (int): The unique identifier of the neuron.
1330+
1331+
Returns:
1332+
Optional[int]: The number of blocks since the last update, or ``None`` if the subnetwork or UID does not exist.
1333+
"""
1334+
call = await self.get_hyperparameter(param_name="LastUpdate", netuid=netuid)
1335+
return None if call is None else await self.get_current_block() - int(call[uid])
13061336

13071337
# extrinsics
13081338

@@ -1445,12 +1475,15 @@ async def set_weights(
14451475
14461476
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】.
14471477
"""
1448-
uid = self.get_uid_for_hotkey_on_subnet(wallet.hotkey.ss58_address, netuid)
1478+
uid = await self.get_uid_for_hotkey_on_subnet(
1479+
wallet.hotkey.ss58_address, netuid
1480+
)
14491481
retries = 0
14501482
success = False
14511483
message = "No attempt made. Perhaps it is too soon to set weights!"
14521484
while (
1453-
self.blocks_since_last_update(netuid, uid) > self.weights_rate_limit(netuid) # type: ignore
1485+
await self.blocks_since_last_update(netuid, uid)
1486+
> await self.weights_rate_limit(netuid)
14541487
and retries < max_retries
14551488
):
14561489
try:

bittensor/core/extrinsics/async_transfer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async def do_transfer() -> tuple[bool, str, str]:
111111
# Validate destination address.
112112
if not is_valid_bittensor_address_or_public_key(destination):
113113
logging.error(
114-
f":cross_mark: <red>Invalid destination SS58 address</red>:[bold white]\n {destination}[/bold white]"
114+
f":cross_mark: <red>Invalid destination SS58 address</red>: {destination}"
115115
)
116116
return False
117117
logging.info(f"Initiating transfer on network: {subtensor.network}")

bittensor/utils/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ def validate_chain_endpoint(endpoint_url: str) -> tuple[bool, str]:
369369
parsed = urlparse(endpoint_url)
370370
if parsed.scheme not in ("ws", "wss"):
371371
return False, (
372-
f"Invalid URL or network name provided: [bright_cyan]({endpoint_url})[/bright_cyan].\n"
373-
"Allowed network names are [bright_cyan]finney, test, local[/bright_cyan]. "
374-
"Valid chain endpoints should use the scheme [bright_cyan]`ws` or `wss`[/bright_cyan].\n"
372+
f"Invalid URL or network name provided: ({endpoint_url}).\n"
373+
"Allowed network names are finney, test, local. "
374+
"Valid chain endpoints should use the scheme `ws` or `wss`.\n"
375375
)
376376
if not parsed.netloc:
377377
return False, "Invalid URL passed as the endpoint"

bittensor/utils/balance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ def __str__(self):
7272

7373
def __rich__(self):
7474
int_tao, fract_tao = format(float(self.tao), "f").split(".")
75-
return f"[green]{self.unit}[/green][green]{int_tao}[/green][green].[/green][dim green]{fract_tao}[/dim green]"
75+
return f"<green>{self.unit}{int_tao}.{fract_tao}</green>"
7676

7777
def __str_rao__(self):
7878
return f"{self.rao_unit}{int(self.rao)}"
7979

8080
def __rich_rao__(self):
81-
return f"[green]{self.rao_unit}{int(self.rao)}[/green]"
81+
return f"<green>{self.rao_unit}{int(self.rao)}</green>"
8282

8383
def __repr__(self):
8484
return self.__str__()

0 commit comments

Comments
 (0)