Skip to content

Commit

Permalink
Parachain registration (#912)
Browse files Browse the repository at this point in the history
* removed ws assumption

* removing check

* never registered

* Fixed sched_getaffinity for mac osx

* Started adding parachain support

* [hot-fix] fix indent again. add test (#907)

fix indent again. add test

* Fixed registration check and first time registration

* Removed old entrypoint list structure

* Fixed unit tests

Co-authored-by: Eugene <[email protected]>
Co-authored-by: Ala Shaabana <[email protected]>
Co-authored-by: Cameron Fairchild <[email protected]>
  • Loading branch information
4 people authored Sep 27, 2022
1 parent 00bc477 commit 06b8541
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 36 deletions.
25 changes: 12 additions & 13 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,27 @@ def turn_console_off():
# Wallet ss58 address length
__ss58_address_length__ = 48

__networks__ = [ 'local', 'nobunaga', 'nakamoto']
__networks__ = [ 'local', 'bellagene', 'nobunaga', 'nakamoto']

__datasets__ = ['ArXiv', 'BookCorpus2', 'Books3', 'DMMathematics', 'EnronEmails', 'EuroParl', 'Gutenberg_PG', 'HackerNews', 'NIHExPorter', 'OpenSubtitles', 'PhilPapers', 'UbuntuIRC', 'YoutubeSubtitles']

__nakamoto_entrypoints__ = [
"AtreusLB-2c6154f73e6429a9.elb.us-east-2.amazonaws.com:9944"
]
__nakamoto_entrypoint__ = "AtreusLB-2c6154f73e6429a9.elb.us-east-2.amazonaws.com:9944"

__nobunaga_entrypoints__ = [
'staging.nobunaga.opentensor.ai:9944'
]

__local_entrypoints__ = [
'127.0.0.1:9944'
]
__nobunaga_entrypoint__ = "staging.nobunaga.opentensor.ai:9944"


__bellagene_entrypoint__ = "parachain.opentensor.ai:443"


__local_entrypoint__ = "127.0.0.1:9944"


# Avoid collisions with other processes
from .utils.test_utils import get_random_unused_port
mock_subtensor_port = get_random_unused_port()
__mock_entrypoints__ = [
f"localhost:{mock_subtensor_port}"
]
__mock_entrypoint__ = f"localhost:{mock_subtensor_port}"



# ---- Config ----
Expand Down
23 changes: 15 additions & 8 deletions bittensor/_subtensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ def __new__(
else:
config.subtensor.chain_endpoint = subtensor.determine_chain_endpoint( bittensor.defaults.subtensor.network )
config.subtensor.network = bittensor.defaults.subtensor.network

# make sure it's wss:// or ws://
# If it's bellagene (parachain testnet) then it has to be wss
endpoint_url: str = config.subtensor.chain_endpoint
if endpoint_url[0:6] != "wss://" and endpoint_url[0:5] != "ws://":
endpoint_url = "ws://{}".format(endpoint_url)

if config.subtensor.network == "bellagene":
endpoint_url = "wss://{}".format(endpoint_url)
else:
endpoint_url = "ws://{}".format(endpoint_url)

substrate = SubstrateInterface(
ss58_format = bittensor.__ss58_format__,
type_registry_preset='substrate-node-template',
Expand Down Expand Up @@ -235,14 +239,17 @@ def check_config( config: 'bittensor.Config' ):
def determine_chain_endpoint(network: str):
if network == "nakamoto":
# Main network.
return bittensor.__nakamoto_entrypoints__[0]
return bittensor.__nakamoto_entrypoint__
elif network == "nobunaga":
# Staging network.
return bittensor.__nobunaga_entrypoints__[0]
return bittensor.__nobunaga_entrypoint__
elif network == "bellagene":
# Parachain test net
return bittensor.__bellagene_entrypoint__
elif network == "local":
# Local chain.
return bittensor.__local_entrypoints__[0]
return bittensor.__local_entrypoint__
elif network == 'mock':
return bittensor.__mock_entrypoints__[0]
return bittensor.__mock_entrypoint__
else:
return bittensor.__local_entrypoints__[0]
return bittensor.__local_entrypoint__
11 changes: 5 additions & 6 deletions bittensor/_subtensor/subtensor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,10 @@ def register (
"""

with bittensor.__console__.status(":satellite: Checking Account..."):
neuron = self.neuron_for_pubkey( wallet.hotkey.ss58_address )
if not neuron.is_null:
bittensor.__console__.print(":white_heavy_check_mark: [green]Already Registered[/green]:\n uid: [bold white]{}[/bold white]\n hotkey: [bold white]{}[/bold white]\n coldkey: [bold white]{}[/bold white]".format(neuron.uid, neuron.hotkey, neuron.coldkey))
return True
neuron = self.neuron_for_pubkey( wallet.hotkey.ss58_address )
if not neuron.is_null:
bittensor.__console__.print(":white_heavy_check_mark: [green]Already Registered[/green]:\n uid: [bold white]{}[/bold white]\n hotkey: [bold white]{}[/bold white]\n coldkey: [bold white]{}[/bold white]".format(neuron.uid, neuron.hotkey, neuron.coldkey))
return True

if prompt:
if not Confirm.ask("Continue Registration?\n hotkey: [bold white]{}[/bold white]\n coldkey: [bold white]{}[/bold white]\n network: [bold white]{}[/bold white]".format( wallet.hotkey.ss58_address, wallet.coldkeypub.ss58_address, self.network ) ):
Expand Down Expand Up @@ -562,9 +562,8 @@ def register (
else:
# Exited loop because pow is no longer valid.
bittensor.__console__.print( "[red]POW is stale.[/red]" )
# Try again.
# Try again
continue

if attempts < max_allowed_attempts:
#Failed registration, retry pow
attempts += 1
Expand Down
4 changes: 2 additions & 2 deletions bittensor/_subtensor/subtensor_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def mock(cls):
_owned_mock_subtensor_process = None
print ('Mock subtensor already running.')

endpoint = bittensor.__mock_entrypoints__[0]
endpoint = bittensor.__mock_entrypoint__
port = int(endpoint.split(':')[1])
substrate = SubstrateInterface(
ss58_format = bittensor.__ss58_format__,
Expand Down Expand Up @@ -105,7 +105,7 @@ def create_global_mock_process(self):
try:
operating_system = "OSX" if platform == "darwin" else "Linux"
path = "./bin/chain/{}/node-subtensor".format(operating_system)
ws_port = int(bittensor.__mock_entrypoints__[0].split(':')[1])
ws_port = int(bittensor.__mock_entrypoint__.split(':')[1])
print(ws_port)
print(os.getpid())
baseport = get_random_unused_port()
Expand Down
3 changes: 0 additions & 3 deletions bittensor/_wallet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ def add_args(cls, parser: argparse.ArgumentParser, prefix: str = None ):
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.''')

except argparse.ArgumentError as e:
import pdb
#pdb.set_trace()
# re-parsing arguments.
pass

@classmethod
Expand Down
1 change: 1 addition & 0 deletions bittensor/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import binascii
import hashlib
from inspect import Attribute
import math
import multiprocessing
import numbers
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def setUp(self):
assert True

def test_defaults_to_nobunaga( self ):
assert self.subtensor.endpoint_for_network() in bittensor.__nobunaga_entrypoints__
assert self.subtensor.endpoint_for_network() == bittensor.__nobunaga_entrypoint__

def test_networks( self ):
assert self.subtensor.endpoint_for_network() in bittensor.__nobunaga_entrypoints__
assert self.subtensor.endpoint_for_network() == bittensor.__nobunaga_entrypoint__

def test_network_overrides( self ):
config = bittensor.subtensor.config()
subtensor = bittensor.subtensor(network='nobunaga', config=config, )
assert subtensor.endpoint_for_network() in bittensor.__nobunaga_entrypoints__
assert subtensor.endpoint_for_network() == bittensor.__nobunaga_entrypoint__

def test_connect_no_failure( self ):
self.subtensor.connect(timeout = 1, failure=False)
Expand Down
57 changes: 56 additions & 1 deletion tests/unit_tests/bittensor_tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from _pytest.fixtures import fixture
from loguru import logger

from types import SimpleNamespace

from unittest.mock import MagicMock, patch


Expand Down Expand Up @@ -400,5 +402,58 @@ class MockException(Exception):
assert call_params['nonce'] == mock_result['nonce']


def test_pow_called_for_cuda():
class MockException(Exception):
pass
mock_compose_call = MagicMock(side_effect=MockException)

mock_subtensor = bittensor.subtensor(_mock=True)
mock_subtensor.neuron_for_pubkey=MagicMock(is_null=True)
mock_subtensor.substrate = MagicMock(
__enter__= MagicMock(return_value=MagicMock(
compose_call=mock_compose_call
)),
__exit__ = MagicMock(return_value=None),
)

mock_wallet = SimpleNamespace(
hotkey=SimpleNamespace(
ss58_address=''
),
coldkeypub=SimpleNamespace(
ss58_address=''
)
)

mock_result = {
"block_number": 1,
'nonce': random.randint(0, pow(2, 32)),
'work': b'\x00' * 64,
}

with patch('bittensor.utils.POWNotStale', return_value=True) as mock_pow_not_stale:
with patch('torch.cuda.is_available', return_value=True) as mock_cuda_available:
with patch('bittensor.utils.create_pow', return_value=mock_result) as mock_create_pow:
with patch('bittensor.utils.hex_bytes_to_u8_list', return_value=b''):

# Should exit early
with pytest.raises(MockException):
mock_subtensor.register(mock_wallet, cuda=True, prompt=False)

mock_pow_not_stale.assert_called_once()
mock_create_pow.assert_called_once()
mock_cuda_available.assert_called_once()

call0 = mock_pow_not_stale.call_args
assert call0[0][0] == mock_subtensor
assert call0[0][1] == mock_result

mock_compose_call.assert_called_once()
call1 = mock_compose_call.call_args
assert call1[1]['call_function'] == 'register'
call_params = call1[1]['call_params']
assert call_params['nonce'] == mock_result['nonce']


if __name__ == "__main__":
test_solve_for_difficulty_fast_registered_already()
test_solve_for_difficulty_fast_registered_already()

0 comments on commit 06b8541

Please sign in to comment.