Skip to content

Commit 3e8b103

Browse files
author
Roman
committed
Merge branch 'staging' into feat/roman/selective_metagraph_back
2 parents 5df2797 + e46bb90 commit 3e8b103

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

bittensor/core/async_subtensor.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,7 @@ async def set_reveal_commitment(
30953095
async def subnet(
30963096
self,
30973097
netuid: int,
3098-
block: int = None,
3098+
block: Optional[int] = None,
30993099
block_hash: Optional[str] = None,
31003100
reuse_block: bool = False,
31013101
) -> Optional[DynamicInfo]:
@@ -3105,23 +3105,26 @@ async def subnet(
31053105
Args:
31063106
netuid (int): The unique identifier of the subnet.
31073107
block (Optional[int]): The block number to get the subnets at.
3108-
block_hash (str): The hash of the blockchain block number for the query.
3108+
block_hash (Optional[str]): The hash of the blockchain block number for the query.
31093109
reuse_block (bool): Whether to reuse the last-used blockchain block hash.
31103110
31113111
Returns:
31123112
Optional[DynamicInfo]: A DynamicInfo object, containing detailed information about a subnet.
31133113
"""
31143114
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
3115+
31153116
if not block_hash and reuse_block:
31163117
block_hash = self.substrate.last_block_hash
3118+
31173119
query = await self.substrate.runtime_call(
31183120
"SubnetInfoRuntimeApi",
31193121
"get_dynamic_info",
31203122
params=[netuid],
31213123
block_hash=block_hash,
31223124
)
3123-
subnet = DynamicInfo.from_dict(query.decode())
3124-
return subnet
3125+
3126+
if isinstance(decoded := query.decode(), dict):
3127+
return DynamicInfo.from_dict(decoded)
31253128

31263129
async def subnet_exists(
31273130
self,
@@ -3270,6 +3273,7 @@ async def handler(block_data: dict):
32703273

32713274
current_block = await self.substrate.get_block()
32723275
current_block_hash = current_block.get("header", {}).get("hash")
3276+
32733277
if block is not None:
32743278
target_block = block
32753279
else:
@@ -3367,15 +3371,14 @@ async def get_timestamp(
33673371
Returns:
33683372
datetime object for the timestamp of the block
33693373
"""
3370-
unix = (
3371-
await self.query_module(
3372-
"Timestamp",
3373-
"Now",
3374-
block=block,
3375-
block_hash=block_hash,
3376-
reuse_block=reuse_block,
3377-
)
3378-
).value
3374+
res = await self.query_module(
3375+
"Timestamp",
3376+
"Now",
3377+
block=block,
3378+
block_hash=block_hash,
3379+
reuse_block=reuse_block,
3380+
)
3381+
unix = res.value
33793382
return datetime.fromtimestamp(unix / 1000, tz=timezone.utc)
33803383

33813384
async def get_subnet_owner_hotkey(

bittensor/core/chain_data/info_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from bittensor.core.errors import SubstrateRequestException
55

6+
# NOTE: once Python 3.10+ is required, we can use `typing.Self` instead of this for better ide integration and type hinting.
7+
# This current generic does not play so nice with the inherited type hinting.
68
T = TypeVar("T", bound="InfoBase")
79

810

tests/e2e_tests/test_staking.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from bittensor.core.chain_data.stake_info import StakeInfo
33
from bittensor.utils.balance import Balance
44
from tests.e2e_tests.utils.chain_interactions import get_dynamic_balance
5-
from tests.helpers.helpers import ApproxBalance
65
from tests.e2e_tests.utils.e2e_test_utils import wait_to_start_call
76

87

@@ -173,7 +172,7 @@ def test_single_operation(subtensor, alice_wallet, bob_wallet):
173172

174173
# all balances have been unstaked
175174
assert stake == Balance(0).set_unit(alice_subnet_netuid)
176-
logging.console.success(f"✅ Test [green]test_single_operation[/green] passed")
175+
logging.console.success("✅ Test [green]test_single_operation[/green] passed")
177176

178177

179178
def test_batch_operations(subtensor, alice_wallet, bob_wallet):
@@ -216,7 +215,7 @@ def test_batch_operations(subtensor, alice_wallet, bob_wallet):
216215
netuid=netuid,
217216
)
218217

219-
assert stake == Balance(0), f"netuid={netuid} stake={stake}"
218+
assert stake == Balance(0).set_unit(netuid), f"netuid={netuid} stake={stake}"
220219

221220
balances = subtensor.get_balances(
222221
alice_wallet.coldkey.ss58_address,
@@ -255,7 +254,7 @@ def test_batch_operations(subtensor, alice_wallet, bob_wallet):
255254
]
256255

257256
for netuid, stake in zip(netuids, stakes):
258-
assert stake > Balance(0), f"netuid={netuid} stake={stake}"
257+
assert stake > Balance(0).set_unit(netuid), f"netuid={netuid} stake={stake}"
259258

260259
alice_balance -= len(netuids) * Balance.from_tao(10_000)
261260

@@ -308,7 +307,7 @@ def test_batch_operations(subtensor, alice_wallet, bob_wallet):
308307
assert balances == expected_balances
309308

310309
assert balances[alice_wallet.coldkey.ss58_address] > alice_balance
311-
logging.console.success(f"✅ Test [green]test_batch_operations[/green] passed")
310+
logging.console.success("✅ Test [green]test_batch_operations[/green] passed")
312311

313312

314313
def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
@@ -349,7 +348,7 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
349348
bob_wallet.hotkey.ss58_address,
350349
netuid=alice_subnet_netuid,
351350
)
352-
assert initial_stake == Balance(0)
351+
assert initial_stake == Balance(0).set_unit(alice_subnet_netuid)
353352

354353
# Test Staking Scenarios
355354
stake_amount = Balance.from_tao(100)
@@ -373,7 +372,9 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
373372
bob_wallet.hotkey.ss58_address,
374373
netuid=alice_subnet_netuid,
375374
)
376-
assert current_stake == Balance(0), "Stake should not change after failed attempt"
375+
assert current_stake == Balance(0).set_unit(alice_subnet_netuid), (
376+
"Stake should not change after failed attempt"
377+
)
377378

