Skip to content

Commit

Permalink
fix return format to use Tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
camfairchild committed Jul 4, 2023
1 parent 2df31eb commit 0252fcc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
10 changes: 5 additions & 5 deletions bittensor/_subtensor/extrinsics/delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def nominate_extrinsic(

with bittensor.__console__.status(":satellite: Sending nominate call on [white]{}[/white] ...".format(subtensor.network)):
try:
success = subtensor._do_nominate(
success, error_msg = subtensor._do_nominate(
wallet = wallet,
wait_for_inclusion = wait_for_inclusion,
wait_for_finalization = wait_for_finalization
Expand Down Expand Up @@ -150,7 +150,7 @@ def delegate_extrinsic(

try:
with bittensor.__console__.status(":satellite: Staking to: [bold white]{}[/bold white] ...".format(subtensor.network)):
staking_response: bool = subtensor._do_delegation(
staking_response, error_msg = subtensor._do_delegation(
wallet = wallet,
delegate_ss58 = delegate_ss58,
amount = staking_balance,
Expand Down Expand Up @@ -178,7 +178,7 @@ def delegate_extrinsic(
bittensor.__console__.print("Stake:\n [blue]{}[/blue] :arrow_right: [green]{}[/green]".format( my_prev_delegated_stake, new_delegate_stake ))
return True
else:
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: Error unknown.")
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: Error:\n {error_msg}.")
return False

except NotRegisteredError as e:
Expand Down Expand Up @@ -258,7 +258,7 @@ def undelegate_extrinsic(

try:
with bittensor.__console__.status(":satellite: Unstaking from: [bold white]{}[/bold white] ...".format(subtensor.network)):
staking_response: bool = subtensor._do_undelegation(
staking_response, err_msg = subtensor._do_undelegation(
wallet = wallet,
delegate_ss58 = delegate_ss58,
amount = unstaking_balance,
Expand Down Expand Up @@ -286,7 +286,7 @@ def undelegate_extrinsic(
bittensor.__console__.print("Stake:\n [blue]{}[/blue] :arrow_right: [green]{}[/green]".format( my_prev_delegated_stake, new_delegate_stake ))
return True
else:
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: Error unknown.")
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: Error:\n {error_msg}.")
return False

except NotRegisteredError as e:
Expand Down
12 changes: 7 additions & 5 deletions bittensor/_subtensor/extrinsics/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def add_stake_multiple_extrinsic (
continue

try:
staking_response: bool = __do_add_stake_single(
staking_response, error_msg = __do_add_stake_single(
subtensor = subtensor,
wallet = wallet,
hotkey_ss58 = hotkey_ss58,
Expand Down Expand Up @@ -306,7 +306,7 @@ def add_stake_multiple_extrinsic (
break

else:
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: Error unknown.")
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: Error:\n {error_msg}.")
continue

except NotRegisteredError as e:
Expand All @@ -332,7 +332,7 @@ def __do_add_stake_single(
amount: 'bittensor.Balance',
wait_for_inclusion: bool = True,
wait_for_finalization: bool = False,
) -> bool:
) -> Tuple[bool, Optional[str]]:
r"""
Executes a stake call to the chain using the wallet and amount specified.
Args:
Expand All @@ -354,6 +354,8 @@ def __do_add_stake_single(
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.
error_msg (Optional[str]):
If the extrinsic failed, the error message is returned.
Raises:
StakeError:
If the extrinsic fails to be finalized or included in the block.
Expand All @@ -374,13 +376,13 @@ def __do_add_stake_single(
if not subtensor.is_hotkey_delegate( hotkey_ss58 = hotkey_ss58 ):
raise NotDelegateError("Hotkey: {} is not a delegate.".format(hotkey_ss58))

success = subtensor._do_stake(
success, error_msg = subtensor._do_stake(
wallet = wallet,
hotkey_ss58 = hotkey_ss58,
amount = amount,
wait_for_inclusion = wait_for_inclusion,
wait_for_finalization = wait_for_finalization,
)

return success
return success, error_msg

12 changes: 6 additions & 6 deletions bittensor/_subtensor/extrinsics/unstaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ def __do_remove_stake_single(
# Decrypt keys,
wallet.coldkey

success = subtensor._do_unstake(
success, error_msg = subtensor._do_unstake(
wallet = wallet,
hotkey_ss58 = hotkey_ss58,
amount = amount,
wait_for_inclusion = wait_for_inclusion,
wait_for_finalization = wait_for_finalization,
)

return success
return success, error_msg

def unstake_extrinsic (
subtensor: 'bittensor.Subtensor',
Expand Down Expand Up @@ -137,7 +137,7 @@ def unstake_extrinsic (

try:
with bittensor.__console__.status(":satellite: Unstaking from chain: [white]{}[/white] ...".format(subtensor.network)):
staking_response: bool = __do_remove_stake_single(
staking_response, error_msg = __do_remove_stake_single(
subtensor = subtensor,
wallet = wallet,
hotkey_ss58 = hotkey_ss58,
Expand All @@ -160,7 +160,7 @@ def unstake_extrinsic (
bittensor.__console__.print("Stake:\n [blue]{}[/blue] :arrow_right: [green]{}[/green]".format( old_stake, new_stake ))
return True
else:
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: Error unknown.")
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: Error:\n {error_msg}.")
return False

except NotRegisteredError as e:
Expand Down Expand Up @@ -258,7 +258,7 @@ def unstake_multiple_extrinsic (

try:
with bittensor.__console__.status(":satellite: Unstaking from chain: [white]{}[/white] ...".format(subtensor.network)):
staking_response: bool = __do_remove_stake_single(
staking_response, error_msg = __do_remove_stake_single(
subtensor = subtensor,
wallet = wallet,
hotkey_ss58 = hotkey_ss58,
Expand Down Expand Up @@ -289,7 +289,7 @@ def unstake_multiple_extrinsic (
bittensor.__console__.print("Stake ({}): [blue]{}[/blue] :arrow_right: [green]{}[/green]".format( hotkey_ss58, stake_on_uid, new_stake ))
successful_unstakes += 1
else:
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: Error unknown.")
bittensor.__console__.print(f":cross_mark: [red]Failed[/red]: Error:\n {error_msg}.")
continue

except NotRegisteredError as e:
Expand Down
30 changes: 15 additions & 15 deletions bittensor/_subtensor/subtensor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def _do_serve_axon(
call_params: AxonServeCallParams,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = True,
):
) -> Tuple[bool, Optional[str]]:
with self.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
Expand All @@ -506,7 +506,7 @@ def _do_serve_axon(
else:
return False, response.error_message
else:
return True
return True, None

def serve_prometheus (
self,
Expand Down Expand Up @@ -551,7 +551,7 @@ def _do_serve_prometheus(
else:
return False, response.error_message
else:
return True
return True, None

#################
#### Staking ####
Expand Down Expand Up @@ -1444,7 +1444,7 @@ def _do_delegation(
amount: 'bittensor.Balance',
wait_for_inclusion: bool = True,
wait_for_finalization: bool = False,
) -> bool:
) -> Tuple[bool, Optional[str]]:
with self.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
Expand All @@ -1458,12 +1458,12 @@ def _do_delegation(
response = substrate.submit_extrinsic( extrinsic, wait_for_inclusion = wait_for_inclusion, wait_for_finalization = wait_for_finalization )
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True
return True, None
response.process_events()
if response.is_success:
return True
return True, None
else:
raise StakeError(response.error_message)
raise False, response.error_message

def _do_undelegation(
self,
Expand All @@ -1472,7 +1472,7 @@ def _do_undelegation(
amount: 'bittensor.Balance',
wait_for_inclusion: bool = True,
wait_for_finalization: bool = False,
) -> bool:
) -> Tuple[bool, Optional[str]]::
with self.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
Expand All @@ -1486,19 +1486,19 @@ def _do_undelegation(
response = substrate.submit_extrinsic( extrinsic, wait_for_inclusion = wait_for_inclusion, wait_for_finalization = wait_for_finalization )
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True
return True, None
response.process_events()
if response.is_success:
return True
return True, None
else:
raise StakeError(response.error_message)
raise False, response.error_message

def _do_nominate(
self,
wallet: 'bittensor.wallet',
wait_for_inclusion: bool = True,
wait_for_finalization: bool = False,
) -> bool:
) -> Tuple[bool, Optional[str]]:
with self.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
Expand All @@ -1511,12 +1511,12 @@ def _do_nominate(
response = substrate.submit_extrinsic( extrinsic, wait_for_inclusion = wait_for_inclusion, wait_for_finalization = wait_for_finalization )
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True
return True, None
response.process_events()
if response.is_success:
return True
return True, None
else:
raise NominationError(response.error_message)
raise False, response.error_message

################
#### Legacy ####
Expand Down
12 changes: 6 additions & 6 deletions tests/integration_tests/test_subtensor_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_get_current_block( self ):
assert (type(block) == int)

def test_unstake( self ):
self.subtensor._do_unstake = MagicMock(return_value = True)
self.subtensor._do_unstake = MagicMock(return_value = (True, None))

self.subtensor.substrate.get_payment_info = MagicMock(
return_value = { 'partialFee': 100 }
Expand All @@ -124,7 +124,7 @@ def test_unstake( self ):
self.assertTrue(success, msg="Unstake should succeed")

def test_unstake_inclusion( self ):
self.subtensor._do_unstake = MagicMock(return_value = True)
self.subtensor._do_unstake = MagicMock(return_value = (True, None))

self.subtensor.substrate.get_payment_info = MagicMock(
return_value = { 'partialFee': 100 }
Expand All @@ -142,7 +142,7 @@ def test_unstake_inclusion( self ):
self.assertTrue(success, msg="Unstake should succeed")

def test_unstake_failed( self ):
self.subtensor._do_unstake = MagicMock(return_value = False)
self.subtensor._do_unstake = MagicMock(return_value = (False, "Error"))

self.subtensor.register = MagicMock(return_value = True)
self.subtensor.get_balance = MagicMock(return_value = self.balance)
Expand All @@ -156,7 +156,7 @@ def test_unstake_failed( self ):
self.assertFalse(fail, msg="Unstake should fail")

def test_stake(self):
self.subtensor._do_stake = MagicMock(return_value = True)
self.subtensor._do_stake = MagicMock(return_value = (True, None))

self.subtensor.substrate.get_payment_info = MagicMock(
return_value = { 'partialFee': 100 }
Expand All @@ -174,7 +174,7 @@ def test_stake(self):
self.assertTrue(success, msg="Stake should succeed")

def test_stake_inclusion(self):
self.subtensor._do_stake = MagicMock(return_value = True)
self.subtensor._do_stake = MagicMock(return_value = (True, None))

self.subtensor.substrate.get_payment_info = MagicMock(
return_value = { 'partialFee': 100 }
Expand All @@ -193,7 +193,7 @@ def test_stake_inclusion(self):
self.assertTrue(success, msg="Stake should succeed")

def test_stake_failed( self ):
self.subtensor._do_stake = MagicMock(return_value = False)
self.subtensor._do_stake = MagicMock(return_value = (False, "Error"))

self.subtensor.substrate.get_payment_info = MagicMock(
return_value = { 'partialFee': 100 }
Expand Down

0 comments on commit 0252fcc

Please sign in to comment.