Skip to content

Commit 5ce4b9f

Browse files
make raise_error param optional
1 parent 8e4c768 commit 5ce4b9f

File tree

6 files changed

+98
-39
lines changed

6 files changed

+98
-39
lines changed

bittensor/core/async_subtensor.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,9 +3281,10 @@ async def set_delegate_take(
32813281
wallet: "Wallet",
32823282
hotkey_ss58: str,
32833283
take: float,
3284-
wait_for_inclusion=True,
3285-
wait_for_finalization=True,
3286-
) -> None:
3284+
wait_for_inclusion: bool = True,
3285+
wait_for_finalization: bool = True,
3286+
raise_error: bool = False,
3287+
) -> tuple[bool, str]:
32873288
"""
32883289
Sets the delegate 'take' percentage for a neuron identified by its hotkey.
32893290
The 'take' represents the percentage of rewards that the delegate claims from its nominators' stakes.
@@ -3294,6 +3295,11 @@ async def set_delegate_take(
32943295
take (float): Percentage reward for the delegate.
32953296
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
32963297
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
3298+
raise_error: Raises relevant exception rather than returning `False` if unsuccessful.
3299+
3300+
Returns:
3301+
tuple[bool, str]: A tuple where the first element is a boolean indicating success or failure of the
3302+
operation, and the second element is a message providing additional information.
32973303
32983304
Raises:
32993305
DelegateTakeTooHigh: Delegate take is too high.
@@ -3316,30 +3322,35 @@ async def set_delegate_take(
33163322

33173323
if current_take_u16 == take_u16:
33183324
logging.info(":white_heavy_check_mark: [green]Already Set[/green]")
3319-
return
3325+
return True, ""
33203326

33213327
logging.info(f"Updating {hotkey_ss58} take: current={current_take} new={take}")
33223328

33233329
if current_take_u16 < take_u16:
3324-
await increase_take_extrinsic(
3330+
success, error = await increase_take_extrinsic(
33253331
self,
33263332
wallet,
33273333
hotkey_ss58,
33283334
take_u16,
33293335
wait_for_finalization=wait_for_finalization,
33303336
wait_for_inclusion=wait_for_inclusion,
3337+
raise_error=raise_error,
33313338
)
33323339
else:
3333-
await decrease_take_extrinsic(
3340+
success, error = await decrease_take_extrinsic(
33343341
self,
33353342
wallet,
33363343
hotkey_ss58,
33373344
take_u16,
33383345
wait_for_finalization=wait_for_finalization,
33393346
wait_for_inclusion=wait_for_inclusion,
3347+
raise_error=raise_error,
33403348
)
33413349

3342-
logging.info(":white_heavy_check_mark: [green]Take Updated[/green]")
3350+
if success:
3351+
logging.info(":white_heavy_check_mark: [green]Take Updated[/green]")
3352+
3353+
return success, error
33433354

33443355
async def set_subnet_identity(
33453356
self,

bittensor/core/extrinsics/asyncex/take.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from bittensor_wallet.bittensor_wallet import Wallet
44

5+
from bittensor.utils import unlock_key
6+
57
if TYPE_CHECKING:
68
from bittensor.core.async_subtensor import AsyncSubtensor
79

@@ -11,10 +13,14 @@ async def increase_take_extrinsic(
1113
wallet: Wallet,
1214
hotkey_ss58: str,
1315
take: int,
14-
wait_for_inclusion=True,
15-
wait_for_finalization=True,
16-
) -> None:
17-
wallet.unlock_coldkey()
16+
wait_for_inclusion: bool = True,
17+
wait_for_finalization: bool = True,
18+
raise_error: bool = False,
19+
) -> tuple[bool, str]:
20+
unlock = unlock_key(wallet, raise_error=raise_error)
21+
22+
if not unlock.success:
23+
return False, unlock.message
1824

1925
call = await subtensor.substrate.compose_call(
2026
call_module="SubtensorModule",
@@ -25,12 +31,12 @@ async def increase_take_extrinsic(
2531
},
2632
)
2733

28-
await subtensor.sign_and_send_extrinsic(
34+
return await subtensor.sign_and_send_extrinsic(
2935
call,
3036
wallet,
3137
wait_for_inclusion=wait_for_inclusion,
3238
wait_for_finalization=wait_for_finalization,
33-
raise_error=True,
39+
raise_error=raise_error,
3440
)
3541

3642

@@ -39,10 +45,14 @@ async def decrease_take_extrinsic(
3945
wallet: Wallet,
4046
hotkey_ss58: str,
4147
take: int,
42-
wait_for_inclusion=True,
43-
wait_for_finalization=True,
44-
) -> None:
45-
wallet.unlock_coldkey()
48+
wait_for_inclusion: bool = True,
49+
wait_for_finalization: bool = True,
50+
raise_error: bool = False,
51+
) -> tuple[bool, str]:
52+
unlock = unlock_key(wallet, raise_error=raise_error)
53+
54+
if not unlock.success:
55+
return False, unlock.message
4656

4757
call = await subtensor.substrate.compose_call(
4858
call_module="SubtensorModule",
@@ -53,10 +63,10 @@ async def decrease_take_extrinsic(
5363
},
5464
)
5565

56-
await subtensor.sign_and_send_extrinsic(
66+
return await subtensor.sign_and_send_extrinsic(
5767
call,
5868
wallet,
5969
wait_for_inclusion=wait_for_inclusion,
6070
wait_for_finalization=wait_for_finalization,
61-
raise_error=True,
71+
raise_error=raise_error,
6272
)

bittensor/core/extrinsics/take.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from bittensor_wallet.bittensor_wallet import Wallet
44

5+
from bittensor.utils.btlogging import logging
6+
from bittensor.utils import unlock_key
7+
58
if TYPE_CHECKING:
69
from bittensor.core.subtensor import Subtensor
710

@@ -11,10 +14,14 @@ def increase_take_extrinsic(
1114
wallet: Wallet,
1215
hotkey_ss58: str,
1316
take: int,
14-
wait_for_inclusion=True,
15-
wait_for_finalization=True,
16-
) -> None:
17-
wallet.unlock_coldkey()
17+
wait_for_inclusion: bool = True,
18+
wait_for_finalization: bool = True,
19+
raise_error: bool = False,
20+
) -> tuple[bool, str]:
21+
unlock = unlock_key(wallet, raise_error=raise_error)
22+
23+
if not unlock.success:
24+
return False, unlock.message
1825

1926
call = subtensor.substrate.compose_call(
2027
call_module="SubtensorModule",
@@ -25,12 +32,12 @@ def increase_take_extrinsic(
2532
},
2633
)
2734

28-
subtensor.sign_and_send_extrinsic(
35+
return subtensor.sign_and_send_extrinsic(
2936
call,
3037
wallet,
3138
wait_for_inclusion=wait_for_inclusion,
3239
wait_for_finalization=wait_for_finalization,
33-
raise_error=True,
40+
raise_error=raise_error,
3441
)
3542

3643

@@ -39,10 +46,14 @@ def decrease_take_extrinsic(
3946
wallet: Wallet,
4047
hotkey_ss58: str,
4148
take: int,
42-
wait_for_inclusion=True,
43-
wait_for_finalization=True,
44-
) -> None:
45-
wallet.unlock_coldkey()
49+
wait_for_inclusion: bool = True,
50+
wait_for_finalization: bool = True,
51+
raise_error: bool = False,
52+
) -> tuple[bool, str]:
53+
unlock = unlock_key(wallet, raise_error=raise_error)
54+
55+
if not unlock.success:
56+
return False, unlock.message
4657

4758
call = subtensor.substrate.compose_call(
4859
call_module="SubtensorModule",
@@ -53,10 +64,10 @@ def decrease_take_extrinsic(
5364
},
5465
)
5566

56-
subtensor.sign_and_send_extrinsic(
67+
return subtensor.sign_and_send_extrinsic(
5768
call,
5869
wallet,
5970
wait_for_inclusion=wait_for_inclusion,
6071
wait_for_finalization=wait_for_finalization,
61-
raise_error=True,
72+
raise_error=raise_error,
6273
)

bittensor/core/subtensor.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,9 +1577,10 @@ def set_delegate_take(
15771577
wallet: "Wallet",
15781578
hotkey_ss58: str,
15791579
take: float,
1580-
wait_for_inclusion=True,
1581-
wait_for_finalization=True,
1582-
) -> None:
1580+
wait_for_inclusion: bool = True,
1581+
wait_for_finalization: bool = True,
1582+
raise_error: bool = False,
1583+
) -> tuple[bool, str]:
15831584
"""
15841585
Sets the delegate 'take' percentage for a nueron identified by its hotkey.
15851586
The 'take' represents the percentage of rewards that the delegate claims from its nominators' stakes.
@@ -1590,6 +1591,11 @@ def set_delegate_take(
15901591
take (float): Percentage reward for the delegate.
15911592
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
15921593
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
1594+
raise_error: Raises relevant exception rather than returning `False` if unsuccessful.
1595+
1596+
Returns:
1597+
tuple[bool, str]: A tuple where the first element is a boolean indicating success or failure of the
1598+
operation, and the second element is a message providing additional information.
15931599
15941600
Raises:
15951601
DelegateTakeTooHigh: Delegate take is too high.
@@ -1612,30 +1618,35 @@ def set_delegate_take(
16121618

16131619
if current_take_u16 == take_u16:
16141620
logging.info(":white_heavy_check_mark: [green]Already Set[/green]")
1615-
return
1621+
return True, ""
16161622

16171623
logging.info(f"Updating {hotkey_ss58} take: current={current_take} new={take}")
16181624

16191625
if current_take_u16 < take_u16:
1620-
increase_take_extrinsic(
1626+
success, error = increase_take_extrinsic(
16211627
self,
16221628
wallet,
16231629
hotkey_ss58,
16241630
take_u16,
16251631
wait_for_finalization=wait_for_finalization,
16261632
wait_for_inclusion=wait_for_inclusion,
1633+
raise_error=raise_error,
16271634
)
16281635
else:
1629-
decrease_take_extrinsic(
1636+
success, error = decrease_take_extrinsic(
16301637
self,
16311638
wallet,
16321639
hotkey_ss58,
16331640
take_u16,
16341641
wait_for_finalization=wait_for_finalization,
16351642
wait_for_inclusion=wait_for_inclusion,
1643+
raise_error=raise_error,
16361644
)
16371645

1638-
logging.info(":white_heavy_check_mark: [green]Take Updated[/green]")
1646+
if success:
1647+
logging.info(":white_heavy_check_mark: [green]Take Updated[/green]")
1648+
1649+
return success, error
16391650

16401651
def is_hotkey_delegate(self, hotkey_ss58: str, block: Optional[int] = None) -> bool:
16411652
"""

bittensor/utils/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ def validate_chain_endpoint(endpoint_url: str) -> tuple[bool, str]:
330330
return True, ""
331331

332332

333-
def unlock_key(wallet: "Wallet", unlock_type="coldkey") -> "UnlockStatus":
333+
def unlock_key(
334+
wallet: "Wallet",
335+
unlock_type="coldkey",
336+
raise_error=False,
337+
) -> "UnlockStatus":
334338
"""
335339
Attempts to decrypt a wallet's coldkey or hotkey
336340
Args:
@@ -350,9 +354,15 @@ def unlock_key(wallet: "Wallet", unlock_type="coldkey") -> "UnlockStatus":
350354
getattr(wallet, unlocker)()
351355
return UnlockStatus(True, "")
352356
except PasswordError:
357+
if raise_error:
358+
raise
359+
353360
err_msg = f"The password used to decrypt your {unlock_type.capitalize()} keyfile is invalid."
354361
return UnlockStatus(False, err_msg)
355362
except KeyFileError:
363+
if raise_error:
364+
raise
365+
356366
err_msg = f"{unlock_type.capitalize()} keyfile is corrupt, non-writable, or non-readable, or non-existent."
357367
return UnlockStatus(False, err_msg)
358368

tests/e2e_tests/test_delegate.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def test_change_take(local_chain, subtensor, alice_wallet, bob_wallet):
9191
alice_wallet,
9292
alice_wallet.hotkey.ss58_address,
9393
0.1,
94+
raise_error=True,
9495
)
9596

9697
subtensor.root_register(
@@ -108,19 +109,22 @@ def test_change_take(local_chain, subtensor, alice_wallet, bob_wallet):
108109
bob_wallet,
109110
alice_wallet.hotkey.ss58_address,
110111
0.1,
112+
raise_error=True,
111113
)
112114

113115
with pytest.raises(bittensor.DelegateTakeTooHigh):
114116
subtensor.set_delegate_take(
115117
alice_wallet,
116118
alice_wallet.hotkey.ss58_address,
117119
0.5,
120+
raise_error=True,
118121
)
119122

120123
subtensor.set_delegate_take(
121124
alice_wallet,
122125
alice_wallet.hotkey.ss58_address,
123126
0.1,
127+
raise_error=True,
124128
)
125129

126130
take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address)
@@ -132,6 +136,7 @@ def test_change_take(local_chain, subtensor, alice_wallet, bob_wallet):
132136
alice_wallet,
133137
alice_wallet.hotkey.ss58_address,
134138
0.15,
139+
raise_error=True,
135140
)
136141

137142
take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address)
@@ -151,6 +156,7 @@ def test_change_take(local_chain, subtensor, alice_wallet, bob_wallet):
151156
alice_wallet,
152157
alice_wallet.hotkey.ss58_address,
153158
0.15,
159+
raise_error=True,
154160
)
155161

156162
take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address)

0 commit comments

Comments
 (0)