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

services:
  polygon-mainnet-erigon3:
    image: ${POLYGON_ERIGON3_IMAGE:-0xpolygon/erigon}:${POLYGON_MAINNET_ERIGON3_VERSION:-v3.3.7}
    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)
      memlock: -1        # Disable memory locking limits (for in-memory DBs like MDBX)
    user: root
    ports:
      - 11320:11320
      - 11320:11320/udp
      - 31320:31320
      - 31320:31320/udp
      - 36320:36320
      - 36320:36320/udp
    expose:
      - 8545
    entrypoint: [erigon]
    command:
      - --bootnodes=enode://07bc4cf87ff8f4e7dc51280991809940f26e846c944609ae4726309be73742a830040cd783989f6941e1b41c02405834bc6365059403a59ca9255ac695156235@34.89.75.187:30303,enode://2c3be2e637a68dc694498a44b6e0d57b5c762925ea97f941079a91f8a080b032fe2eb9e6c3230076e9fb046f626b5dcd3fb045dc9c194689a359aa7167ae0f6c@34.142.43.249:30303,enode://a0bc4dd2b59370d5a375a7ef9ac06cf531571005ae8b2ead2e9aaeb8205168919b169451fb0ef7061e0d80592e6ed0720f559bd1be1c4efb6e6c4381f1bdb986@35.246.99.203:30303,enode://f2b0d50e0b843d38ddcab59614f93065e2c82130100032f86ae193eb874505de12fcaf12502dfd88e339b817c0b374fa4b4f7c4d5a4d1aa04f29c503d95e0228@35.197.233.240:30303,enode://8a3f21c293c913a1148116a295aa69fdf41b9c5b0b0628d49be751aa8c025ae2ec1973d6d84cea8e2aba5541b5d76219dfaae41a124d42d0f56d4e1af50b74f8@35.246.95.65:30303,enode://f5cfe35f47ed928d5403aa28ee616fd64ed7daa527b5ae6a7bc412ca25eaad9b6bf2f776144fd9f8e7e9c80b5360a9c03b67f1d47ea88767def7d391cc7e0cd1@34.105.180.11:30303,enode://fc7624241515f9d5e599a396362c29de92b13a048ad361c90dd72286aa4cca835ba65e140a46ace70cc4dcb18472a476963750b3b69d958c5f546d48675880a8@34.147.169.102:30303,enode://7400e4bc70c56de26d5d240474a1b78af0bf8f0db567edfa851c9724ed697ca7692a92483369e9633d4342a036d10223958007160765d0317a1073f86f2a80c8@34.89.55.74:30303,enode://e4fb013061eba9a2c6fb0a41bbd4149f4808f0fb7e88ec55d7163f19a6f02d64d0ce5ecc81528b769ba552a7068057432d44ab5e9e42842aff5b4709aa2c3f3b@34.89.75.187:30303,enode://a49da6300403cf9b31e30502eb22c142ba4f77c9dda44990bccce9f2121c3152487ee95ee55c6b92d4cdce77845e40f59fd927da70ea91cf935b23e262236d75@34.142.43.249:30303,enode://0e50fdcc2106b0c4e4d9ffbd7798ceda9432e680723dc7b7b4627e384078850c1c4a3e67f17ef2c484201ae6ee7c491cbf5e189b8ffee3948252e9bef59fc54e@35.234.148.172:30303,enode://72c3176693f7100dfedc8a37909120fea16971260a5d95ceff49affbc0e23968c35655fee75734736f0b038147645e8ceeee59af68859b3f5bf91fe249be6259@35.246.95.65:30303,enode://f0e44769385aea31de930d3f4796e3e348962221063bb9f681106d832d13f70e5543d652d30e819812104f1b1ffdd7585977b46bf802ed5a52cf731de8c48dbd@34.105.180.11:30303,enode://198896e373735ba38a0313d073137a413787ece791fbc0d0be0f9f6b9d9dd00ee0841f46519904d666d7f1cdfce5532b093e3a1574b34eb64224f57b9b7fce7b@34.89.55.74:30303
      - --bor.heimdall=https://polygon-heimdall-rest.publicnode.com
      - --chain=bor-mainnet
      - --datadir=/root/.local/share/erigon
      - --discovery.dns=enrtree://AKUEZKN7PSKVNR65FZDHECMKOJQSGPARGTPPBI7WS2VUL4EGR6XPC@pos.polygon-peers.io
      - --http
      - --http.addr=0.0.0.0
      - --http.api=eth,erigon,web3,net,debug,trace,txpool,admin,ots
      - --http.port=8545
      - --http.vhosts=*
      - --maxpeers=50
      - --metrics
      - --metrics.addr=0.0.0.0
      - --metrics.port=6060
      - --nat=extip:${IP}
      - --p2p.allowed-ports=31320
      - --p2p.allowed-ports=36320
      - --persist.receipts=false
      - --port=11320
      - --prune.mode=full
      - --rpc.evmtimeout=${POLYGON_MAINNET_ERIGON3_PRUNED_TRACE_EVMTIMEOUT:-5m0s}
      - --rpc.gascap=6000000000
      - --rpc.overlay.getlogstimeout=${POLYGON_MAINNET_ERIGON3_PRUNED_TRACE_GETLOGSTIMEOUT:-5m0s}
      - --rpc.overlay.replayblocktimeout=${POLYGON_MAINNET_ERIGON3_PRUNED_TRACE_REPLAYBLOCKTIMEOUT:-10s}
      - --rpc.returndata.limit=10000000
      - --torrent.download.rate=${POLYGON_MAINNET_ERIGON3_PRUNED_TRACE_MAX_DOWNLOAD_RATE:-1000mb}
      - --torrent.port=26320
      - --torrent.staticpeers=enode://07bc4cf87ff8f4e7dc51280991809940f26e846c944609ae4726309be73742a830040cd783989f6941e1b41c02405834bc6365059403a59ca9255ac695156235@34.89.75.187:30303,enode://2c3be2e637a68dc694498a44b6e0d57b5c762925ea97f941079a91f8a080b032fe2eb9e6c3230076e9fb046f626b5dcd3fb045dc9c194689a359aa7167ae0f6c@34.142.43.249:30303,enode://a0bc4dd2b59370d5a375a7ef9ac06cf531571005ae8b2ead2e9aaeb8205168919b169451fb0ef7061e0d80592e6ed0720f559bd1be1c4efb6e6c4381f1bdb986@35.246.99.203:30303,enode://f2b0d50e0b843d38ddcab59614f93065e2c82130100032f86ae193eb874505de12fcaf12502dfd88e339b817c0b374fa4b4f7c4d5a4d1aa04f29c503d95e0228@35.197.233.240:30303,enode://8a3f21c293c913a1148116a295aa69fdf41b9c5b0b0628d49be751aa8c025ae2ec1973d6d84cea8e2aba5541b5d76219dfaae41a124d42d0f56d4e1af50b74f8@35.246.95.65:30303,enode://f5cfe35f47ed928d5403aa28ee616fd64ed7daa527b5ae6a7bc412ca25eaad9b6bf2f776144fd9f8e7e9c80b5360a9c03b67f1d47ea88767def7d391cc7e0cd1@34.105.180.11:30303,enode://fc7624241515f9d5e599a396362c29de92b13a048ad361c90dd72286aa4cca835ba65e140a46ace70cc4dcb18472a476963750b3b69d958c5f546d48675880a8@34.147.169.102:30303,enode://7400e4bc70c56de26d5d240474a1b78af0bf8f0db567edfa851c9724ed697ca7692a92483369e9633d4342a036d10223958007160765d0317a1073f86f2a80c8@34.89.55.74:30303,enode://e4fb013061eba9a2c6fb0a41bbd4149f4808f0fb7e88ec55d7163f19a6f02d64d0ce5ecc81528b769ba552a7068057432d44ab5e9e42842aff5b4709aa2c3f3b@34.89.75.187:30303,enode://a49da6300403cf9b31e30502eb22c142ba4f77c9dda44990bccce9f2121c3152487ee95ee55c6b92d4cdce77845e40f59fd927da70ea91cf935b23e262236d75@34.142.43.249:30303,enode://0e50fdcc2106b0c4e4d9ffbd7798ceda9432e680723dc7b7b4627e384078850c1c4a3e67f17ef2c484201ae6ee7c491cbf5e189b8ffee3948252e9bef59fc54e@35.234.148.172:30303,enode://72c3176693f7100dfedc8a37909120fea16971260a5d95ceff49affbc0e23968c35655fee75734736f0b038147645e8ceeee59af68859b3f5bf91fe249be6259@35.246.95.65:30303,enode://f0e44769385aea31de930d3f4796e3e348962221063bb9f681106d832d13f70e5543d652d30e819812104f1b1ffdd7585977b46bf802ed5a52cf731de8c48dbd@34.105.180.11:30303,enode://198896e373735ba38a0313d073137a413787ece791fbc0d0be0f9f6b9d9dd00ee0841f46519904d666d7f1cdfce5532b093e3a1574b34eb64224f57b9b7fce7b@34.89.55.74:30303
      - --ws
      - --ws.port=8545
    restart: unless-stopped
    stop_grace_period: 5m
    networks:
      - chains
    volumes:
      - ${POLYGON_MAINNET_ERIGON3_PRUNED_TRACE_DATA:-polygon-mainnet-erigon3-pruned-trace}:/root/.local/share/erigon
      - /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.polygon-mainnet-erigon3-pruned-trace-stripprefix.stripprefix.prefixes=/polygon-mainnet-erigon3
      - traefik.http.services.polygon-mainnet-erigon3-pruned-trace.loadbalancer.server.port=8545
      - ${NO_SSL:-traefik.http.routers.polygon-mainnet-erigon3-pruned-trace.entrypoints=websecure}
      - ${NO_SSL:-traefik.http.routers.polygon-mainnet-erigon3-pruned-trace.tls.certresolver=myresolver}
      - ${NO_SSL:-traefik.http.routers.polygon-mainnet-erigon3-pruned-trace.rule=Host(`$DOMAIN`) && (Path(`/polygon-mainnet-erigon3`) || Path(`/polygon-mainnet-erigon3/`))}
      - ${NO_SSL:+traefik.http.routers.polygon-mainnet-erigon3-pruned-trace.rule=Path(`/polygon-mainnet-erigon3`) || Path(`/polygon-mainnet-erigon3/`)}
      - traefik.http.routers.polygon-mainnet-erigon3-pruned-trace.middlewares=polygon-mainnet-erigon3-pruned-trace-stripprefix, ipallowlist
    shm_size: 2gb

