# Info: KCL Polkadot Solochain task schemas for provisioning (Provisioning)
# Author: Provisioning System
# Release: 0.0.1
# Date: 2025-07-24

import regex

schema User:
    """
    User settings for Polkadot solochain
    """
    name: str
    group: str = name
    home?: str = "/home/${name}"

schema NetworkConfig:
    """
    Network configuration for Polkadot solochain
    """
    name: str = "local"
    chain_id: str = "local_testnet"
    node_key?: str
    listen_addr: str = "/ip4/0.0.0.0/tcp/30333"
    public_addr?: str
    bootnodes: [str] = []
    reserved_nodes: [str] = []
    reserved_only: bool = False
    max_peers: int = 50

schema Consensus:
    """
    Consensus configuration
    """
    algorithm: "aura" | "babe" = "aura"
    finality: "grandpa" = "grandpa"
    block_time: int = 6000  # milliseconds
    epoch_duration: int = 600  # blocks for BABE

schema RPC:
    """
    RPC configuration
    """
    enabled: bool = True
    bind_addr: str = "127.0.0.1"
    port: int = 9944
    ws_port: int = 9944
    http_port: int = 9933
    max_connections: int = 100
    cors: [str] = ["all"]
    methods: [str] = ["safe"]

schema Telemetry:
    """
    Telemetry configuration
    """
    enabled: bool = False
    url?: str
    verbosity: int = 0

schema Runtime:
    """
    Runtime configuration including PVM support
    """
    name: str = "solochain-template"
    version: str = "1.0.0"
    pvm_enabled: bool = True
    wasm_execution: "compiled" | "interpreted" = "compiled"
    heap_pages: int = 64
    max_block_weight: int = 2000000000000  # Weight units
    max_block_length: int = 5242880  # 5MB
    pallets: [str] = [
        "system",
        "timestamp", 
        "aura",
        "grandpa",
        "balances",
        "transaction_payment",
        "sudo"
    ]

schema Validator:
    """
    Validator configuration
    """
    enabled: bool = False
    key_type: "sr25519" | "ed25519" = "sr25519"
    session_keys?: str
    validator_id?: str

schema PolkadotSolochain:
    """
    Polkadot solochain node configuration
    """
    name: str = "polkadot-solochain"
    version: str = "stable2024"
    run_user: User = {
        name = "polkadot"
    }
    work_path: str = "/var/lib/polkadot"
    config_path: str = "/etc/polkadot"
    bin_path: str = "/usr/local/bin"
    node_binary: str = "solochain-template-node"
    base_path: str = "/var/lib/polkadot/data"
    keystore_path: str = "/var/lib/polkadot/keystore"
    network: NetworkConfig = {}
    consensus: Consensus = {}
    rpc: RPC = {}
    telemetry: Telemetry = {}
    runtime: Runtime = {}
    validator: Validator = {}
    execution_strategy: "native" | "wasm" | "both" = "wasm"
    wasm_runtime_overrides?: str
    pruning: "archive" | int = 256
    state_cache_size: int = 67108864  # 64MB
    log_level: "error" | "warn" | "info" | "debug" | "trace" = "info"
    log_targets: [str] = []
    dev_mode: bool = False
    alice_validator: bool = False if not dev_mode else True
    
    check:
        1 <= rpc.port <= 65535, "RPC port must be between 1 and 65535"
        1 <= rpc.ws_port <= 65535, "WebSocket port must be between 1 and 65535"
        1 <= rpc.http_port <= 65535, "HTTP port must be between 1 and 65535"
        len(run_user.name) > 0, "Check run_user name"
        len(work_path) > 0, "Check work_path"
        len(config_path) > 0, "Check config_path"
        len(base_path) > 0, "Check base_path"
        network.max_peers > 0, "max_peers must be positive"
        consensus.block_time > 0, "block_time must be positive"
        runtime.heap_pages > 0, "heap_pages must be positive"
        state_cache_size > 0, "state_cache_size must be positive"