Skip to content

Commit

Permalink
Wrapper for presenting the error in a human-readable view (#1859)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-opentensor committed Jun 5, 2024
1 parent 6899884 commit 26cba11
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 130 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
docker:
- image: cimg/python:<< parameters.python-version >>

timeout_in_minutes: 20

steps:
- checkout

Expand Down
3 changes: 2 additions & 1 deletion bittensor/extrinsics/commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from rich.prompt import Confirm

import bittensor
from bittensor.utils import format_error_message


def commit_weights_extrinsic(
Expand Down Expand Up @@ -67,7 +68,7 @@ def commit_weights_extrinsic(
return True, "Successfully committed weights."
else:
bittensor.logging.error(f"Failed to commit weights: {error_message}")
return False, error_message
return False, format_error_message(error_message)


def reveal_weights_extrinsic(
Expand Down
14 changes: 7 additions & 7 deletions bittensor/extrinsics/delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def nominate_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if success == True:
if success is True:
bittensor.__console__.print(
":white_heavy_check_mark: [green]Finalized[/green]"
)
Expand Down Expand Up @@ -138,7 +138,7 @@ def delegate_extrinsic(
)

# Convert to bittensor.Balance
if amount == None:
if amount is None:
# Stake it all.
staking_balance = bittensor.Balance.from_tao(my_prev_coldkey_balance.tao)
elif not isinstance(amount, bittensor.Balance):
Expand Down Expand Up @@ -184,7 +184,7 @@ def delegate_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if staking_response == True: # If we successfully staked.
if staking_response is True: # If we successfully staked.
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True
Expand Down Expand Up @@ -273,7 +273,7 @@ def undelegate_extrinsic(
)

# Convert to bittensor.Balance
if amount == None:
if amount is None:
# Stake it all.
unstaking_balance = bittensor.Balance.from_tao(my_prev_delegated_stake.tao)

Expand Down Expand Up @@ -315,7 +315,7 @@ def undelegate_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if staking_response == True: # If we successfully staked.
if staking_response is True: # If we successfully staked.
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True
Expand Down Expand Up @@ -403,7 +403,7 @@ def decrease_take_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if success == True:
if success is True:
bittensor.__console__.print(
":white_heavy_check_mark: [green]Finalized[/green]"
)
Expand Down Expand Up @@ -463,7 +463,7 @@ def increase_take_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if success == True:
if success is True:
bittensor.__console__.print(
":white_heavy_check_mark: [green]Finalized[/green]"
)
Expand Down
56 changes: 33 additions & 23 deletions bittensor/extrinsics/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,39 @@
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import time
import bittensor

import substrateinterface
from rich.prompt import Confirm

import bittensor
from bittensor.utils import format_error_message
from ..commands.network import HYPERPARAMS


def _find_event_attributes_in_extrinsic_receipt(
response: "substrateinterface.base.ExtrinsicReceipt", event_name: str
) -> list:
"""
Searches for the attributes of a specified event within an extrinsic receipt.
Args:
response (substrateinterface.base.ExtrinsicReceipt): The receipt of the extrinsic to be searched.
event_name (str): The name of the event to search for.
Returns:
list: A list of attributes for the specified event. Returns [-1] if the event is not found.
"""
for event in response.triggered_events:
# Access the event details
event_details = event.value["event"]
# Check if the event_id is 'NetworkAdded'
if event_details["event_id"] == event_name:
# Once found, you can access the attributes of the event_name
return event_details["attributes"]
return [-1]


def register_subnetwork_extrinsic(
subtensor: "bittensor.subtensor",
Expand Down Expand Up @@ -86,15 +114,13 @@ def register_subnetwork_extrinsic(
response.process_events()
if not response.is_success:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(
response.error_message
)
f":cross_mark: [red]Failed[/red]: {format_error_message(response.error_message)}"
)
time.sleep(0.5)

# Successful registration, final check for membership
else:
attributes = find_event_attributes_in_extrinsic_receipt(
attributes = _find_event_attributes_in_extrinsic_receipt(
response, "NetworkAdded"
)
bittensor.__console__.print(
Expand All @@ -103,20 +129,6 @@ def register_subnetwork_extrinsic(
return True


def find_event_attributes_in_extrinsic_receipt(response, event_name) -> list:
for event in response.triggered_events:
# Access the event details
event_details = event.value["event"]
# Check if the event_id is 'NetworkAdded'
if event_details["event_id"] == event_name:
# Once found, you can access the attributes of the event_name
return event_details["attributes"]
return [-1]


from ..commands.network import HYPERPARAMS


def set_hyperparameter_extrinsic(
subtensor: "bittensor.subtensor",
wallet: "bittensor.wallet",
Expand Down Expand Up @@ -158,7 +170,7 @@ def set_hyperparameter_extrinsic(
wallet.coldkey # unlock coldkey

extrinsic = HYPERPARAMS.get(parameter)
if extrinsic == None:
if extrinsic is None:
bittensor.__console__.print(
":cross_mark: [red]Invalid hyperparameter specified.[/red]"
)
Expand Down Expand Up @@ -198,9 +210,7 @@ def set_hyperparameter_extrinsic(
response.process_events()
if not response.is_success:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(
response.error_message
)
f":cross_mark: [red]Failed[/red]: {format_error_message(response.error_message)}"
)
time.sleep(0.5)

Expand Down
11 changes: 4 additions & 7 deletions bittensor/extrinsics/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import bittensor

import json
Expand Down Expand Up @@ -54,7 +55,7 @@ def prometheus_extrinsic(
"""

# ---- Get external ip ----
if ip == None:
if ip is None:
try:
external_ip = net.get_external_ip()
bittensor.__console__.print(
Expand Down Expand Up @@ -125,19 +126,15 @@ def prometheus_extrinsic(
)

if wait_for_inclusion or wait_for_finalization:
if success == True:
if success is True:
bittensor.__console__.print(
":white_heavy_check_mark: [green]Served prometheus[/green]\n [bold white]{}[/bold white]".format(
json.dumps(call_params, indent=4, sort_keys=True)
)
)
return True
else:
bittensor.__console__.print(
":cross_mark: [green]Failed to serve prometheus[/green] error: {}".format(
err
)
)
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: {err}")
return False
else:
return True
30 changes: 15 additions & 15 deletions bittensor/extrinsics/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import bittensor
import time
from rich.prompt import Confirm
from typing import List, Union, Optional, Tuple

from rich.prompt import Confirm

import bittensor
from bittensor.utils import format_error_message
from bittensor.utils.registration import (
POWSolution,
create_pow,
Expand Down Expand Up @@ -171,16 +174,17 @@ def register_extrinsic(
)
success, err_msg = result

if success != True or success == False:
if "key is already registered" in err_msg:
# Error meant that the key is already registered.
if not success:
# Look error here
# https://github.com/opentensor/subtensor/blob/development/pallets/subtensor/src/errors.rs
if "HotKeyAlreadyRegisteredInSubNet" in err_msg:
bittensor.__console__.print(
f":white_heavy_check_mark: [green]Already Registered on [bold]subnet:{netuid}[/bold][/green]"
)
return True

bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(err_msg)
f":cross_mark: [red]Failed[/red]: {err_msg}"
)
time.sleep(0.5)

Expand Down Expand Up @@ -290,10 +294,8 @@ def burned_register_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if success != True or success == False:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(err_msg)
)
if not success:
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
time.sleep(0.5)
return False
# Successful registration, final check for neuron and pubkey
Expand Down Expand Up @@ -454,7 +456,7 @@ def run_faucet_extrinsic(
response.process_events()
if not response.is_success:
bittensor.__console__.print(
f":cross_mark: [red]Failed[/red]: Error: {response.error_message}"
f":cross_mark: [red]Failed[/red]: {format_error_message(response.error_message)}"
)
if attempts == max_allowed_attempts:
raise MaxAttemptsException
Expand Down Expand Up @@ -506,10 +508,8 @@ def swap_hotkey_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if success != True or success == False:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(err_msg)
)
if not success:
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
time.sleep(0.5)
return False

Expand Down
8 changes: 3 additions & 5 deletions bittensor/extrinsics/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ def root_register_extrinsic(
wait_for_finalization=wait_for_finalization,
)

if success != True or success == False:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(err_msg)
)
if not success:
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
time.sleep(0.5)

# Successful registration, final check for neuron and pubkey
Expand Down Expand Up @@ -205,7 +203,7 @@ def set_root_weights_extrinsic(
return True
else:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(error_message)
f":cross_mark: [red]Failed[/red]: {error_message}"
)
bittensor.logging.warning(
prefix="Set weights",
Expand Down
19 changes: 7 additions & 12 deletions bittensor/extrinsics/senate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# Imports
import bittensor

import time

from rich.prompt import Confirm

import bittensor
from bittensor.utils import format_error_message


def register_senate_extrinsic(
subtensor: "bittensor.subtensor",
Expand Down Expand Up @@ -78,9 +79,7 @@ def register_senate_extrinsic(
response.process_events()
if not response.is_success:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(
response.error_message
)
f":cross_mark: [red]Failed[/red]:{format_error_message(response.error_message)}"
)
time.sleep(0.5)

Expand Down Expand Up @@ -155,9 +154,7 @@ def leave_senate_extrinsic(
response.process_events()
if not response.is_success:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(
response.error_message
)
f":cross_mark: [red]Failed[/red]: {format_error_message(response.error_message)}"
)
time.sleep(0.5)

Expand Down Expand Up @@ -240,9 +237,7 @@ def vote_senate_extrinsic(
response.process_events()
if not response.is_success:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(
response.error_message
)
f":cross_mark: [red]Failed[/red]: {format_error_message(response.error_message)}"
)
time.sleep(0.5)

Expand Down
Loading

0 comments on commit 26cba11

Please sign in to comment.