Skip to content

Commit ab51411

Browse files
authored
Merge pull request #588 from opentensor/feat/thewhaleking/disk-cache-config
disk cache in config
2 parents bbc4705 + 4f77804 commit ab51411

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

bittensor_cli/cli.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ def __init__(self):
623623
"wallet_hotkey": None,
624624
"network": None,
625625
"use_cache": True,
626+
"disk_cache": False,
626627
"rate_tolerance": None,
627628
"safe_staking": True,
628629
"allow_partial_stake": False,
@@ -1092,6 +1093,7 @@ def initialize_chain(
10921093
"Verify this is intended.",
10931094
)
10941095
if not self.subtensor:
1096+
use_disk_cache = self.config.get("disk_cache", False)
10951097
if network:
10961098
network_ = None
10971099
for item in network:
@@ -1108,15 +1110,21 @@ def initialize_chain(
11081110
f"[{COLORS.G.ARG}]{', '.join(not_selected_networks)}[/{COLORS.G.ARG}]"
11091111
)
11101112

1111-
self.subtensor = SubtensorInterface(network_)
1113+
self.subtensor = SubtensorInterface(
1114+
network_, use_disk_cache=use_disk_cache
1115+
)
11121116
elif self.config["network"]:
11131117
console.print(
11141118
f"Using the specified network [{COLORS.G.LINKS}]{self.config['network']}"
11151119
f"[/{COLORS.G.LINKS}] from config"
11161120
)
1117-
self.subtensor = SubtensorInterface(self.config["network"])
1121+
self.subtensor = SubtensorInterface(
1122+
self.config["network"], use_disk_cache=use_disk_cache
1123+
)
11181124
else:
1119-
self.subtensor = SubtensorInterface(defaults.subtensor.network)
1125+
self.subtensor = SubtensorInterface(
1126+
defaults.subtensor.network, use_disk_cache=use_disk_cache
1127+
)
11201128
return self.subtensor
11211129

11221130
def _run_command(self, cmd: Coroutine, exit_early: bool = True):
@@ -1273,6 +1281,13 @@ def set_config(
12731281
help="Disable caching of some commands. This will disable the `--reuse-last` and `--html` flags on "
12741282
"commands such as `subnets metagraph`, `stake show` and `subnets list`.",
12751283
),
1284+
disk_cache: Optional[bool] = typer.Option(
1285+
None,
1286+
"--disk-cache/--no-disk-cache",
1287+
" /--no-disk-cache",
1288+
help="Enables or disables the caching on disk. Enabling this can significantly speed up commands run "
1289+
"sequentially",
1290+
),
12761291
rate_tolerance: Optional[float] = typer.Option(
12771292
None,
12781293
"--tolerance",
@@ -1319,12 +1334,13 @@ def set_config(
13191334
"wallet_hotkey": wallet_hotkey,
13201335
"network": network,
13211336
"use_cache": use_cache,
1337+
"disk_cache": disk_cache,
13221338
"rate_tolerance": rate_tolerance,
13231339
"safe_staking": safe_staking,
13241340
"allow_partial_stake": allow_partial_stake,
13251341
"dashboard_path": dashboard_path,
13261342
}
1327-
bools = ["use_cache", "safe_staking", "allow_partial_stake"]
1343+
bools = ["use_cache", "disk_cache", "safe_staking", "allow_partial_stake"]
13281344
if all(v is None for v in args.values()):
13291345
# Print existing configs
13301346
self.get_config()

bittensor_cli/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class config:
9494
"wallet_name": None,
9595
"wallet_hotkey": None,
9696
"use_cache": True,
97+
"disk_cache": False,
9798
"metagraph_cols": {
9899
"UID": True,
99100
"GLOBAL_STAKE": True,

bittensor_cli/src/bittensor/subtensor_interface.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@
4242
get_hotkey_pub_ss58,
4343
)
4444

45-
SubstrateClass = (
46-
DiskCachedAsyncSubstrateInterface
47-
if os.getenv("DISK_CACHE", "0") == "1"
48-
else AsyncSubstrateInterface
49-
)
50-
5145

5246
class ParamWithTypes(TypedDict):
5347
name: str # Name of the parameter.
@@ -81,7 +75,7 @@ class SubtensorInterface:
8175
Thin layer for interacting with Substrate Interface. Mostly a collection of frequently-used calls.
8276
"""
8377

84-
def __init__(self, network):
78+
def __init__(self, network, use_disk_cache: bool = False):
8579
if network in Constants.network_map:
8680
self.chain_endpoint = Constants.network_map[network]
8781
self.network = network
@@ -111,8 +105,12 @@ def __init__(self, network):
111105
)
112106
self.chain_endpoint = Constants.network_map[defaults.subtensor.network]
113107
self.network = defaults.subtensor.network
114-
115-
self.substrate = SubstrateClass(
108+
substrate_class = (
109+
DiskCachedAsyncSubstrateInterface
110+
if (use_disk_cache or os.getenv("DISK_CACHE", "0") == "1")
111+
else AsyncSubstrateInterface
112+
)
113+
self.substrate = substrate_class(
116114
url=self.chain_endpoint,
117115
ss58_format=SS58_FORMAT,
118116
type_registry=TYPE_REGISTRY,

bittensor_cli/src/commands/subnets/price.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ async def price(
5252

5353
step = 300
5454
start_block = max(0, current_block - total_blocks)
55-
block_numbers = list(range(start_block, current_block + 1, step))
55+
56+
# snap start block down to nearest multiple of 10
57+
start_block -= start_block % 10
58+
59+
block_numbers = []
60+
for b in range(start_block, current_block + 1, step):
61+
if b == current_block:
62+
block_numbers.append(b) # exact current block
63+
else:
64+
block_numbers.append(b - (b % 5)) # snap down to multiple of 10
65+
block_numbers = sorted(set(block_numbers))
5666

5767
# Block hashes
5868
block_hash_cors = [

0 commit comments

Comments
 (0)