Files
aitbc/cli/aitbc_cli/commands/mining.py
aitbc 3897bcbf24
Some checks failed
CLI Tests / test-cli (push) Failing after 4s
Deploy to Testnet / deploy-testnet (push) Successful in 1m40s
Documentation Validation / validate-docs (push) Failing after 12s
Documentation Validation / validate-policies-strict (push) Successful in 4s
Integration Tests / test-service-integration (push) Successful in 2m42s
Package Tests / Python package - aitbc-agent-sdk (push) Failing after 34s
Package Tests / Python package - aitbc-core (push) Successful in 27s
Package Tests / Python package - aitbc-crypto (push) Successful in 13s
Package Tests / Python package - aitbc-sdk (push) Successful in 16s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 8s
Package Tests / JavaScript package - aitbc-token (push) Successful in 18s
Python Tests / test-python (push) Failing after 50s
Security Scanning / security-scan (push) Failing after 43s
Multi-Node Stress Testing / stress-test (push) Successful in 12s
Cross-Node Transaction Testing / transaction-test (push) Successful in 9s
refactor: move version to separate module and improve logging
- Created aitbc/_version.py with centralized version definition
- Updated aitbc/__init__.py to import __version__ from _version module
- Updated constants.py to use __version__ for PACKAGE_VERSION
- Replaced print() calls with logger in decorators.py, events.py, queue_manager.py, and state.py
- Added logger initialization using get_logger(__name__) in config.py, decorators.py, events.py, queue_manager.py, and state.py
- Added cli/commands
2026-05-11 20:12:01 +02:00

107 lines
3.1 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}")