Skip to content

Commit bc10981

Browse files
committed
Merge pull request #2382 from opentensor/feat/roman/remove-prompt-from-sdk
Avoid using `prompt` in SDK
1 parent b8ffe19 commit bc10981

19 files changed

+146
-397
lines changed

bittensor/core/config.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,6 @@ def __init__(
9797
# this can fail if --no_version_checking has already been added.
9898
pass
9999

100-
try:
101-
parser.add_argument(
102-
"--no_prompt",
103-
dest="no_prompt",
104-
action="store_true",
105-
help="Set ``true`` to stop cli from prompting the user.",
106-
default=False,
107-
)
108-
except Exception:
109-
# this can fail if --no_version_checking has already been added.
110-
pass
111-
112100
# Get args from argv if not passed in.
113101
if args is None:
114102
args = sys.argv[1:]

bittensor/core/extrinsics/commit_weights.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from typing import Optional, TYPE_CHECKING
2121

2222
from retry import retry
23-
from rich.prompt import Confirm
2423

2524
from bittensor.core.extrinsics.utils import submit_extrinsic
2625
from bittensor.utils import format_error_message
@@ -33,7 +32,7 @@
3332
from bittensor.core.subtensor import Subtensor
3433

3534

36-
# # Chain call for `commit_weights_extrinsic`
35+
# Chain call for `commit_weights_extrinsic`
3736
@ensure_connected
3837
def do_commit_weights(
3938
self: "Subtensor",
@@ -101,11 +100,10 @@ def commit_weights_extrinsic(
101100
commit_hash: str,
102101
wait_for_inclusion: bool = False,
103102
wait_for_finalization: bool = False,
104-
prompt: bool = False,
105103
) -> tuple[bool, str]:
106104
"""
107105
Commits a hash of the neuron's weights to the Bittensor blockchain using the provided wallet.
108-
This function is a wrapper around the `do_commit_weights` method, handling user prompts and error messages.
106+
This function is a wrapper around the `do_commit_weights` method.
109107
110108
Args:
111109
subtensor (bittensor.core.subtensor.Subtensor): The subtensor instance used for blockchain interaction.
@@ -114,16 +112,12 @@ def commit_weights_extrinsic(
114112
commit_hash (str): The hash of the neuron's weights to be committed.
115113
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
116114
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
117-
prompt (bool): If ``True``, prompts for user confirmation before proceeding.
118115
119116
Returns:
120-
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string
121-
value describing the success or potential error.
117+
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string value describing the success or potential error.
122118
123119
This function provides a user-friendly interface for committing weights to the Bittensor blockchain, ensuring proper error handling and user interaction when required.
124120
"""
125-
if prompt and not Confirm.ask(f"Would you like to commit weights?"):
126-
return False, "User cancelled the operation."
127121

128122
success, error_message = do_commit_weights(
129123
self=subtensor,
@@ -224,11 +218,10 @@ def reveal_weights_extrinsic(
224218
version_key: int,
225219
wait_for_inclusion: bool = False,
226220
wait_for_finalization: bool = False,
227-
prompt: bool = False,
228221
) -> tuple[bool, str]:
229222
"""
230223
Reveals the weights for a specific subnet on the Bittensor blockchain using the provided wallet.
231-
This function is a wrapper around the `_do_reveal_weights` method, handling user prompts and error messages.
224+
This function is a wrapper around the `_do_reveal_weights` method.
232225
233226
Args:
234227
subtensor (bittensor.core.subtensor.Subtensor): The subtensor instance used for blockchain interaction.
@@ -240,18 +233,13 @@ def reveal_weights_extrinsic(
240233
version_key (int): Version key for compatibility with the network.
241234
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
242235
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
243-
prompt (bool): If ``True``, prompts for user confirmation before proceeding.
244236
245237
Returns:
246-
tuple[bool, str]: ``True`` if the weight revelation is successful, False otherwise. And `msg`, a string
247-
value describing the success or potential error.
238+
tuple[bool, str]: ``True`` if the weight revelation is successful, False otherwise. And `msg`, a string value describing the success or potential error.
248239
249240
This function provides a user-friendly interface for revealing weights on the Bittensor blockchain, ensuring proper error handling and user interaction when required.
250241
"""
251242

252-
if prompt and not Confirm.ask(f"Would you like to reveal weights?"):
253-
return False, "User cancelled the operation."
254-
255243
success, error_message = do_reveal_weights(
256244
self=subtensor,
257245
wallet=wallet,

bittensor/core/extrinsics/registration.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from bittensor_wallet.errors import KeyFileError
2222
from retry import retry
23-
from rich.prompt import Confirm
2423

2524
from bittensor.utils import format_error_message
2625
from bittensor.utils.btlogging import logging
@@ -43,7 +42,7 @@ def _do_pow_register(
4342
self: "Subtensor",
4443
netuid: int,
4544
wallet: "Wallet",
46-
pow_result: POWSolution,
45+
pow_result: "POWSolution",
4746
wait_for_inclusion: bool = False,
4847
wait_for_finalization: bool = True,
4948
) -> tuple[bool, Optional[str]]:
@@ -53,14 +52,12 @@ def _do_pow_register(
5352
netuid (int): The subnet to register on.
5453
wallet (bittensor.wallet): The wallet to register.
5554
pow_result (POWSolution): The PoW result to register.
56-
wait_for_inclusion (bool): If ``True``, waits for the extrinsic to be included in a block.
57-
Default to `False`.
55+
wait_for_inclusion (bool): If ``True``, waits for the extrinsic to be included in a block. Default to `False`.
5856
wait_for_finalization (bool): If ``True``, waits for the extrinsic to be finalized. Default to `True`.
5957
6058
Returns:
6159
success (bool): ``True`` if the extrinsic was included in a block.
62-
error (Optional[str]): ``None`` on success or not waiting for inclusion/finalization, otherwise the error
63-
message.
60+
error (Optional[str]): ``None`` on success or not waiting for inclusion/finalization, otherwise the error message.
6461
"""
6562

6663
@retry(delay=1, tries=3, backoff=2, max_delay=4)
@@ -108,7 +105,6 @@ def register_extrinsic(
108105
netuid: int,
109106
wait_for_inclusion: bool = False,
110107
wait_for_finalization: bool = True,
111-
prompt: bool = False,
112108
max_allowed_attempts: int = 3,
113109
output_in_place: bool = True,
114110
cuda: bool = False,
@@ -126,7 +122,6 @@ def register_extrinsic(
126122
netuid (int): The ``netuid`` of the subnet to register on.
127123
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.
128124
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.
129-
prompt (bool): If ``true``, the call waits for confirmation from the user before proceeding.
130125
max_allowed_attempts (int): Maximum number of attempts to register the wallet.
131126
output_in_place (bool): If true, prints the progress of the proof of work to the console in-place. Meaning the progress is printed on the same lines. Defaults to `True`.
132127
cuda (bool): If ``true``, the wallet should be registered using CUDA device(s).
@@ -158,15 +153,9 @@ def register_extrinsic(
158153
)
159154
return True
160155

161-
if prompt:
162-
if not Confirm.ask(
163-
"Continue Registration?\n hotkey: [bold white]{}[/bold white]\n coldkey: [bold white]{}[/bold white]\n network: [bold white]{}[/bold white]".format(
164-
wallet.hotkey.ss58_address,
165-
wallet.coldkeypub.ss58_address,
166-
subtensor.network,
167-
)
168-
):
169-
return False
156+
logging.debug(
157+
f"Registration hotkey: <blue>{wallet.hotkey.ss58_address}</blue>, <green>Public</green> coldkey: <blue>{wallet.coldkey.ss58_address}</blue> in the network: <blue>{subtensor.network}</blue>."
158+
)
170159

171160
if not torch:
172161
log_no_torch_error()
@@ -181,8 +170,6 @@ def register_extrinsic(
181170
# Solve latest POW.
182171
if cuda:
183172
if not torch.cuda.is_available():
184-
if prompt:
185-
logging.info("CUDA is not available.")
186173
return False
187174
pow_result: Optional[POWSolution] = create_pow(
188175
subtensor,
@@ -349,7 +336,6 @@ def burned_register_extrinsic(
349336
netuid: int,
350337
wait_for_inclusion: bool = False,
351338
wait_for_finalization: bool = True,
352-
prompt: bool = False,
353339
) -> bool:
354340
"""Registers the wallet to chain by recycling TAO.
355341
@@ -359,7 +345,6 @@ def burned_register_extrinsic(
359345
netuid (int): The ``netuid`` of the subnet to register on.
360346
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.
361347
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.
362-
prompt (bool): If ``true``, the call waits for confirmation from the user before proceeding.
363348
364349
Returns:
365350
success (bool): Flag is ``true`` if extrinsic was finalized or uncluded in the block. If we did not wait for finalization / inclusion, the response is ``true``.
@@ -386,7 +371,6 @@ def burned_register_extrinsic(
386371

387372
old_balance = subtensor.get_balance(wallet.coldkeypub.ss58_address)
388373

389-
recycle_amount = subtensor.recycle(netuid=netuid)
390374
if not neuron.is_null:
391375
logging.info(":white_heavy_check_mark: <green>Already Registered</green>")
392376
logging.info(f"\t\tuid: <blue>{neuron.uid}</blue>")
@@ -395,12 +379,11 @@ def burned_register_extrinsic(
395379
logging.info(f"\t\tcoldkey: <blue>{neuron.coldkey}</blue>")
396380
return True
397381

398-
if prompt:
399-
# Prompt user for confirmation.
400-
if not Confirm.ask(f"Recycle {recycle_amount} to register on subnet:{netuid}?"):
401-
return False
402-
403382
logging.info(":satellite: <magenta>Recycling TAO for Registration...</magenta>")
383+
384+
recycle_amount = subtensor.recycle(netuid=netuid)
385+
logging.info(f"Recycling {recycle_amount} to register on subnet:{netuid}")
386+
404387
success, err_msg = _do_burned_register(
405388
self=subtensor,
406389
netuid=netuid,
@@ -410,7 +393,7 @@ def burned_register_extrinsic(
410393
)
411394

412395
if not success:
413-
logging.error(f":cross_mark: <red>Failed:</red> {err_msg}")
396+
logging.error(f":cross_mark: <red>Failed error:</red> {err_msg}")
414397
time.sleep(0.5)
415398
return False
416399
# Successful registration, final check for neuron and pubkey

bittensor/core/extrinsics/root.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from bittensor_wallet.errors import KeyFileError
66
from numpy.typing import NDArray
77
from retry import retry
8-
from rich.prompt import Confirm
98

109
from bittensor.core.settings import version_as_int
1110
from bittensor.utils import format_error_message, weight_utils
@@ -62,7 +61,6 @@ def root_register_extrinsic(
6261
wallet: "Wallet",
6362
wait_for_inclusion: bool = False,
6463
wait_for_finalization: bool = True,
65-
prompt: bool = False,
6664
) -> bool:
6765
"""Registers the wallet to root network.
6866
@@ -71,7 +69,6 @@ def root_register_extrinsic(
7169
wallet (bittensor_wallet.Wallet): Bittensor wallet object.
7270
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. Default is ``False``.
7371
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. Default is ``True``.
74-
prompt (bool): If ``true``, the call waits for confirmation from the user before proceeding. Default is ``False``.
7572
7673
Returns:
7774
success (bool): Flag is ``true`` if extrinsic was finalized or uncluded in the block. If we did not wait for finalization / inclusion, the response is ``true``.
@@ -94,11 +91,6 @@ def root_register_extrinsic(
9491
)
9592
return True
9693

97-
if prompt:
98-
# Prompt user for confirmation.
99-
if not Confirm.ask("Register to root network?"):
100-
return False
101-
10294
logging.info(":satellite: <magenta>Registering to root network...</magenta>")
10395
success, err_msg = _do_root_register(
10496
wallet=wallet,
@@ -199,7 +191,6 @@ def set_root_weights_extrinsic(
199191
version_key: int = 0,
200192
wait_for_inclusion: bool = False,
201193
wait_for_finalization: bool = False,
202-
prompt: bool = False,
203194
) -> bool:
204195
"""Sets the given weights and values on chain for wallet hotkey account.
205196
@@ -211,7 +202,6 @@ def set_root_weights_extrinsic(
211202
version_key (int): The version key of the validator. Default is ``0``.
212203
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. Default is ``False``.
213204
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. Default is ``False``.
214-
prompt (bool): If ``true``, the call waits for confirmation from the user before proceeding. Default is ``False``.
215205
216206
Returns:
217207
success (bool): Flag is ``true`` if extrinsic was finalized or uncluded in the block. If we did not wait for finalization / inclusion, the response is ``true``.
@@ -254,15 +244,6 @@ def set_root_weights_extrinsic(
254244
f"Raw Weights -> Normalized weights: <blue>{weights}</blue> -> <green>{formatted_weights}</green>"
255245
)
256246

257-
# Ask before moving on.
258-
if prompt:
259-
if not Confirm.ask(
260-
"Do you want to set the following root weights?:\n[bold white] weights: {}\n uids: {}[/bold white ]?".format(
261-
formatted_weights, netuids
262-
)
263-
):
264-
return False
265-
266247
logging.info(
267248
f":satellite: <magenta>Setting root weights on</magenta> <blue>{subtensor.network}</blue> <magenta>...</magenta>"
268249
)

bittensor/core/extrinsics/serving.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1616
# DEALINGS IN THE SOFTWARE.
1717

18-
import json
1918
from typing import Optional, TYPE_CHECKING
2019

2120
from retry import retry
22-
from rich.prompt import Confirm
2321

2422
from bittensor.core.errors import MetadataError
2523
from bittensor.core.extrinsics.utils import submit_extrinsic
@@ -100,7 +98,6 @@ def serve_extrinsic(
10098
placeholder2: int = 0,
10199
wait_for_inclusion: bool = False,
102100
wait_for_finalization=True,
103-
prompt: bool = False,
104101
) -> bool:
105102
"""Subscribes a Bittensor endpoint to the subtensor chain.
106103
@@ -115,7 +112,6 @@ def serve_extrinsic(
115112
placeholder2 (int): A placeholder for future use.
116113
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.
117114
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.
118-
prompt (bool): If ``true``, the call waits for confirmation from the user before proceeding.
119115
120116
Returns:
121117
success (bool): Flag is ``true`` if extrinsic was finalized or uncluded in the block. If we did not wait for finalization / inclusion, the response is ``true``.
@@ -159,15 +155,6 @@ def serve_extrinsic(
159155
)
160156
return True
161157

162-
if prompt:
163-
output = params.copy()
164-
output["coldkey"] = wallet.coldkeypub.ss58_address
165-
output["hotkey"] = wallet.hotkey.ss58_address
166-
if not Confirm.ask(
167-
f"Do you want to serve axon:\n [bold white]{json.dumps(output, indent=4, sort_keys=True)}[/bold white]"
168-
):
169-
return False
170-
171158
logging.debug(
172159
f"Serving axon with: AxonInfo({wallet.hotkey.ss58_address},{ip}:{port}) -> {subtensor.network}:{netuid}"
173160
)

bittensor/core/extrinsics/set_weights.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import numpy as np
2222
from numpy.typing import NDArray
2323
from retry import retry
24-
from rich.prompt import Confirm
2524

2625
from bittensor.core.extrinsics.utils import submit_extrinsic
2726
from bittensor.core.settings import version_as_int
@@ -114,7 +113,6 @@ def set_weights_extrinsic(
114113
version_key: int = 0,
115114
wait_for_inclusion: bool = False,
116115
wait_for_finalization: bool = False,
117-
prompt: bool = False,
118116
) -> tuple[bool, str]:
119117
"""Sets the given weights and values on chain for wallet hotkey account.
120118
@@ -127,7 +125,6 @@ def set_weights_extrinsic(
127125
version_key (int): The version key of the validator.
128126
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.
129127
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.
130-
prompt (bool): If ``true``, the call waits for confirmation from the user before proceeding.
131128
132129
Returns:
133130
tuple[bool, str]: A tuple containing a success flag and an optional response message.
@@ -149,17 +146,11 @@ def set_weights_extrinsic(
149146
uids, weights
150147
)
151148

152-
# Ask before moving on.
153-
if prompt:
154-
if not Confirm.ask(
155-
f"Do you want to set weights:\n[bold white] weights: {[float(v / 65535) for v in weight_vals]}\n"
156-
f"uids: {weight_uids}[/bold white ]?"
157-
):
158-
return False, "Prompt refused."
159-
160149
logging.info(
161150
f":satellite: <magenta>Setting weights on </magenta><blue>{subtensor.network}<blue> <magenta>...</magenta>"
162151
)
152+
logging.debug(f"Weights: {[float(v / 65535) for v in weight_vals]}")
153+
163154
try:
164155
success, error_message = do_set_weights(
165156
self=subtensor,

0 commit comments

Comments
 (0)