Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] only reregister if flag is set #937

Merged
merged 9 commits into from
Oct 12, 2022
3 changes: 2 additions & 1 deletion bittensor/_cli/cli_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def run_miner ( self ):
wallet.coldkeypub

# Check registration
self.register()
## Will exit if --wallet.reregister is False
wallet.reregister()

# Run miner.
if self.config.model == 'core_server':
Expand Down
2 changes: 1 addition & 1 deletion bittensor/_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __new__( cls, parser: ArgumentParser = None, strict: bool = False, args: Opt

# 1.1 Optionally load defaults if the --config is set.
try:
config_file_path = str(os.getcwd()) + '/' + vars(parser.parse_known_args()[0])['config']
config_file_path = str(os.getcwd()) + '/' + vars(parser.parse_known_args(args)[0])['config']
except Exception as e:
config_file_path = None

Expand Down
3 changes: 2 additions & 1 deletion bittensor/_wallet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import argparse
import copy
from distutils.util import strtobool
import os

import bittensor
Expand Down Expand Up @@ -114,7 +115,7 @@ def add_args(cls, parser: argparse.ArgumentParser, prefix: str = None ):

parser.add_argument('--' + prefix_str + 'wallet.hotkeys', '--' + prefix_str + 'wallet.exclude_hotkeys', required=False, action='store', default=bittensor.defaults.wallet.hotkeys, type=str, nargs='*', help='''Specify the hotkeys by name. (e.g. hk1 hk2 hk3)''')
parser.add_argument('--' + prefix_str + 'wallet.all_hotkeys', required=False, action='store_true', default=bittensor.defaults.wallet.all_hotkeys, help='''To specify all hotkeys. Specifying hotkeys will exclude them from this all.''')
parser.add_argument('--' + prefix_str + 'wallet.reregister', required=False, action='store', default=bittensor.defaults.wallet.reregister, type=bool, help='''Whether to reregister the wallet if it is not already registered.''')
parser.add_argument('--' + prefix_str + 'wallet.reregister', required=False, default=bittensor.defaults.wallet.reregister, type=lambda x: bool(strtobool(x)), help='''Whether to reregister the wallet if it is not already registered.''')

except argparse.ArgumentError as e:
pass
Expand Down
32 changes: 28 additions & 4 deletions tests/integration_tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,32 @@ def test_btcli_help():
assert 'overview' in help_out
assert 'run' in help_out

class TestCLIUsingArgs(unittest.TestCase):
"""
Test the CLI by passing args directly to the bittensor.cli factory
"""
def test_run_reregister_false(self):
"""
Verify that the btcli run command does not reregister a not registered wallet
if --wallet.reregister is False
"""

with patch('bittensor.Wallet.is_registered', MagicMock(return_value=False)) as mock_wallet_is_reg: # Wallet is not registered
with patch('bittensor.Subtensor.register', MagicMock(side_effect=Exception("shouldn't register during test"))):
with pytest.raises(SystemExit):
cli = bittensor.cli(args=[
'run',
'--wallet.name', 'mock',
'--wallet.hotkey', 'mock_hotkey',
'--wallet._mock', 'True',
'--subtensor.network', 'mock',
'--subtensor._mock', 'True',
'--no_prompt',
'--wallet.reregister', 'False' # Don't reregister
])
cli.run()

args, kwargs = mock_wallet_is_reg.call_args
# args[0] should be self => the wallet
assert args[0].config.wallet.reregister == False

if __name__ == "__main__":
cli = TestCli()
cli.setUp()
cli.test_stake()