Skip to content

Commit

Permalink
Grab delegates details from GitHub (#1245)
Browse files Browse the repository at this point in the history
* add url to init

* add dataclass and util functions

* use in cli

* remove delegates json

---------

Co-authored-by: joeylegere <[email protected]>
  • Loading branch information
camfairchild and joeylegere authored Apr 4, 2023
1 parent 9a3d8ee commit 6cf0380
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 188 deletions.
3 changes: 3 additions & 0 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def turn_console_off():
# Pip address for versioning
__pipaddress__ = 'https://pypi.org/pypi/bittensor/json'

# Raw github url for delegates registry file
__delegates_details_url__: str = "https://raw.githubusercontent.com/opentensor/bittensor-delegates/main/public/delegates.json"

# Substrate ss58_format
__ss58_format__ = 42

Expand Down
34 changes: 12 additions & 22 deletions bittensor/_cli/commands/delegates.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
from rich.prompt import Confirm
from rich.console import Text
from tqdm import tqdm
from .utils import get_delegates_details, DelegatesDetails

import os
import bittensor
from typing import List
from typing import List, Dict, Optional

def _get_coldkey_wallets_for_path( path: str ) -> List['bittensor.wallet']:
try:
Expand All @@ -50,15 +51,10 @@ def show_delegates( delegates: List['bittensor.DelegateInfo'], prev_delegates: L
prev_delegates_dict = {}
for prev_delegate in prev_delegates:
prev_delegates_dict[prev_delegate.hotkey_ss58] = prev_delegate
try:
package_dir = os.path.dirname(bittensor.__file__)
root_dir = os.path.dirname(package_dir)
filename = os.path.join(root_dir, 'delegates.json')
if os.path.exists(filename):
registered_delegate_info = json.load( open(filename, 'r') )
else:
registered_delegate_info = {}
except:

registered_delegate_info: Optional[Dict[str, DelegatesDetails]] = get_delegates_details(url = bittensor.__delegates_details_url__)
if registered_delegate_info is None:
bittensor.__console__.print( ':warning:[yellow]Could not get delegate info from chain.[/yellow]')
registered_delegate_info = {}

table = Table(show_footer=True, width=width, pad_edge=False, box=None, expand=True)
Expand All @@ -85,9 +81,9 @@ def show_delegates( delegates: List['bittensor.DelegateInfo'], prev_delegates: L
bittensor.Balance.from_rao(0) # default to 0 if no owner stake.
)
if delegate.hotkey_ss58 in registered_delegate_info:
delegate_name = registered_delegate_info[delegate.hotkey_ss58]['name']
delegate_url = registered_delegate_info[delegate.hotkey_ss58]['url']
delegate_description = registered_delegate_info[delegate.hotkey_ss58]['description']
delegate_name = registered_delegate_info[delegate.hotkey_ss58].name
delegate_url = registered_delegate_info[delegate.hotkey_ss58].url
delegate_description = registered_delegate_info[delegate.hotkey_ss58].description
else:
delegate_name = ''
delegate_url = ''
Expand Down Expand Up @@ -441,15 +437,9 @@ def run( cli ):

delegates.sort(key=lambda delegate: delegate[0].total_stake, reverse=True)

try:
package_dir = os.path.dirname(bittensor.__file__)
root_dir = os.path.dirname(package_dir)
filename = os.path.join(root_dir, 'delegates.json')
if os.path.exists(filename):
registered_delegate_info = json.load( open(filename, 'r') )
else:
registered_delegate_info = {}
except:
registered_delegate_info: Optional[DelegatesDetails] = get_delegates_details(url = bittensor.__delegates_details_url__)
if registered_delegate_info is None:
bittensor.__console__.print( ':warning:[yellow]Could not get delegate info from chain.[/yellow]')
registered_delegate_info = {}

for i, delegate in enumerate( delegates ):
Expand Down
18 changes: 6 additions & 12 deletions bittensor/_cli/commands/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
from tqdm import tqdm
from rich.table import Table
from rich.prompt import Prompt
from .utils import check_netuid_set
from .utils import check_netuid_set, get_delegates_details, DelegatesDetails
console = bittensor.__console__

import os
import bittensor
from typing import List, Tuple
from typing import List, Tuple, Optional, Dict

def _get_coldkey_wallets_for_path( path: str ) -> List['bittensor.wallet']:
try:
Expand Down Expand Up @@ -66,15 +66,9 @@ def run (cli):

netuids = subtensor.get_all_subnet_netuids()

try:
package_dir = os.path.dirname(bittensor.__file__)
root_dir = os.path.dirname(package_dir)
filename = os.path.join(root_dir, 'delegates.json')
if os.path.exists(filename):
registered_delegate_info = json.load( open(filename, 'r') )
else:
registered_delegate_info = {}
except:
registered_delegate_info: Optional[Dict[str, DelegatesDetails]] = get_delegates_details(url = bittensor.__delegates_details_url__)
if registered_delegate_info is None:
bittensor.__console__.print( ':warning:[yellow]Could not get delegate info from chain.[/yellow]')
registered_delegate_info = {}

neuron_state_dict = {}
Expand Down Expand Up @@ -108,7 +102,7 @@ def run (cli):
)
for dele, staked in delegates:
if dele.hotkey_ss58 in registered_delegate_info:
delegate_name = registered_delegate_info[dele.hotkey_ss58]['name']
delegate_name = registered_delegate_info[dele.hotkey_ss58].name
else:
delegate_name = dele.hotkey_ss58
table.add_row(
Expand Down
41 changes: 39 additions & 2 deletions bittensor/_cli/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import os
import torch
import bittensor
from typing import List, Dict
from typing import List, Dict, Any, Optional
from rich.prompt import Confirm, Prompt, PromptBase
import requests
from dataclasses import dataclass
console = bittensor.__console__

class IntListPrompt(PromptBase):
Expand Down Expand Up @@ -126,4 +128,39 @@ def get_all_wallets_for_path( path:str ) -> List['bittensor.wallet']:
for cold_wallet in cold_wallets:
if cold_wallet.coldkeypub_file.exists_on_device() and not cold_wallet.coldkeypub_file.is_encrypted():
all_wallets.extend( get_hotkey_wallets_for_wallet(cold_wallet) )
return all_wallets
return all_wallets

@dataclass
class DelegatesDetails:
name: str
url: str
description: str
signature: str

@classmethod
def from_json(cls, json: Dict[str, any]) -> 'DelegatesDetails':
return cls(
name=json['name'],
url=json['url'],
description=json['description'],
signature=json['signature'],
)

def _get_delegates_details_from_github(requests_get, url: str) -> Dict[str, DelegatesDetails]:
response = requests_get(url)


if response.status_code == 200:
all_delegates: Dict[str, Any] = response.json()
all_delegates_details = {}
for delegate_hotkey, delegates_details in all_delegates.items():
all_delegates_details[delegate_hotkey] = DelegatesDetails.from_json(delegates_details)
return all_delegates_details
else:
return {}

def get_delegates_details(url: str) -> Optional[Dict[str, DelegatesDetails]]:
try:
return _get_delegates_details_from_github(requests.get, url)
except Exception:
return None # Fail silently
152 changes: 0 additions & 152 deletions delegates.json

This file was deleted.

0 comments on commit 6cf0380

Please sign in to comment.