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

services:
  fuse-mainnet:
    image: ${FUSE_NETHERMIND_IMAGE:-fusenet/node}:${FUSE_MAINNET_NETHERMIND_VERSION:-nethermind-1.28.0-v6.0.3}
    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:
      - 14778:14778
      - 14778:14778/udp
    expose:
      - 8545
      - 9091
    environment:
      - NETHERMIND_CONFIG=fuse
      - NETHERMIND_INITCONFIG_BASEDBPATH=/data/db
      - NETHERMIND_INITCONFIG_LOGDIRECTORY=/data/logs
      - NETHERMIND_INITCONFIG_WEBSOCKETSENABLED=true
      - NETHERMIND_JSONRPCCONFIG_ENABLED=true
      - NETHERMIND_JSONRPCCONFIG_ENABLEDMODULES=[Eth,Subscribe,TxPool,Web3,Personal,Proof,Net,Parity,Health,Rpc,Debug,Admin,Traces]
      - NETHERMIND_JSONRPCCONFIG_HOST=0.0.0.0
      - NETHERMIND_JSONRPCCONFIG_PORT=8545
      - NETHERMIND_JSONRPCCONFIG_WEBSOCKETSPORT=8545
      - NETHERMIND_METRICSCONFIG_EXPOSEPORT=9091
      - NETHERMIND_NETWORKCONFIG_DISCOVERYPORT=14778
      - NETHERMIND_NETWORKCONFIG_EXTERNALIP=${IP}
      - NETHERMIND_NETWORKCONFIG_MAXACTIVEPEERS=250
      - NETHERMIND_NETWORKCONFIG_P2PPORT=14778
      - NETHERMIND_TRACESTORECONFIG_ENABLED=true
      - NETHERMIND_TRACESTORECONFIG_TRACETYPES=Trace,Rewards
    restart: unless-stopped
    stop_grace_period: 5m
    networks:
      - chains
    volumes:
      - ${FUSE_MAINNET_NETHERMIND_PRUNED_ROCKSDB_TRACE_DATA:-fuse-mainnet-nethermind-pruned-rocksdb-trace}:/data
      - /slowdisk:/slowdisk
    logging: *logging-defaults
    labels:
      - prometheus-scrape.enabled=true
      - prometheus-scrape.port=9091
      - prometheus-scrape.path=/metrics
      - traefik.enable=true
      - traefik.http.middlewares.fuse-mainnet-nethermind-pruned-rocksdb-trace-stripprefix.stripprefix.prefixes=/fuse-mainnet
      - traefik.http.services.fuse-mainnet-nethermind-pruned-rocksdb-trace.loadbalancer.server.port=8545
      - ${NO_SSL:-traefik.http.routers.fuse-mainnet-nethermind-pruned-rocksdb-trace.entrypoints=websecure}
      - ${NO_SSL:-traefik.http.routers.fuse-mainnet-nethermind-pruned-rocksdb-trace.tls.certresolver=myresolver}
      - ${NO_SSL:-traefik.http.routers.fuse-mainnet-nethermind-pruned-rocksdb-trace.rule=Host(`$DOMAIN`) && (Path(`/fuse-mainnet`) || Path(`/fuse-mainnet/`))}
      - ${NO_SSL:+traefik.http.routers.fuse-mainnet-nethermind-pruned-rocksdb-trace.rule=Path(`/fuse-mainnet`) || Path(`/fuse-mainnet/`)}
      - traefik.http.routers.fuse-mainnet-nethermind-pruned-rocksdb-trace.middlewares=fuse-mainnet-nethermind-pruned-rocksdb-trace-stripprefix, ipallowlist

volumes:
  fuse-mainnet-nethermind-pruned-rocksdb-trace:

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