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

services:
  morph-mainnet-archive:
    image: ${MORPH_L2GETH_IMAGE:-ghcr.io/morph-l2/go-ethereum}:${MORPH_MAINNET_L2GETH_VERSION:-2.2.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:
      - 11860:11860
      - 11860:11860/udp
    expose:
      - 8545
      - 6060
      - 8551
    command:
      - --datadir=/root/.ethereum
      - --gcmode=archive
      - --maxpeers=50
      - --metrics
      - --metrics.addr=0.0.0.0
      - --metrics.port=6060
      - --morph
      - --morph-mpt
      - --nat=extip:${IP}
      - --port=11860
      - --rpc.gascap=600000000
      - --rpc.txfeecap=0
      - --syncmode=full
      - --http
      - --http.addr=0.0.0.0
      - --http.api=web3,debug,eth,txpool,net,morph,engine,admin
      - --http.port=8545
      - --http.vhosts=*
      - --ws
      - --ws.addr=0.0.0.0
      - --ws.api=web3,debug,eth,txpool,net,morph,engine,admin
      - --ws.origins=*
      - --ws.port=8545
      - --authrpc.addr=0.0.0.0
      - --authrpc.jwtsecret=/jwtsecret
      - --authrpc.vhosts=*
    restart: unless-stopped
    stop_grace_period: 5m
    networks:
      - chains
    volumes:
      - ${MORPH_MAINNET_L2GETH_ARCHIVE_LEVELDB_HASH_MPT_DATA:-morph-mainnet-l2geth-archive-leveldb-hash-mpt}:/root/.ethereum
      - ./morph/mainnet:/config
      - .jwtsecret:/jwtsecret:ro
      - /slowdisk:/slowdisk
    logging: *logging-defaults
    labels:
      - prometheus-scrape.enabled=true
      - prometheus-scrape.port=6060
      - prometheus-scrape.path=/debug/metrics/prometheus
      - traefik.enable=true
      - traefik.http.middlewares.morph-mainnet-l2geth-archive-leveldb-hash-mpt-stripprefix.stripprefix.prefixes=/morph-mainnet-archive
      - traefik.http.services.morph-mainnet-l2geth-archive-leveldb-hash-mpt.loadbalancer.server.port=8545
      - ${NO_SSL:-traefik.http.routers.morph-mainnet-l2geth-archive-leveldb-hash-mpt.entrypoints=websecure}
      - ${NO_SSL:-traefik.http.routers.morph-mainnet-l2geth-archive-leveldb-hash-mpt.tls.certresolver=myresolver}
      - ${NO_SSL:-traefik.http.routers.morph-mainnet-l2geth-archive-leveldb-hash-mpt.rule=Host(`$DOMAIN`) && (Path(`/morph-mainnet-archive`) || Path(`/morph-mainnet-archive/`))}
      - ${NO_SSL:+traefik.http.routers.morph-mainnet-l2geth-archive-leveldb-hash-mpt.rule=Path(`/morph-mainnet-archive`) || Path(`/morph-mainnet-archive/`)}
      - traefik.http.routers.morph-mainnet-l2geth-archive-leveldb-hash-mpt.middlewares=morph-mainnet-l2geth-archive-leveldb-hash-mpt-stripprefix, ipallowlist

  morph-mainnet-archive-node-init:
    image: alpine:3.20
    entrypoint: [/bin/sh, -c]
    command:
      - |
        mkdir -p /db/config /db/data
        cp -n /seed/config.toml  /db/config/config.toml
        cp -n /seed/genesis.json /db/config/genesis.json
        [ -f /db/data/priv_validator_state.json ] || echo '{"height":"0","round":0,"step":0}' > /db/data/priv_validator_state.json
    restart: no
    networks:
      - chains
    volumes:
      - ./morph/mainnet/node-data/config:/seed:ro
      - morph-mainnet-l2geth-archive-leveldb-hash-mpt_morph:/db
    logging: *logging-defaults

  morph-mainnet-archive-node:
    image: ${MORPH_MORPH_IMAGE:-ghcr.io/morph-l2/node}:${MORPH_MAINNET_MORPH_VERSION:-0.5.7}
    ports:
      - 26656:26656
      - 26656:26656/udp
    expose:
      - 26657
      - 26658
    environment:
      - MORPH_NODE_L1_CHAIN_ID=1
      - MORPH_NODE_L1_ETH_RPC=${ETHEREUM_MAINNET_EXECUTION_RPC}
      - MORPH_NODE_L2_ENGINE_AUTH=/jwtsecret
      - MORPH_NODE_L2_ENGINE_RPC=http://morph-mainnet-archive:8551
      - MORPH_NODE_L2_ETH_RPC=http://morph-mainnet-archive:8545
      - MORPH_NODE_ROLLUP_ADDRESS=0x759894ced0e6af42c26668076ffa84d02e3cef60
      - MORPH_NODE_SYNC_START_HEIGHT=24883052
    entrypoint: [morphnode, --home, /db, --log.filename, /db/node.log, --mainnet]
    restart: unless-stopped
    depends_on:
      morph-mainnet-archive-node-init:
        condition: service_completed_successfully
      morph-mainnet-archive:
        condition: service_started
    networks:
      - chains
    volumes:
      - ${MORPH_MAINNET_L2GETH_ARCHIVE_LEVELDB_HASH_MPT__MORPH_DATA:-morph-mainnet-l2geth-archive-leveldb-hash-mpt_morph}:/db
      - ./morph/mainnet:/config
      - .jwtsecret:/jwtsecret:ro
    logging: *logging-defaults
    labels:
      - prometheus-scrape.enabled=true
      - prometheus-scrape.port=26660
      - prometheus-scrape.path=/metrics

volumes:
  morph-mainnet-l2geth-archive-leveldb-hash-mpt:
  morph-mainnet-l2geth-archive-leveldb-hash-mpt_morph:

x-upstreams:
  - id: $${ID}
    labels:
      provider: $${PROVIDER}
    connection:
      generic:
        rpc:
          url: $${RPC_URL}
        ws:
          frameSize: 20Mb
          msgSize: 50Mb
          url: $${WS_URL}
    chain: morph
    method-groups:
      enabled:
        - debug
        - filter
    methods:
      disabled:
      enabled:
        - name: txpool_content # TODO: should be disabled for rollup nodes
        # standard geth only
        - name: debug_getRawBlock
        - name: debug_getRawTransaction
        - name: debug_getRawReceipts
        - name: debug_getRawHeader
        - name: debug_getBadBlocks
        # non standard geth only slightly dangerous
        - name: debug_intermediateRoots
        - name: debug_dumpBlock
        # standard geth and erigon
        - name: debug_accountRange
        - name: debug_getModifiedAccountsByNumber
        - name: debug_getModifiedAccountsByHash
        # non standard geth and erigon
        - name: eth_getRawTransactionByHash
        - name: eth_getRawTransactionByBlockHashAndIndex
...