Files
aitbc/cli/aitbc_cli/commands/mining.py
aitbc 27a078a279
Some checks failed
CLI Tests / test-cli (push) Failing after 3s
Cross-Node Transaction Testing / transaction-test (push) Successful in 3s
Deploy to Testnet / deploy-testnet (push) Successful in 1m18s
Multi-Node Stress Testing / stress-test (push) Successful in 2s
Security Scanning / security-scan (push) Successful in 31s
fix: CLI bugs - island list, output signature, GPU credentials, mining subcommand
- S05 Islands: edge.py list_islands now has name='list' for 'aitbc edge island list'
- S06 Market: output() now handles structured data (dict/list) by JSON-encoding
- S09 GPU: added safe_load_credentials() to gpu_marketplace.py and exchange_island.py
  with graceful FileNotFoundError handling and helpful error message
- S13 Mining: added 'list' subcommand to mining.py for listing active miners
2026-05-19 19:38:06 +02:00

125 lines
3.6 KiB
Python

"""
Mining commands for AITBC CLI
"""
import json
from pathlib import Path
from typing import Optional, Dict
import click
from ..utils import error, success
from aitbc import AITBCHTTPClient, NetworkError, KEYSTORE_DIR
DEFAULT_RPC_URL = "http://localhost:8006"
DEFAULT_KEYSTORE_DIR = KEYSTORE_DIR
@click.group()
def mining():
"""Mining operations commands"""
pass
@mining.command()
@click.argument('wallet_name')
@click.option('--threads', type=int, default=1, help='Number of mining threads')
@click.option('--rpc-url', help='Blockchain RPC URL')
def start(wallet_name: str, threads: int, rpc_url: Optional[str]):
"""Start mining with specified wallet"""
if not rpc_url:
rpc_url = DEFAULT_RPC_URL
try:
# Get wallet address
keystore_path = DEFAULT_KEYSTORE_DIR / f"{wallet_name}.json"
if not keystore_path.exists():
error(f"Wallet '{wallet_name}' not found")
return False
with open(keystore_path) as f:
wallet_data = json.load(f)
address = wallet_data['address']
# Start mining via RPC
mining_config = {
"miner_address": address,
"threads": threads,
"enabled": True
}
try:
http_client = AITBCHTTPClient(base_url=rpc_url, timeout=30)
result = http_client.post("/rpc/mining/start", json=mining_config)
success(f"Mining started with wallet '{wallet_name}'")
click.echo(f"Miner address: {address}")
click.echo(f"Threads: {threads}")
click.echo(f"Status: {result.get('status', 'started')}")
return result
except NetworkError as e:
error(f"Error starting mining: {e}")
return None
except Exception as e:
error(f"Error: {e}")
return False
except Exception as e:
error(f"Error: {e}")
return False
@mining.command()
@click.option('--rpc-url', help='Blockchain RPC URL')
def stop(rpc_url: Optional[str]):
"""Stop mining"""
if not rpc_url:
rpc_url = DEFAULT_RPC_URL
try:
http_client = AITBCHTTPClient(base_url=rpc_url, timeout=30)
result = http_client.post("/rpc/mining/stop")
success("Mining stopped")
click.echo(f"Status: {result.get('status', 'stopped')}")
return True
except NetworkError as e:
error(f"Error stopping mining: {e}")
return False
except Exception as e:
error(f"Error: {e}")
return False
@mining.command()
@click.option('--rpc-url', help='Blockchain RPC URL')
def status(rpc_url: Optional[str]):
"""Get mining status"""
if not rpc_url:
rpc_url = DEFAULT_RPC_URL
try:
http_client = AITBCHTTPClient(base_url=rpc_url, timeout=30)
result = http_client.get("/rpc/mining/status")
success("Mining status:")
click.echo(json.dumps(result, indent=2))
except NetworkError as e:
error(f"Error getting mining status: {e}")
except Exception as e:
error(f"Error: {e}")
@mining.command(name='list')
@click.option('--rpc-url', help='Blockchain RPC URL')
def list_miners(rpc_url: Optional[str]):
"""List active miners"""
if not rpc_url:
rpc_url = DEFAULT_RPC_URL
try:
http_client = AITBCHTTPClient(base_url=rpc_url, timeout=30)
result = http_client.get("/rpc/mining/miners")
success("Active miners:")
click.echo(json.dumps(result, indent=2))
except NetworkError as e:
error(f"Error listing miners: {e}")
except Exception as e:
error(f"Error: {e}")