From 0100d8dd67e467e1bf5ad37a2059a1a59392721d Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Fri, 8 Aug 2025 14:56:40 +0200 Subject: [PATCH 1/3] WIP checkin --- bittensor_cli/cli.py | 18 ++++++++++++++---- bittensor_cli/src/__init__.py | 1 + .../src/bittensor/subtensor_interface.py | 16 +++++++--------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index c26defd6d..a85ae9d26 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -618,6 +618,7 @@ def __init__(self): "wallet_hotkey": None, "network": None, "use_cache": True, + "disk_cache": False, "rate_tolerance": None, "safe_staking": True, "allow_partial_stake": False, @@ -1087,6 +1088,7 @@ def initialize_chain( "Verify this is intended.", ) if not self.subtensor: + use_disk_cache = self.config.get("disk_cache", False) if network: network_ = None for item in network: @@ -1103,15 +1105,15 @@ def initialize_chain( f"[{COLORS.G.ARG}]{', '.join(not_selected_networks)}[/{COLORS.G.ARG}]" ) - self.subtensor = SubtensorInterface(network_) + self.subtensor = SubtensorInterface(network_, use_disk_cache=use_disk_cache) elif self.config["network"]: console.print( f"Using the specified network [{COLORS.G.LINKS}]{self.config['network']}" f"[/{COLORS.G.LINKS}] from config" ) - self.subtensor = SubtensorInterface(self.config["network"]) + self.subtensor = SubtensorInterface(self.config["network"], use_disk_cache=use_disk_cache) else: - self.subtensor = SubtensorInterface(defaults.subtensor.network) + self.subtensor = SubtensorInterface(defaults.subtensor.network, use_disk_cache=use_disk_cache) return self.subtensor def _run_command(self, cmd: Coroutine, exit_early: bool = True): @@ -1268,6 +1270,13 @@ def set_config( help="Disable caching of some commands. This will disable the `--reuse-last` and `--html` flags on " "commands such as `subnets metagraph`, `stake show` and `subnets list`.", ), + disk_cache: Optional[bool] = typer.Option( + None, + "--disk-cache/--no-disk-cache", + " /--no-disk-cache", + help="Enables or disables the caching on disk. Enabling this can significantly speed up commands run " + "sequentially" + ), rate_tolerance: Optional[float] = typer.Option( None, "--tolerance", @@ -1314,12 +1323,13 @@ def set_config( "wallet_hotkey": wallet_hotkey, "network": network, "use_cache": use_cache, + "disk_cache": disk_cache, "rate_tolerance": rate_tolerance, "safe_staking": safe_staking, "allow_partial_stake": allow_partial_stake, "dashboard_path": dashboard_path, } - bools = ["use_cache", "safe_staking", "allow_partial_stake"] + bools = ["use_cache", "disk_cache", "safe_staking", "allow_partial_stake"] if all(v is None for v in args.values()): # Print existing configs self.get_config() diff --git a/bittensor_cli/src/__init__.py b/bittensor_cli/src/__init__.py index 7cd09ab84..afabc4249 100644 --- a/bittensor_cli/src/__init__.py +++ b/bittensor_cli/src/__init__.py @@ -94,6 +94,7 @@ class config: "wallet_name": None, "wallet_hotkey": None, "use_cache": True, + "disk_cache": False, "metagraph_cols": { "UID": True, "GLOBAL_STAKE": True, diff --git a/bittensor_cli/src/bittensor/subtensor_interface.py b/bittensor_cli/src/bittensor/subtensor_interface.py index 0684b31ef..ffe8d78e2 100644 --- a/bittensor_cli/src/bittensor/subtensor_interface.py +++ b/bittensor_cli/src/bittensor/subtensor_interface.py @@ -42,12 +42,6 @@ get_hotkey_pub_ss58, ) -SubstrateClass = ( - DiskCachedAsyncSubstrateInterface - if os.getenv("DISK_CACHE", "0") == "1" - else AsyncSubstrateInterface -) - class ParamWithTypes(TypedDict): name: str # Name of the parameter. @@ -81,7 +75,7 @@ class SubtensorInterface: Thin layer for interacting with Substrate Interface. Mostly a collection of frequently-used calls. """ - def __init__(self, network): + def __init__(self, network, use_disk_cache: bool = False): if network in Constants.network_map: self.chain_endpoint = Constants.network_map[network] self.network = network @@ -111,8 +105,12 @@ def __init__(self, network): ) self.chain_endpoint = Constants.network_map[defaults.subtensor.network] self.network = defaults.subtensor.network - - self.substrate = SubstrateClass( + substrate_class = ( + DiskCachedAsyncSubstrateInterface + if (use_disk_cache or os.getenv("DISK_CACHE", "0") == "1") + else AsyncSubstrateInterface + ) + self.substrate = substrate_class( url=self.chain_endpoint, ss58_format=SS58_FORMAT, type_registry=TYPE_REGISTRY, From 5f5225819f489542bda6c5836935daeb32981e39 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 12 Aug 2025 22:38:20 +0200 Subject: [PATCH 2/3] Optimise btcli s list for disk caching --- bittensor_cli/src/commands/subnets/price.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bittensor_cli/src/commands/subnets/price.py b/bittensor_cli/src/commands/subnets/price.py index 38a20d00d..8e488a95c 100644 --- a/bittensor_cli/src/commands/subnets/price.py +++ b/bittensor_cli/src/commands/subnets/price.py @@ -52,7 +52,17 @@ async def price( step = 300 start_block = max(0, current_block - total_blocks) - block_numbers = list(range(start_block, current_block + 1, step)) + + # snap start block down to nearest multiple of 10 + start_block -= start_block % 10 + + block_numbers = [] + for b in range(start_block, current_block + 1, step): + if b == current_block: + block_numbers.append(b) # exact current block + else: + block_numbers.append(b - (b % 5)) # snap down to multiple of 10 + block_numbers = sorted(set(block_numbers)) # Block hashes block_hash_cors = [ From 4f77804d675e6011757f700033e3a7c0f2886771 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 12 Aug 2025 22:38:28 +0200 Subject: [PATCH 3/3] Ruff --- bittensor_cli/cli.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/bittensor_cli/cli.py b/bittensor_cli/cli.py index eeba4d613..6604c08b7 100755 --- a/bittensor_cli/cli.py +++ b/bittensor_cli/cli.py @@ -1110,15 +1110,21 @@ def initialize_chain( f"[{COLORS.G.ARG}]{', '.join(not_selected_networks)}[/{COLORS.G.ARG}]" ) - self.subtensor = SubtensorInterface(network_, use_disk_cache=use_disk_cache) + self.subtensor = SubtensorInterface( + network_, use_disk_cache=use_disk_cache + ) elif self.config["network"]: console.print( f"Using the specified network [{COLORS.G.LINKS}]{self.config['network']}" f"[/{COLORS.G.LINKS}] from config" ) - self.subtensor = SubtensorInterface(self.config["network"], use_disk_cache=use_disk_cache) + self.subtensor = SubtensorInterface( + self.config["network"], use_disk_cache=use_disk_cache + ) else: - self.subtensor = SubtensorInterface(defaults.subtensor.network, use_disk_cache=use_disk_cache) + self.subtensor = SubtensorInterface( + defaults.subtensor.network, use_disk_cache=use_disk_cache + ) return self.subtensor def _run_command(self, cmd: Coroutine, exit_early: bool = True): @@ -1280,7 +1286,7 @@ def set_config( "--disk-cache/--no-disk-cache", " /--no-disk-cache", help="Enables or disables the caching on disk. Enabling this can significantly speed up commands run " - "sequentially" + "sequentially", ), rate_tolerance: Optional[float] = typer.Option( None,