|
10 | 10 | from rich.table import Column, Table
|
11 | 11 | from rich import box
|
12 | 12 |
|
13 |
| -from bittensor_cli.src import COLOR_PALETTE |
| 13 | +from bittensor_cli.src import COLOR_PALETTE, Constants |
14 | 14 | from bittensor_cli.src.bittensor.balances import Balance
|
15 | 15 | from bittensor_cli.src.bittensor.extrinsics.registration import (
|
16 | 16 | register_extrinsic,
|
|
34 | 34 | prompt_for_identity,
|
35 | 35 | get_subnet_name,
|
36 | 36 | unlock_key,
|
| 37 | + blocks_to_duration, |
37 | 38 | json_console,
|
38 | 39 | )
|
39 | 40 |
|
@@ -2308,3 +2309,112 @@ async def get_identity(
|
2308 | 2309 | else:
|
2309 | 2310 | console.print(table)
|
2310 | 2311 | return identity
|
| 2312 | + |
| 2313 | + |
| 2314 | +async def get_start_schedule( |
| 2315 | + subtensor: "SubtensorInterface", |
| 2316 | + netuid: int, |
| 2317 | +) -> None: |
| 2318 | + """Fetch and display existing emission schedule information.""" |
| 2319 | + |
| 2320 | + if not await subtensor.subnet_exists(netuid): |
| 2321 | + print_error(f"Subnet {netuid} does not exist.") |
| 2322 | + return None |
| 2323 | + |
| 2324 | + registration_block = await subtensor.query( |
| 2325 | + module="SubtensorModule", |
| 2326 | + storage_function="NetworkRegisteredAt", |
| 2327 | + params=[netuid], |
| 2328 | + ) |
| 2329 | + min_blocks_to_start = Constants.emission_start_schedule |
| 2330 | + current_block = await subtensor.substrate.get_block_number() |
| 2331 | + |
| 2332 | + potential_start_block = registration_block + min_blocks_to_start |
| 2333 | + if current_block < potential_start_block: |
| 2334 | + blocks_to_wait = potential_start_block - current_block |
| 2335 | + time_to_wait = blocks_to_duration(blocks_to_wait) |
| 2336 | + |
| 2337 | + console.print( |
| 2338 | + f"[blue]Subnet {netuid}[/blue]:\n" |
| 2339 | + f"[blue]Registered at:[/blue] {registration_block}\n" |
| 2340 | + f"[blue]Minimum start block:[/blue] {potential_start_block}\n" |
| 2341 | + f"[blue]Current block:[/blue] {current_block}\n" |
| 2342 | + f"[blue]Blocks remaining:[/blue] {blocks_to_wait}\n" |
| 2343 | + f"[blue]Time to wait:[/blue] {time_to_wait}" |
| 2344 | + ) |
| 2345 | + else: |
| 2346 | + console.print( |
| 2347 | + f"[blue]Subnet {netuid}[/blue]:\n" |
| 2348 | + f"[blue]Registered at:[/blue] {registration_block}\n" |
| 2349 | + f"[blue]Current block:[/blue] {current_block}\n" |
| 2350 | + f"[blue]Minimum start block:[/blue] {potential_start_block}\n" |
| 2351 | + f"[dark_sea_green3]Emission schedule can be started[/dark_sea_green3]" |
| 2352 | + ) |
| 2353 | + |
| 2354 | + return |
| 2355 | + |
| 2356 | + |
| 2357 | +async def start_subnet( |
| 2358 | + wallet: "Wallet", |
| 2359 | + subtensor: "SubtensorInterface", |
| 2360 | + netuid: int, |
| 2361 | + prompt: bool = False, |
| 2362 | +) -> bool: |
| 2363 | + """Start a subnet's emission schedule""" |
| 2364 | + |
| 2365 | + if not await subtensor.subnet_exists(netuid): |
| 2366 | + print_error(f"Subnet {netuid} does not exist.") |
| 2367 | + return False |
| 2368 | + |
| 2369 | + subnet_owner = await subtensor.query( |
| 2370 | + module="SubtensorModule", |
| 2371 | + storage_function="SubnetOwner", |
| 2372 | + params=[netuid], |
| 2373 | + ) |
| 2374 | + if subnet_owner != wallet.coldkeypub.ss58_address: |
| 2375 | + print_error(":cross_mark: This wallet doesn't own the specified subnet.") |
| 2376 | + return False |
| 2377 | + |
| 2378 | + if prompt: |
| 2379 | + if not Confirm.ask( |
| 2380 | + f"Are you sure you want to start subnet {netuid}'s emission schedule?" |
| 2381 | + ): |
| 2382 | + return False |
| 2383 | + |
| 2384 | + if not unlock_key(wallet).success: |
| 2385 | + return False |
| 2386 | + |
| 2387 | + with console.status( |
| 2388 | + f":satellite: Starting subnet {netuid}'s emission schedule...", spinner="earth" |
| 2389 | + ): |
| 2390 | + start_call = await subtensor.substrate.compose_call( |
| 2391 | + call_module="SubtensorModule", |
| 2392 | + call_function="start_call", |
| 2393 | + call_params={"netuid": netuid}, |
| 2394 | + ) |
| 2395 | + |
| 2396 | + signed_ext = await subtensor.substrate.create_signed_extrinsic( |
| 2397 | + call=start_call, |
| 2398 | + keypair=wallet.coldkey, |
| 2399 | + ) |
| 2400 | + |
| 2401 | + response = await subtensor.substrate.submit_extrinsic( |
| 2402 | + extrinsic=signed_ext, |
| 2403 | + wait_for_inclusion=True, |
| 2404 | + wait_for_finalization=True, |
| 2405 | + ) |
| 2406 | + |
| 2407 | + if await response.is_success: |
| 2408 | + console.print( |
| 2409 | + f":white_heavy_check_mark: [green]Successfully started subnet {netuid}'s emission schedule.[/green]" |
| 2410 | + ) |
| 2411 | + return True |
| 2412 | + else: |
| 2413 | + error_msg = format_error_message(await response.error_message) |
| 2414 | + if "FirstEmissionBlockNumberAlreadySet" in error_msg: |
| 2415 | + console.print(f"[dark_sea_green3]Subnet {netuid} already has an emission schedule.[/dark_sea_green3]") |
| 2416 | + return True |
| 2417 | + |
| 2418 | + await get_start_schedule(subtensor, netuid) |
| 2419 | + print_error(f":cross_mark: Failed to start subnet: {error_msg}") |
| 2420 | + return False |
0 commit comments