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

services:
  tron-mainnet:
    image: ${TRON_JAVA_TRON_IMAGE:-tronprotocol/java-tron}:${TRON_MAINNET_JAVA_TRON_VERSION:-GreatVoyage-v4.8.1}
    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:
      - 25815:25815
      - 25815:25815/udp
    expose:
      - 8545
    command: -c /config/config.conf --log-config /config/logback.xml
    restart: unless-stopped
    stop_grace_period: 5m
    networks:
      - chains
    volumes:
      - ${TRON_MAINNET_JAVA_TRON_PRUNED_ROCKSDB_DATA:-tron-mainnet-java-tron-pruned-rocksdb}:/java-tron/output-directory
      - ./tron/mainnet:/config
      - /slowdisk:/slowdisk
    logging: *logging-defaults
    labels:
      - prometheus-scrape.enabled=false
      - traefik.enable=true
      - traefik.http.middlewares.tron-mainnet-java-tron-pruned-rocksdb-set-path.replacepath.path=/jsonrpc
      - traefik.http.middlewares.tron-mainnet-java-tron-pruned-rocksdb-stripprefix.stripprefix.prefixes=/tron-mainnet
      - traefik.http.services.tron-mainnet-java-tron-pruned-rocksdb.loadbalancer.server.port=8545
      - ${NO_SSL:-traefik.http.routers.tron-mainnet-java-tron-pruned-rocksdb.entrypoints=websecure}
      - ${NO_SSL:-traefik.http.routers.tron-mainnet-java-tron-pruned-rocksdb.tls.certresolver=myresolver}
      - ${NO_SSL:-traefik.http.routers.tron-mainnet-java-tron-pruned-rocksdb.rule=Host(`$DOMAIN`) && (Path(`/tron-mainnet`) || Path(`/tron-mainnet/`))}
      - ${NO_SSL:+traefik.http.routers.tron-mainnet-java-tron-pruned-rocksdb.rule=Path(`/tron-mainnet`) || Path(`/tron-mainnet/`)}
      - traefik.http.routers.tron-mainnet-java-tron-pruned-rocksdb.middlewares=tron-mainnet-java-tron-pruned-rocksdb-stripprefix, tron-mainnet-java-tron-pruned-rocksdb-set-path, ipallowlist

volumes:
  tron-mainnet-java-tron-pruned-rocksdb:

x-upstreams:
  - id: $${ID}
    labels:
      provider: $${PROVIDER}
    connection:
      generic:
        rpc:
          url: $${RPC_URL}
    chain: tron
    method-groups:
      enabled:
        - debug
        - filter
    methods:
      disabled:
      enabled:
        - name: txpool_content # TODO: should be disabled for rollup nodes
...