378379
# 2. Partial allowed - should succeed partially
379380
success = subtensor.add_stake(
@@ -394,7 +395,9 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
394395
bob_wallet.hotkey.ss58_address,
395396
netuid=alice_subnet_netuid,
396397
)
397-
assert partial_stake > Balance(0), "Partial stake should be added"
398+
assert partial_stake > Balance(0).set_unit(alice_subnet_netuid), (
399+
"Partial stake should be added"
400+
)
398401
assert partial_stake < stake_amount, (
399402
"Partial stake should be less than requested amount"
400403
)
@@ -468,7 +471,9 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
468471
netuid=alice_subnet_netuid,
469472
)
470473
logging.console.info(f"[orange]Partial unstake: {partial_unstake}[orange]")
471-
assert partial_unstake > Balance(0), "Some stake should remain"
474+
assert partial_unstake > Balance(0).set_unit(alice_subnet_netuid), (
475+
"Some stake should remain"
476+
)
472477

473478
# 3. Higher threshold - should succeed fully
474479
success = subtensor.unstake(
@@ -483,9 +488,7 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
483488
allow_partial_stake=False,
484489
)
485490
assert success is True, "Unstake should succeed"
486-
logging.console.success(
487-
f"✅ Test [green]test_safe_staking_scenarios[/green] passed"
488-
)
491+
logging.console.success("✅ Test [green]test_safe_staking_scenarios[/green] passed")
489492

490493

491494
def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
@@ -539,7 +542,9 @@ def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
539542
alice_wallet.hotkey.ss58_address,
540543
netuid=origin_netuid,
541544
)
542-
assert origin_stake > Balance(0), "Origin stake should be non-zero"
545+
assert origin_stake > Balance(0).set_unit(origin_netuid), (
546+
"Origin stake should be non-zero"
547+
)
543548

