# ============================================================
# Genex Blockchain Stack — Docker Compose
#
# 包含:
#   - genex-node-1/2/3: 3个创世验证节点
#   - genex-inst-1/2: 2个机构验证节点
#   - genex-regulatory: 1个监管只读节点
#   - contract-deployer: 智能合约部署任务
#   - enterprise-api: 企业API服务 (:3020)
#   - gas-relayer: Gas代付中继 (:3022)
#   - faucet: 测试网水龙头 (:3023)
#   - bridge-monitor: 跨链桥监控 (:3024)
#
# 启动:
#   docker compose up -d
#
# 仅启动单节点开发模式:
#   docker compose up genex-node-1 -d
#
# 启动生态服务:
#   docker compose --profile ecosystem up -d
#
# 部署合约:
#   docker compose run contract-deployer
#
# Blockscout 浏览器:
#   docker compose -f docker-compose.explorer.yml up -d
#
# 链监控:
#   docker compose -f chain-monitor/docker-compose.monitor.yml up -d
# ============================================================

version: "3.9"

x-genex-node: &genex-node-defaults
  build:
    context: ./genex-chain
    dockerfile: Dockerfile
  command: ["start", "--chain-id", "genex-testnet-1"]
  restart: unless-stopped
  networks:
    - genex-net

