|
42 | 42 | from bittensor_cli.src.bittensor import utils
|
43 | 43 | from bittensor_cli.src.bittensor.balances import Balance
|
44 | 44 | from bittensor_cli.src.bittensor.chain_data import SubnetHyperparameters
|
45 |
| -from bittensor_cli.src.bittensor.subtensor_interface import SubtensorInterface |
| 45 | +from bittensor_cli.src.bittensor.subtensor_interface import ( |
| 46 | + SubtensorInterface, |
| 47 | + best_connection, |
| 48 | +) |
46 | 49 | from bittensor_cli.src.bittensor.utils import (
|
47 | 50 | console,
|
48 | 51 | err_console,
|
@@ -619,7 +622,7 @@ class CLIManager:
|
619 | 622 | wallet_app: typer.Typer
|
620 | 623 | subnets_app: typer.Typer
|
621 | 624 | weights_app: typer.Typer
|
622 |
| - utils_app = typer.Typer(epilog=_epilog) |
| 625 | + utils_app: typer.Typer |
623 | 626 | view_app: typer.Typer
|
624 | 627 | asyncio_runner = asyncio
|
625 | 628 |
|
@@ -692,6 +695,7 @@ def __init__(self):
|
692 | 695 | self.weights_app = typer.Typer(epilog=_epilog)
|
693 | 696 | self.view_app = typer.Typer(epilog=_epilog)
|
694 | 697 | self.liquidity_app = typer.Typer(epilog=_epilog)
|
| 698 | + self.utils_app = typer.Typer(epilog=_epilog) |
695 | 699 |
|
696 | 700 | # config alias
|
697 | 701 | self.app.add_typer(
|
@@ -766,7 +770,7 @@ def __init__(self):
|
766 | 770 |
|
767 | 771 | # utils app
|
768 | 772 | self.app.add_typer(
|
769 |
| - self.utils_app, name="utils", no_args_is_help=True, hidden=True |
| 773 | + self.utils_app, name="utils", no_args_is_help=True, hidden=False |
770 | 774 | )
|
771 | 775 |
|
772 | 776 | # view app
|
@@ -1048,6 +1052,10 @@ def __init__(self):
|
1048 | 1052 | "remove", rich_help_panel=HELP_PANELS["LIQUIDITY"]["LIQUIDITY_MGMT"]
|
1049 | 1053 | )(self.liquidity_remove)
|
1050 | 1054 |
|
| 1055 | + # utils app |
| 1056 | + self.utils_app.command("convert")(self.convert) |
| 1057 | + self.utils_app.command("latency")(self.best_connection) |
| 1058 | + |
1051 | 1059 | def generate_command_tree(self) -> Tree:
|
1052 | 1060 | """
|
1053 | 1061 | Generates a rich.Tree of the commands, subcommands, and groups of this app
|
@@ -6296,7 +6304,6 @@ def liquidity_modify(
|
6296 | 6304 | )
|
6297 | 6305 |
|
6298 | 6306 | @staticmethod
|
6299 |
| - @utils_app.command("convert") |
6300 | 6307 | def convert(
|
6301 | 6308 | from_rao: Optional[str] = typer.Option(
|
6302 | 6309 | None, "--rao", help="Convert amount from Rao"
|
@@ -6326,6 +6333,66 @@ def convert(
|
6326 | 6333 | f"{Balance.from_tao(tao).rao}{Balance.rao_unit}",
|
6327 | 6334 | )
|
6328 | 6335 |
|
| 6336 | + def best_connection( |
| 6337 | + self, |
| 6338 | + additional_networks: Optional[list[str]] = typer.Option( |
| 6339 | + None, |
| 6340 | + "--network", |
| 6341 | + help="Network(s) to test for the best connection", |
| 6342 | + ), |
| 6343 | + ): |
| 6344 | + """ |
| 6345 | + This command will give you the latency of all finney-like network in additional to any additional networks you specify via the '--network' flag |
| 6346 | +
|
| 6347 | + The results are three-fold. One column is the overall time to initialise a connection, send the requests, and wait for the results. The second column measures single ping-pong speed once connected. The third makes a real world call to fetch the chain head. |
| 6348 | +
|
| 6349 | + EXAMPLE |
| 6350 | +
|
| 6351 | + [green]$[/green] btcli utils latency --network ws://189.234.12.45 --network wss://mysubtensor.duckdns.org |
| 6352 | +
|
| 6353 | + """ |
| 6354 | + additional_networks = additional_networks or [] |
| 6355 | + if any(not x.startswith("ws") for x in additional_networks): |
| 6356 | + err_console.print( |
| 6357 | + "Invalid network endpoint. Ensure you are specifying a valid websocket endpoint" |
| 6358 | + f" (starting with [{COLORS.G.LINKS}]ws://[/{COLORS.G.LINKS}] or " |
| 6359 | + f"[{COLORS.G.LINKS}]wss://[/{COLORS.G.LINKS}]).", |
| 6360 | + ) |
| 6361 | + return False |
| 6362 | + results: dict[str, list[float]] = self._run_command( |
| 6363 | + best_connection(Constants.lite_nodes + additional_networks) |
| 6364 | + ) |
| 6365 | + sorted_results = { |
| 6366 | + k: v for k, v in sorted(results.items(), key=lambda item: item[1][0]) |
| 6367 | + } |
| 6368 | + table = Table( |
| 6369 | + Column("Network"), |
| 6370 | + Column("End to End Latency", style="cyan"), |
| 6371 | + Column("Single Request Ping", style="cyan"), |
| 6372 | + Column("Chain Head Request Latency", style="cyan"), |
| 6373 | + title="Connection Latencies (seconds)", |
| 6374 | + caption="lower value is faster", |
| 6375 | + ) |
| 6376 | + for n_name, ( |
| 6377 | + overall_latency, |
| 6378 | + single_request, |
| 6379 | + chain_head, |
| 6380 | + ) in sorted_results.items(): |
| 6381 | + table.add_row( |
| 6382 | + n_name, str(overall_latency), str(single_request), str(chain_head) |
| 6383 | + ) |
| 6384 | + console.print(table) |
| 6385 | + fastest = next(iter(sorted_results.keys())) |
| 6386 | + if conf_net := self.config.get("network", ""): |
| 6387 | + if not conf_net.startswith("ws") and conf_net in Constants.networks: |
| 6388 | + conf_net = Constants.network_map[conf_net] |
| 6389 | + if conf_net != fastest: |
| 6390 | + console.print( |
| 6391 | + f"The fastest network is {fastest}. You currently have {conf_net} selected as your default network." |
| 6392 | + f"\nYou can update this with {arg__(f'btcli config set --network {fastest}')}" |
| 6393 | + ) |
| 6394 | + return True |
| 6395 | + |
6329 | 6396 | def run(self):
|
6330 | 6397 | self.app()
|
6331 | 6398 |
|
|
0 commit comments