544549
stake_swap_amount = Balance.from_tao(10_000)
545550
# 1. Try swap with strict threshold and big amount- should fail
@@ -563,7 +568,7 @@ def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
563568
alice_wallet.hotkey.ss58_address,
564569
netuid=dest_netuid,
565570
)
566-
assert dest_stake == Balance(0), (
571+
assert dest_stake == Balance(0).set_unit(dest_netuid), (
567572
"Destination stake should remain 0 after failed swap"
568573
)
569574

@@ -584,7 +589,7 @@ def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
584589
assert success is True
585590

586591
# Verify stake was moved
587-
origin_stake = subtensor.get_stake(
592+
origin_stake = subtensor.get_stake( # TODO this seems unused
588593
alice_wallet.coldkey.ss58_address,
589594
alice_wallet.hotkey.ss58_address,
590595
netuid=origin_netuid,
@@ -594,11 +599,11 @@ def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
594599
alice_wallet.hotkey.ss58_address,
595600
netuid=dest_netuid,
596601
)
597-
assert dest_stake > Balance(0), (
602+
assert dest_stake > Balance(0).set_unit(dest_netuid), (
598603
"Destination stake should be non-zero after successful swap"
599604
)
600605
logging.console.success(
601-
f"✅ Test [green]test_safe_swap_stake_scenarios[/green] passed"
606+
"✅ Test [green]test_safe_swap_stake_scenarios[/green] passed"
602607
)
603608

604609

@@ -641,7 +646,7 @@ def test_move_stake(subtensor, alice_wallet, bob_wallet):
641646
coldkey_ss58=alice_wallet.coldkey.ss58_address,
642647
netuid=alice_subnet_netuid,
643648
stake=get_dynamic_balance(stakes[0].stake.rao, alice_subnet_netuid),
644-
locked=Balance(0),
649+
locked=Balance(0).set_unit(alice_subnet_netuid),
645650
emission=get_dynamic_balance(stakes[0].emission.rao, alice_subnet_netuid),
646651
drain=0,
647652
is_registered=True,
@@ -704,7 +709,7 @@ def test_move_stake(subtensor, alice_wallet, bob_wallet):
704709
expected_stakes += fast_block_stake
705710

706711
assert stakes == expected_stakes
707-
logging.console.success(f"✅ Test [green]test_move_stake[/green] passed")
712+
logging.console.success("✅ Test [green]test_move_stake[/green] passed")
708713

709714

710715
def test_transfer_stake(subtensor, alice_wallet, bob_wallet, dave_wallet):
@@ -746,7 +751,7 @@ def test_transfer_stake(subtensor, alice_wallet, bob_wallet, dave_wallet):
746751
coldkey_ss58=alice_wallet.coldkey.ss58_address,
747752
netuid=alice_subnet_netuid,
748753
stake=get_dynamic_balance(alice_stakes[0].stake.rao, alice_subnet_netuid),
749-
locked=Balance(0),
754+
locked=Balance(0).set_unit(alice_subnet_netuid),
750755
emission=get_dynamic_balance(
751756
alice_stakes[0].emission.rao, alice_subnet_netuid
752757
),
@@ -815,7 +820,7 @@ def test_transfer_stake(subtensor, alice_wallet, bob_wallet, dave_wallet):
815820
coldkey_ss58=bob_wallet.coldkey.ss58_address,
816821
netuid=dave_subnet_netuid,
817822
stake=get_dynamic_balance(bob_stakes[0].stake.rao, dave_subnet_netuid),
818-
locked=Balance(0),
823+
locked=Balance(0).set_unit(dave_subnet_netuid),
819824
emission=get_dynamic_balance(
820825
bob_stakes[0].emission.rao, dave_subnet_netuid
821826
),
@@ -824,4 +829,4 @@ def test_transfer_stake(subtensor, alice_wallet, bob_wallet, dave_wallet):
824829
),
825830
]
826831
assert bob_stakes == expected_bob_stake
827-
logging.console.success(f"✅ Test [green]test_transfer_stake[/green] passed")
832+
logging.console.success("✅ Test [green]test_transfer_stake[/green] passed")

0 commit comments

Comments
 (0)