services:
  # =============================================
  # Genesis Validator Node 1 (Primary, US-East)
  # =============================================
  genex-node-1:
    <<: *genex-node-defaults
    container_name: genex-us-east-1
    hostname: genex-us-east-1
    environment:
      - MONIKER=genex-us-east-1
      - CHAIN_ID=genex-testnet-1
      - NODE_TYPE=genesis
    ports:
      - "26656:26656"  # P2P
      - "26657:26657"  # CometBFT RPC
      - "8545:8545"    # EVM JSON-RPC
      - "8546:8546"    # EVM WebSocket
      - "1317:1317"    # Cosmos REST API
      - "9090:9090"    # gRPC
    volumes:
      - node1-data:/home/genex/.genexd

  # =============================================
  # Genesis Validator Node 2 (US-West)
  # =============================================
  genex-node-2:
    <<: *genex-node-defaults
    container_name: genex-us-west-1
    hostname: genex-us-west-1
    environment:
      - MONIKER=genex-us-west-1
      - CHAIN_ID=genex-testnet-1
      - NODE_TYPE=genesis
      - PERSISTENT_PEERS=genex-us-east-1:26656
    ports:
      - "26666:26656"
      - "26667:26657"
      - "8555:8545"
    volumes:
      - node2-data:/home/genex/.genexd
    depends_on:
      - genex-node-1

  # =============================================
  # Genesis Validator Node 3 (Singapore)
  # =============================================
  genex-node-3:
    <<: *genex-node-defaults
    container_name: genex-sg-1
    hostname: genex-sg-1
    environment:
      - MONIKER=genex-sg-1
      - CHAIN_ID=genex-testnet-1
      - NODE_TYPE=genesis
      - PERSISTENT_PEERS=genex-us-east-1:26656,genex-us-west-1:26656
    ports:
      - "26676:26656"
      - "26677:26657"
      - "8565:8545"
    volumes:
      - node3-data:/home/genex/.genexd
    depends_on:
      - genex-node-1

  # =============================================
  # Institution Validator Node 1
  # =============================================
  genex-inst-1:
    <<: *genex-node-defaults
    container_name: genex-inst-1
    hostname: genex-inst-1
    environment:
      - MONIKER=genex-inst-1
      - CHAIN_ID=genex-testnet-1
      - NODE_TYPE=institution
      - PERSISTENT_PEERS=genex-us-east-1:26656,genex-sg-1:26656
    ports:
      - "26686:26656"
      - "26687:26657"
      - "8575:8545"
    volumes:
      - inst1-data:/home/genex/.genexd
    depends_on:
      - genex-node-1
      - genex-node-3

  # =============================================
  # Institution Validator Node 2
  # =============================================
  genex-inst-2:
    <<: *genex-node-defaults
    container_name: genex-inst-2
    hostname: genex-inst-2
    environment:
      - MONIKER=genex-inst-2
      - CHAIN_ID=genex-testnet-1
      - NODE_TYPE=institution
      - PERSISTENT_PEERS=genex-us-east-1:26656,genex-inst-1:26656
    ports:
      - "26696:26656"
      - "26697:26657"
      - "8585:8545"
    volumes:
      - inst2-data:/home/genex/.genexd
    depends_on:
      - genex-node-1

  # =============================================
  # Regulatory Observer Node (Read-Only)
  # =============================================
  genex-regulatory:
    <<: *genex-node-defaults
    container_name: genex-regulatory-1
    hostname: genex-regulatory-1
    environment:
      - MONIKER=genex-regulatory-1
      - CHAIN_ID=genex-testnet-1
      - NODE_TYPE=regulatory
      - PERSISTENT_PEERS=genex-us-east-1:26656,genex-sg-1:26656
    ports:
      - "26706:26656"
      - "26707:26657"
      - "8595:8545"
    volumes:
      - regulatory-data:/home/genex/.genexd
    depends_on:
      - genex-node-1

  # =============================================
  # Smart Contract Deployer (one-shot job)
  # =============================================
  contract-deployer:
    build:
      context: ./genex-contracts
      dockerfile: Dockerfile
    container_name: genex-contract-deployer
    environment:
      - RPC_URL=http://genex-node-1:8545
      - DEPLOYER_PRIVATE_KEY=${DEPLOYER_PRIVATE_KEY:-}
      - USDC_ADDRESS=${USDC_ADDRESS:-}
      - FEE_COLLECTOR=${FEE_COLLECTOR:-}
    networks:
      - genex-net
    depends_on:
      - genex-node-1
    profiles:
      - deploy

  # =============================================
  # 生态基础设施服务 (profile: ecosystem)
  # =============================================

  # Enterprise API — 四层认证企业级链上接口
  enterprise-api:
    build:
      context: ./enterprise-api
      dockerfile: Dockerfile
    container_name: genex-enterprise-api
    restart: unless-stopped
    environment:
      - PORT=3020
      - GENEX_RPC_URL=http://genex-node-1:8545
      - GENEX_WS_URL=ws://genex-node-1:8546
      - CHAIN_ID=8888
      - REDIS_URL=redis://redis:6379
      - DATABASE_URL=postgresql://genex:genex@postgres:5432/genex_enterprise
    ports:
      - "3020:3020"
    networks:
      - genex-net
    depends_on:
      - genex-node-1
    profiles:
      - ecosystem

  # Gas Relayer — Meta-TX 代付中继
  gas-relayer:
    build:
      context: ./gas-relayer
      dockerfile: Dockerfile
    container_name: genex-gas-relayer
    restart: unless-stopped
    environment:
      - PORT=3022
      - GENEX_RPC_URL=http://genex-node-1:8545
      - CHAIN_ID=8888
      - REDIS_URL=redis://redis:6379
      - RATE_LIMIT_PER_USER=50
    ports:
      - "3022:3022"
    networks:
      - genex-net
    depends_on:
      - genex-node-1
    profiles:
      - ecosystem

  # Faucet — 测试网水龙头
  faucet:
    build:
      context: ./faucet-service
      dockerfile: Dockerfile
    container_name: genex-faucet
    restart: unless-stopped
    environment:
      - PORT=3023
      - GENEX_RPC_URL=http://genex-node-1:8545
      - CHAIN_ID=8888
      - REDIS_URL=redis://redis:6379
      - DRIP_AMOUNT_GNX=100
      - DRIP_AMOUNT_USDC=10000
      - COOLDOWN_HOURS=24
    ports:
      - "3023:3023"
    networks:
      - genex-net
    depends_on:
      - genex-node-1
    profiles:
      - ecosystem

  # Bridge Monitor — 跨链桥对账监控
  bridge-monitor:
    build:
      context: ./bridge-monitor
      dockerfile: Dockerfile
    container_name: genex-bridge-monitor
    restart: unless-stopped
    environment:
      - PORT=3024
      - GENEX_RPC_URL=http://genex-node-1:8545
      - ETH_RPC_URL=${ETH_RPC_URL:-}
      - RECONCILE_INTERVAL=5m
      - DISCREPANCY_THRESHOLD=0.0001
      - ALERT_WEBHOOK=${BRIDGE_ALERT_WEBHOOK:-}
    ports:
      - "3024:3024"
    networks:
      - genex-net
    depends_on:
      - genex-node-1
    profiles:
      - ecosystem

  # Archive Node — 归档节点（全历史状态）
  genex-archive:
    <<: *genex-node-defaults
    container_name: genex-archive-1
    hostname: genex-archive-1
    environment:
      - MONIKER=genex-archive-1
      - CHAIN_ID=genex-testnet-1
      - NODE_TYPE=archive
      - PERSISTENT_PEERS=genex-us-east-1:26656,genex-sg-1:26656
      - PRUNING=nothing
    ports:
      - "26716:26656"
      - "26717:26657"
      - "8600:8545"
      - "8601:8546"
    volumes:
      - archive-data:/home/genex/.genexd
    depends_on:
      - genex-node-1
    profiles:
      - ecosystem

volumes:
  node1-data:
  node2-data:
  node3-data:
  inst1-data:
  inst2-data:
  regulatory-data:
  archive-data:

networks:
  genex-net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.28.0.0/16