volumes:
  polygon-mainnet-erigon3-pruned-trace:

x-upstreams:
  - id: $${ID}
    labels:
      provider: $${PROVIDER}
    connection:
      generic:
        rpc:
          url: $${RPC_URL}
        ws:
          frameSize: 20Mb
          msgSize: 50Mb
          url: $${WS_URL}
    chain: polygon
    method-groups:
      enabled:
        - debug
        - filter
        - trace
    methods:
      disabled:
        - name: eth_getLogs # this returns result [] on ethereum mainnet when it shouldn't so it's probably buggy on al networks
      enabled:
        - name: txpool_content # TODO: should be disabled for rollup nodes
        # standard geth and erigon
        - name: debug_accountRange
        - name: debug_getModifiedAccountsByNumber
        - name: debug_getModifiedAccountsByHash
        # non standard geth and erigon
        - name: eth_getRawTransactionByHash
        - name: eth_getRawTransactionByBlockHashAndIndex
        # non standard erigon only
        - name: eth_getBlockReceipts
        - name: eth_protocolVersion
        - name: eth_callMany
        - name: eth_callBundle
        - name: debug_accountAt
        - name: debug_traceCallMany
        - name: erigon_getHeaderByHash
        - name: erigon_getBlockReceiptsByBlockHash
        - name: erigon_getHeaderByNumber
        - name: erigon_getLogsByHash
        - name: erigon_forks
        - name: erigon_getBlockByTimestamp
        - name: erigon_BlockNumber
        - name: erigon_getLatestLogs
        - name: ots_getInternalOperations
        - name: ots_hasCode
        - name: ots_getTransactionError
        - name: ots_traceTransaction
        - name: ots_getBlockDetails
        - name: ots_getBlockDetailsByHash
        - name: ots_getBlockTransactions
        - name: ots_searchTransactionsBefore
        - name: ots_searchTransactionsAfter
        - name: ots_getTransactionBySenderAndNonce
        - name: ots_getContractCreator
...