---
x-logging-defaults: &logging-defaults
  driver: json-file
  options:
    max-size: "10m"
    max-file: "3"

# Usage:
#
# mkdir rpc && cd rpc
#
# git init
# git remote add origin https://github.com/StakeSquid/ethereum-rpc-docker.git
# git fetch origin vibe
# git checkout origin/vibe
#
# docker run --rm alpine sh -c "printf '0x'; head -c32 /dev/urandom | xxd -p -c 64" > .jwtsecret
#
# env
# ...
# IP=$(curl ipinfo.io/ip)
# DOMAIN=${IP}.traefik.me
# COMPOSE_FILE=base.yml:rpc.yml:cosmos/gaiad/cosmos-mainnet-gaiad-pruned.yml
#
# docker compose up -d
#
# curl -X POST https://${IP}.traefik.me/cosmos-mainnet-pruned \
#   -H "Content-Type: application/json" \
#   --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

services:
  cosmos-mainnet-pruned:
    build:
      context: ./cosmos
      dockerfile: cometbft.Dockerfile
      args:
        CL_IMAGE: ${COSMOS_GAIAD_IMAGE:-ghcr.io/cosmos/gaia}
        CL_VERSION: ${COSMOS_MAINNET_GAIAD_VERSION:-v27.4.0}
    sysctls:
      # TCP Performance
      net.ipv4.tcp_slow_start_after_idle: 0       # Disable slow start after idle
      net.ipv4.tcp_no_metrics_save: 1             # Disable metrics cache
      net.ipv4.tcp_rmem: 4096 87380 16777216      # Increase TCP read buffers
      net.ipv4.tcp_wmem: 4096 87380 16777216      # Increase TCP write buffers
      net.core.somaxconn: 32768                   # Higher connection queue
      # Memory/Connection Management
      # net.core.netdev_max_backlog: 50000          # Increase network buffer
      net.ipv4.tcp_max_syn_backlog: 30000         # More SYN requests
      net.ipv4.tcp_max_tw_buckets: 2000000        # Allow more TIME_WAIT sockets
    ulimits:
      nofile: 1048576    # Max open files (for RPC/WS connections)
    user: root
    ports:
      - 12554:12554
      - 12554:12554/udp
    expose:
      - 26657
      - 26660
      - 9090
      - 1317
    environment:
      - CHAIN_ID=cosmoshub-4
      - GENESIS_URL=https://github.com/cosmos/mainnet/raw/master/genesis/genesis.cosmoshub-4.json.gz
      - IP=${IP}
      - MIN_GAS=0.005uatom
      - MONIKER=d${DOMAIN:-local}
      - P2P_PORT=12554
      - PERSISTENT_PEERS=a7f14a48cd97c0eae7833849ccdf4e9418622c56@65.108.128.201:14956,37dfe1ec33e9f88f378a61a32462d57d2baa5e74@65.108.99.140:26656,f86d297a28da1a2b186583daf0e229c2b1449414@144.76.217.227:26656,66ca3161c5532da890815e40826ddbbbe2cb7f6c@176.9.101.44:26656
      - STATESYNC_RPC=https://cosmos-rpc.polkachu.com:443
      - WASM_SNAPSHOT_URL=https://snapshots.polkachu.com/wasm/cosmos/cosmos_wasmonly.tar.lz4
    restart: unless-stopped
    stop_grace_period: 5m
    networks:
      - chains
    volumes:
      - ${COSMOS_MAINNET_GAIAD_PRUNED_DATA:-cosmos-mainnet-gaiad-pruned}:/root/.gaia
      - /slowdisk:/slowdisk
    logging: *logging-defaults
    labels:
      - prometheus-scrape.enabled=true
      - prometheus-scrape.port=26660
      - prometheus-scrape.path=/metrics
      - traefik.enable=true
      - traefik.http.middlewares.cosmos-mainnet-gaiad-pruned-stripprefix.stripprefix.prefixes=/cosmos-mainnet-pruned
      - traefik.http.services.cosmos-mainnet-gaiad-pruned.loadbalancer.server.port=26657
      - ${NO_SSL:-traefik.http.routers.cosmos-mainnet-gaiad-pruned.entrypoints=websecure}
      - ${NO_SSL:-traefik.http.routers.cosmos-mainnet-gaiad-pruned.tls.certresolver=myresolver}
      - ${NO_SSL:-traefik.http.routers.cosmos-mainnet-gaiad-pruned.rule=Host(`$DOMAIN`) && (Path(`/cosmos-mainnet-pruned`) || Path(`/cosmos-mainnet-pruned/`))}
      - ${NO_SSL:+traefik.http.routers.cosmos-mainnet-gaiad-pruned.rule=Path(`/cosmos-mainnet-pruned`) || Path(`/cosmos-mainnet-pruned/`)}
      - traefik.http.routers.cosmos-mainnet-gaiad-pruned.middlewares=cosmos-mainnet-gaiad-pruned-stripprefix, ipallowlist
      - traefik.http.routers.cosmos-mainnet-gaiad-pruned.priority=50   # gets any request that is not GET with UPGRADE header
      - traefik.http.routers.cosmos-mainnet-gaiad-pruned-ws.priority=100   # answers GET requests first
      - traefik.http.middlewares.cosmos-mainnet-gaiad-pruned-set-ws-path.replacepath.path=/websocket
      - traefik.http.services.cosmos-mainnet-gaiad-pruned-ws.loadbalancer.server.port=26657
      - traefik.http.routers.cosmos-mainnet-gaiad-pruned-ws.service=cosmos-mainnet-gaiad-pruned-ws
      - traefik.http.routers.cosmos-mainnet-gaiad-pruned.service=cosmos-mainnet-gaiad-pruned
      - ${NO_SSL:-traefik.http.routers.cosmos-mainnet-gaiad-pruned-ws.entrypoints=websecure}
      - ${NO_SSL:-traefik.http.routers.cosmos-mainnet-gaiad-pruned-ws.tls.certresolver=myresolver}
      - ${NO_SSL:-traefik.http.routers.cosmos-mainnet-gaiad-pruned-ws.rule=Host(`$DOMAIN`) && (Path(`/cosmos-mainnet-pruned`) || Path(`/cosmos-mainnet-pruned/`)) && HeadersRegexp(`Upgrade`, `(?i)websocket`)}
      - ${NO_SSL:+traefik.http.routers.cosmos-mainnet-gaiad-pruned-ws.rule=(Path(`/cosmos-mainnet-pruned`) || Path(`/cosmos-mainnet-pruned/`)) && HeadersRegexp(`Upgrade`, `(?i)websocket`)}
      - traefik.http.routers.cosmos-mainnet-gaiad-pruned-ws.middlewares=cosmos-mainnet-gaiad-pruned-stripprefix, cosmos-mainnet-gaiad-pruned-set-ws-path, ipallowlist

volumes:
  cosmos-mainnet-gaiad-pruned:

x-upstreams:
  - id: $${ID}
    labels:
      provider: $${PROVIDER}
    connection:
      generic:
        rpc:
          url: $${RPC_URL}
        ws:
          frameSize: 20Mb
          msgSize: 50Mb
          url: $${WS_URL}
    chain: cosmos-hub
    method-groups:
      enabled:
        - debug
        - filter
    methods:
      disabled:
      enabled:
        - name: txpool_content # TODO: should be disabled for rollup nodes
...