From ea3597319d4d753ce1b59cc9a76b2cef9877d873 Mon Sep 17 00:00:00 2001 From: aitbc1 Date: Fri, 20 Mar 2026 12:32:33 +0100 Subject: [PATCH] feat: add sync command to CLI main interface - Import sync command from commands.sync module - Register sync command with CLI application --- cli/aitbc_cli/commands/sync.py | 58 ++++++++++++++++++++++++++++++++++ cli/aitbc_cli/main.py | 2 ++ 2 files changed, 60 insertions(+) create mode 100644 cli/aitbc_cli/commands/sync.py diff --git a/cli/aitbc_cli/commands/sync.py b/cli/aitbc_cli/commands/sync.py new file mode 100644 index 00000000..c88e5022 --- /dev/null +++ b/cli/aitbc_cli/commands/sync.py @@ -0,0 +1,58 @@ +"""Sync management commands for AITBC.""" + +import asyncio +import sys +from pathlib import Path + +import click + +from ..utils import success, error, run_subprocess + + +@click.group() +def sync(): + """Blockchain synchronization utilities.""" + pass + + +@sync.command() +@click.option('--source', default='http://10.1.223.40:8006', help='Source RPC URL (leader)') +@click.option('--import-url', default='http://127.0.0.1:8006', help='Local RPC URL for import') +@click.option('--batch-size', type=int, default=100, help='Blocks per batch') +@click.option('--poll-interval', type=float, default=0.2, help='Seconds between batches') +def bulk(source, import_url, batch_size, poll_interval): + """Bulk import blocks from a leader to catch up quickly.""" + try: + # Resolve paths + blockchain_dir = Path(__file__).resolve().parents[3] / 'apps' / 'blockchain-node' + src_dir = blockchain_dir / 'src' + venv_python = blockchain_dir / '.venv' / 'bin' / 'python3' + sync_cli = src_dir / 'aitbc_chain' / 'sync_cli.py' + + if not sync_cli.exists(): + error("sync_cli.py not found. Ensure bulk sync feature is deployed.") + raise click.Abort() + + cmd = [ + str(venv_python), + str(sync_cli), + '--source', source, + '--import-url', import_url, + '--batch-size', str(batch_size), + '--poll-interval', str(poll_interval), + ] + + # Prepare environment + env = { + 'PYTHONPATH': str(src_dir), + } + + success(f"Running bulk sync from {source} to {import_url} (batch size: {batch_size})") + result = run_subprocess(cmd, env=env, capture_output=False) + if result.returncode != 0: + error("Bulk sync failed. Check logs for details.") + raise click.Abort() + success("Bulk sync completed.") + except Exception as e: + error(f"Error during bulk sync: {e}") + raise click.Abort() diff --git a/cli/aitbc_cli/main.py b/cli/aitbc_cli/main.py index c41a8e4f..c1ad6e9b 100755 --- a/cli/aitbc_cli/main.py +++ b/cli/aitbc_cli/main.py @@ -68,6 +68,7 @@ try: except ImportError: enterprise_integration_group = None +from .commands.sync import sync from .commands.explorer import explorer from .plugins import plugin, load_plugins @@ -270,6 +271,7 @@ cli.add_command(advanced_analytics_group) cli.add_command(ai_surveillance_group) if enterprise_integration_group is not None: cli.add_command(enterprise_integration_group) +cli.add_command(sync) cli.add_command(explorer) cli.add_command(plugin) load_plugins(cli)