Skip to content

Commit fb34f07

Browse files
committed
fix test + ruff
1 parent 423cfe8 commit fb34f07

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
lines changed

bittensor/core/chain_data/dynamic_info.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def fix_decoded_values(cls, decoded: dict) -> "DynamicInfo":
6868
netuid = int(decoded["netuid"])
6969
symbol = bytes([int(b) for b in decoded["token_symbol"]]).decode()
7070
subnet_name = bytes([int(b) for b in decoded["subnet_name"]]).decode()
71-
71+
7272
is_dynamic = (
7373
True if int(decoded["netuid"]) > 0 else False
7474
) # Root is not dynamic

bittensor/core/chain_data/stake_info.py

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class StakeInfo:
3232
drain: int
3333
is_registered: bool
3434

35-
3635
@classmethod
3736
def fix_decoded_values(cls, decoded: Any) -> "StakeInfo":
3837
"""Fixes the decoded values."""

bittensor/core/extrinsics/unstaking.py

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def unstake_multiple_extrinsic(
244244
subtensor (bittensor.core.subtensor.Subtensor): Subtensor instance.
245245
wallet (bittensor_wallet.Wallet): The wallet with the coldkey to unstake to.
246246
hotkey_ss58s (List[str]): List of hotkeys to unstake from.
247+
netuids (List[int]): List of netuids to unstake from.
247248
amounts (List[Union[Balance, float]]): List of amounts to unstake. If ``None``, unstake all.
248249
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning ``true``, or returns ``false`` if the extrinsic fails to enter the block within the timeout.
249250
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning ``true``, or returns ``false`` if the extrinsic fails to be finalized within the timeout.

bittensor/core/subtensor.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1703,12 +1703,15 @@ def get_delegate_by_hotkey(
17031703

17041704
return DelegateInfo.from_vec_u8(bytes(result))
17051705

1706-
def get_stake_for_coldkey(self, coldkey_ss58: str) -> Optional[list["StakeInfo"]]:
1706+
def get_stake_for_coldkey(
1707+
self, coldkey_ss58: str, block: Optional[int] = None
1708+
) -> Optional[list["StakeInfo"]]:
17071709
"""
17081710
Retrieves the stake information for a given coldkey.
17091711
17101712
Args:
17111713
coldkey_ss58 (str): The SS58 address of the coldkey.
1714+
block (Optional[int]): The block number at which to query the stake information.
17121715
17131716
Returns:
17141717
Optional[list[StakeInfo]]: A list of StakeInfo objects, or ``None`` if no stake information is found.
@@ -1718,6 +1721,7 @@ def get_stake_for_coldkey(self, coldkey_ss58: str) -> Optional[list["StakeInfo"]
17181721
runtime_api="StakeInfoRuntimeApi",
17191722
method="get_stake_info_for_coldkey",
17201723
params=[encoded_coldkey],
1724+
block=block,
17211725
)
17221726

17231727
if hex_bytes_result is None:
@@ -1730,7 +1734,11 @@ def get_stake_for_coldkey(self, coldkey_ss58: str) -> Optional[list["StakeInfo"]
17301734
return StakeInfo.list_from_vec_u8(bytes_result)
17311735

17321736
def get_stake_for_coldkey_and_hotkey(
1733-
self, hotkey_ss58: str, coldkey_ss58: str, netuid: Optional[int] = None
1737+
self,
1738+
hotkey_ss58: str,
1739+
coldkey_ss58: str,
1740+
netuid: Optional[int] = None,
1741+
block: Optional[int] = None,
17341742
) -> Optional[Union["StakeInfo", list["StakeInfo"]]]:
17351743
"""
17361744
Returns the stake under a coldkey - hotkey pairing.
@@ -1739,11 +1747,12 @@ def get_stake_for_coldkey_and_hotkey(
17391747
hotkey_ss58 (str): The SS58 address of the hotkey.
17401748
coldkey_ss58 (str): The SS58 address of the coldkey.
17411749
netuid (Optional[int]): The subnet ID to filter by. If provided, only returns stake for this specific subnet.
1750+
block (Optional[int]): The block number at which to query the stake information.
17421751
17431752
Returns:
17441753
Optional[StakeInfo]: The StakeInfo object/s under the coldkey - hotkey pairing, or ``None`` if the pairing does not exist or the stake is not found.
17451754
"""
1746-
all_stakes = self.get_stake_for_coldkey(coldkey_ss58)
1755+
all_stakes = self.get_stake_for_coldkey(coldkey_ss58, block)
17471756
stakes = [
17481757
stake
17491758
for stake in all_stakes
@@ -2426,6 +2435,7 @@ def unstake_multiple(
24262435
Args:
24272436
wallet (bittensor_wallet.Wallet): The wallet linked to the coldkey from which the stakes are being withdrawn.
24282437
hotkey_ss58s (List[str]): A list of hotkey ``SS58`` addresses to unstake from.
2438+
netuids (List[int]): A list of neuron netuids to unstake from.
24292439
amounts (List[Union[Balance, float]]): The amounts of TAO to unstake from each hotkey. If not provided, unstakes all available stakes.
24302440
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
24312441
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.

tests/unit_tests/test_subtensor.py

+66-21
Original file line numberDiff line numberDiff line change
@@ -2196,45 +2196,78 @@ def test_networks_during_connection(mocker):
21962196
sub.chain_endpoint = settings.NETWORK_MAP.get(network)
21972197

21982198

2199-
@pytest.mark.parametrize(
2200-
"fake_value_result",
2201-
[1, None],
2202-
ids=["result has value attr", "result has not value attr"],
2203-
)
2204-
def test_get_stake_for_coldkey_and_hotkey(subtensor, mocker, fake_value_result):
2205-
"""Test get_stake_for_coldkey_and_hotkey calls right method with correct arguments."""
2199+
def test_get_stake_for_coldkey_and_hotkey_with_single_result(subtensor, mocker):
2200+
"""Test `get_stake_for_coldkey_and_hotkey` calls right method with correct arguments and get 1 stake info."""
22062201
# Preps
22072202
fake_hotkey_ss58 = "FAKE_H_SS58"
22082203
fake_coldkey_ss58 = "FAKE_C_SS58"
2204+
fake_netuid = 255
22092205
fake_block = 123
22102206

2211-
return_value = (
2212-
mocker.Mock(value=fake_value_result)
2213-
if fake_value_result is not None
2214-
else fake_value_result
2207+
fake_stake_info_1 = mocker.Mock(hotkey_ss58="some")
2208+
fake_stake_info_2 = mocker.Mock(
2209+
hotkey_ss58=fake_hotkey_ss58, netuid=fake_netuid, stake=100
22152210
)
22162211

2217-
subtensor.query_subtensor = mocker.patch.object(
2218-
subtensor, "query_subtensor", return_value=return_value
2212+
return_value = [
2213+
fake_stake_info_1,
2214+
fake_stake_info_2,
2215+
]
2216+
2217+
subtensor.get_stake_for_coldkey = mocker.patch.object(
2218+
subtensor, "get_stake_for_coldkey", return_value=return_value
22192219
)
2220-
spy_balance_from_rao = mocker.spy(subtensor_module.Balance, "from_rao")
22212220

22222221
# Call
22232222
result = subtensor.get_stake_for_coldkey_and_hotkey(
22242223
hotkey_ss58=fake_hotkey_ss58,
22252224
coldkey_ss58=fake_coldkey_ss58,
2225+
netuid=fake_netuid,
22262226
block=fake_block,
22272227
)
22282228

22292229
# Asserts
2230-
subtensor.query_subtensor.assert_called_once_with(
2231-
"Stake", fake_block, [fake_hotkey_ss58, fake_coldkey_ss58]
2230+
subtensor.get_stake_for_coldkey.assert_called_once_with(
2231+
fake_coldkey_ss58, fake_block
2232+
)
2233+
assert result == fake_stake_info_2
2234+
2235+
2236+
def test_get_stake_for_coldkey_and_hotkey_with_multiple_result(subtensor, mocker):
2237+
"""Test `get_stake_for_coldkey_and_hotkey` calls right method with correct arguments and get multiple stake info."""
2238+
# Preps
2239+
fake_hotkey_ss58 = "FAKE_H_SS58"
2240+
fake_coldkey_ss58 = "FAKE_C_SS58"
2241+
fake_netuid = 255
2242+
fake_block = 123
2243+
2244+
fake_stake_info_1 = mocker.Mock(hotkey_ss58="some")
2245+
fake_stake_info_2 = mocker.Mock(
2246+
hotkey_ss58=fake_hotkey_ss58, netuid=fake_netuid, stake=100
2247+
)
2248+
fake_stake_info_3 = mocker.Mock(
2249+
hotkey_ss58=fake_hotkey_ss58, netuid=fake_netuid, stake=200
2250+
)
2251+
2252+
return_value = [fake_stake_info_1, fake_stake_info_2, fake_stake_info_3]
2253+
2254+
subtensor.get_stake_for_coldkey = mocker.patch.object(
2255+
subtensor, "get_stake_for_coldkey", return_value=return_value
2256+
)
2257+
2258+
# Call
2259+
result = subtensor.get_stake_for_coldkey_and_hotkey(
2260+
hotkey_ss58=fake_hotkey_ss58,
2261+
coldkey_ss58=fake_coldkey_ss58,
2262+
netuid=fake_netuid,
2263+
block=fake_block,
22322264
)
2233-
if fake_value_result is not None:
2234-
spy_balance_from_rao.assert_called_once_with(fake_value_result)
2235-
else:
2236-
spy_balance_from_rao.assert_not_called()
2237-
assert result == fake_value_result
2265+
2266+
# Asserts
2267+
subtensor.get_stake_for_coldkey.assert_called_once_with(
2268+
fake_coldkey_ss58, fake_block
2269+
)
2270+
assert result == [fake_stake_info_2, fake_stake_info_3]
22382271

22392272

22402273
def test_does_hotkey_exist_true(mocker, subtensor):
@@ -2714,6 +2747,7 @@ def test_add_stake_success(mocker, subtensor):
27142747
fake_wallet = mocker.Mock()
27152748
fake_hotkey_ss58 = "fake_hotkey"
27162749
fake_amount = 10.0
2750+
fake_netuid = 123
27172751

27182752
mock_add_stake_extrinsic = mocker.patch.object(
27192753
subtensor_module, "add_stake_extrinsic"
@@ -2723,6 +2757,7 @@ def test_add_stake_success(mocker, subtensor):
27232757
result = subtensor.add_stake(
27242758
wallet=fake_wallet,
27252759
hotkey_ss58=fake_hotkey_ss58,
2760+
netuid=fake_netuid,
27262761
amount=fake_amount,
27272762
wait_for_inclusion=True,
27282763
wait_for_finalization=False,
@@ -2733,6 +2768,7 @@ def test_add_stake_success(mocker, subtensor):
27332768
subtensor=subtensor,
27342769
wallet=fake_wallet,
27352770
hotkey_ss58=fake_hotkey_ss58,
2771+
netuid=fake_netuid,
27362772
amount=fake_amount,
27372773
wait_for_inclusion=True,
27382774
wait_for_finalization=False,
@@ -2746,6 +2782,7 @@ def test_add_stake_multiple_success(mocker, subtensor):
27462782
fake_wallet = mocker.Mock()
27472783
fake_hotkey_ss58 = ["fake_hotkey"]
27482784
fake_amount = [10.0]
2785+
fake_netuids = [1]
27492786

27502787
mock_add_stake_multiple_extrinsic = mocker.patch.object(
27512788
subtensor_module, "add_stake_multiple_extrinsic"
@@ -2755,6 +2792,7 @@ def test_add_stake_multiple_success(mocker, subtensor):
27552792
result = subtensor.add_stake_multiple(
27562793
wallet=fake_wallet,
27572794
hotkey_ss58s=fake_hotkey_ss58,
2795+
netuids=fake_netuids,
27582796
amounts=fake_amount,
27592797
wait_for_inclusion=True,
27602798
wait_for_finalization=False,
@@ -2765,6 +2803,7 @@ def test_add_stake_multiple_success(mocker, subtensor):
27652803
subtensor=subtensor,
27662804
wallet=fake_wallet,
27672805
hotkey_ss58s=fake_hotkey_ss58,
2806+
netuids=fake_netuids,
27682807
amounts=fake_amount,
27692808
wait_for_inclusion=True,
27702809
wait_for_finalization=False,
@@ -2778,13 +2817,15 @@ def test_unstake_success(mocker, subtensor):
27782817
fake_wallet = mocker.Mock()
27792818
fake_hotkey_ss58 = "hotkey_1"
27802819
fake_amount = 10.0
2820+
fake_netuid = 123
27812821

27822822
mock_unstake_extrinsic = mocker.patch.object(subtensor_module, "unstake_extrinsic")
27832823

27842824
# Call
27852825
result = subtensor.unstake(
27862826
wallet=fake_wallet,
27872827
hotkey_ss58=fake_hotkey_ss58,
2828+
netuid=fake_netuid,
27882829
amount=fake_amount,
27892830
wait_for_inclusion=True,
27902831
wait_for_finalization=False,
@@ -2795,6 +2836,7 @@ def test_unstake_success(mocker, subtensor):
27952836
subtensor=subtensor,
27962837
wallet=fake_wallet,
27972838
hotkey_ss58=fake_hotkey_ss58,
2839+
netuid=fake_netuid,
27982840
amount=fake_amount,
27992841
wait_for_inclusion=True,
28002842
wait_for_finalization=False,
@@ -2808,6 +2850,7 @@ def test_unstake_multiple_success(mocker, subtensor):
28082850
fake_wallet = mocker.Mock()
28092851
fake_hotkeys = ["hotkey_1", "hotkey_2"]
28102852
fake_amounts = [10.0, 20.0]
2853+
fake_netuids = [1, 2]
28112854

28122855
mock_unstake_multiple_extrinsic = mocker.patch(
28132856
"bittensor.core.subtensor.unstake_multiple_extrinsic", return_value=True
@@ -2817,6 +2860,7 @@ def test_unstake_multiple_success(mocker, subtensor):
28172860
result = subtensor.unstake_multiple(
28182861
wallet=fake_wallet,
28192862
hotkey_ss58s=fake_hotkeys,
2863+
netuids=fake_netuids,
28202864
amounts=fake_amounts,
28212865
wait_for_inclusion=True,
28222866
wait_for_finalization=False,
@@ -2827,6 +2871,7 @@ def test_unstake_multiple_success(mocker, subtensor):
28272871
subtensor=subtensor,
28282872
wallet=fake_wallet,
28292873
hotkey_ss58s=fake_hotkeys,
2874+
netuids=fake_netuids,
28302875
amounts=fake_amounts,
28312876
wait_for_inclusion=True,
28322877
wait_for_finalization=False,

0 commit comments

Comments
 (0)