---
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:arc/arc/arc-testnet-arc-full.yml
#
# docker compose up -d
#
# curl -X POST https://${IP}.traefik.me/arc-testnet \
#   -H "Content-Type: application/json" \
#   --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

services:
  arc-testnet-snapshots:
    image: ${ARC_ARC_IMAGE:-docker.cloudsmith.io/circle/arc-network/arc-execution}:${ARC_TESTNET_ARC_VERSION:-0.7.2}
    user: 0:0
    entrypoint: [/usr/local/bin/arc-snapshots, download]
    command:
      - --chain=arc-testnet
      - --consensus-path=/data/consensus
      - --execution-path=/data/execution
    restart: no
    networks:
      - chains
    volumes:
      - ${ARC_TESTNET_ARC_FULL_CONSENSUS_DATA:-arc-testnet-arc-full-consensus}:/data/consensus
      - ${ARC_TESTNET_ARC_FULL_EXECUTION_DATA:-arc-testnet-arc-full-execution}:/data/execution
    logging: *logging-defaults

  arc-testnet-consensus-init:
    image: ${ARC_CONSENSUS_IMAGE:-docker.cloudsmith.io/circle/arc-network/arc-consensus}:${ARC_TESTNET_CONSENSUS_VERSION:-0.7.2}
    user: 0:0
    entrypoint: [/usr/local/bin/arc-node-consensus, init]
    command:
      - --home=/data/consensus
    restart: no
    depends_on:
      arc-testnet-snapshots:
        condition: service_completed_successfully
    networks:
      - chains
    volumes:
      - ${ARC_TESTNET_ARC_FULL_CONSENSUS_DATA:-arc-testnet-arc-full-consensus}:/data/consensus
    logging: *logging-defaults

  arc-testnet-sockets-init:
    image: debian:bookworm-slim
    entrypoint: [/bin/sh, -c]
    command: [rm -f /sockets/*.ipc && chown 999:999 /sockets]
    restart: no
    depends_on:
      arc-testnet-consensus-init:
        condition: service_completed_successfully
    networks:
      - chains
    volumes:
      - arc-testnet-arc-full-sockets:/sockets
    logging: *logging-defaults

  arc-testnet:
    image: ${ARC_ARC_IMAGE:-docker.cloudsmith.io/circle/arc-network/arc-execution}:${ARC_TESTNET_ARC_VERSION:-0.7.2}
    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: 0:0
    expose:
      - 8545
      - 9001
    entrypoint: [/usr/local/bin/arc-node-execution, node]
    command:
      - --auth-ipc
      - --auth-ipc.path=/sockets/auth.ipc
      - --chain=arc-testnet
      - --datadir=/data/execution
      - --disable-discovery
      - --enable-arc-rpc
      - --full
      - --http
      - --http.addr=0.0.0.0
      - --http.api=eth,net,web3,txpool,trace,debug
      - --http.port=8545
      - --ipcpath=/sockets/reth.ipc
      - --log.file.directory=/data/execution/logs
      - --metrics=0.0.0.0:9001
      - --rpc.forwarder=https://rpc.quicknode.testnet.arc.network/
    restart: unless-stopped
    stop_grace_period: 5m
    depends_on:
      arc-testnet-sockets-init:
        condition: service_completed_successfully
    networks:
      - chains
    volumes:
      - ${ARC_TESTNET_ARC_FULL_EXECUTION_DATA:-arc-testnet-arc-full-execution}:/data/execution
      - /slowdisk:/slowdisk
      - arc-testnet-arc-full-sockets:/sockets
    logging: *logging-defaults
    labels:
      - prometheus-scrape.enabled=true
      - prometheus-scrape.port=9001
      - prometheus-scrape.path=/metrics
      - traefik.enable=true
      - traefik.http.middlewares.arc-testnet-arc-full-stripprefix.stripprefix.prefixes=/arc-testnet
      - traefik.http.services.arc-testnet-arc-full.loadbalancer.server.port=8545
      - ${NO_SSL:-traefik.http.routers.arc-testnet-arc-full.entrypoints=websecure}
      - ${NO_SSL:-traefik.http.routers.arc-testnet-arc-full.tls.certresolver=myresolver}
      - ${NO_SSL:-traefik.http.routers.arc-testnet-arc-full.rule=Host(`$DOMAIN`) && (Path(`/arc-testnet`) || Path(`/arc-testnet/`))}
      - ${NO_SSL:+traefik.http.routers.arc-testnet-arc-full.rule=Path(`/arc-testnet`) || Path(`/arc-testnet/`)}
      - traefik.http.routers.arc-testnet-arc-full.middlewares=arc-testnet-arc-full-stripprefix, ipallowlist

  arc-testnet-node:
    image: ${ARC_CONSENSUS_IMAGE:-docker.cloudsmith.io/circle/arc-network/arc-consensus}:${ARC_TESTNET_CONSENSUS_VERSION:-0.7.2}
    user: 0:0
    expose:
      - 31000
      - 29000
    entrypoint: [/usr/local/bin/arc-node-consensus, start]
    command:
      - --eth-socket=/sockets/reth.ipc
      - --execution-persistence-backpressure
      - --execution-persistence-backpressure-threshold=50
      - --execution-socket=/sockets/auth.ipc
      - --follow
      - --follow.endpoint=https://rpc.blockdaemon.testnet.arc.network,wss=rpc.blockdaemon.testnet.arc.network/websocket
      - --follow.endpoint=https://rpc.drpc.testnet.arc.network,wss=rpc.drpc.testnet.arc.network
      - --follow.endpoint=https://rpc.quicknode.testnet.arc.network,wss=rpc.quicknode.testnet.arc.network
      - --full
      - --home=/data/consensus
      - --metrics=0.0.0.0:29000
      - --rpc.addr=0.0.0.0:31000
    restart: unless-stopped
    stop_grace_period: 5m
    depends_on:
      arc-testnet-consensus-init:
        condition: service_completed_successfully
      arc-testnet:
        condition: service_started
    networks:
      - chains
    volumes:
      - ${ARC_TESTNET_ARC_FULL_CONSENSUS_DATA:-arc-testnet-arc-full-consensus}:/data/consensus
      - arc-testnet-arc-full-sockets:/sockets
    logging: *logging-defaults
    labels:
      - prometheus-scrape.enabled=true
      - prometheus-scrape.port=29000

volumes:
  arc-testnet-arc-full-consensus:
  arc-testnet-arc-full-execution:
  arc-testnet-arc-full-sockets:

x-upstreams:
  - id: $${ID}
    labels:
      provider: $${PROVIDER}
    connection:
      generic:
        rpc:
          url: $${RPC_URL}
    chain: arc-testnet
    method-groups:
      enabled:
        - debug
        - filter
        - trace
    methods:
      enabled:
        - name: txpool_content
...