# ============================================================
# XDC GP5 Unified Node Image
# Supports: Mainnet (50) + Apothem (51)
# State Schemes: HBSS (hash, default) + PBSS (path)
# GC Modes: full (default) + archive
# Sync Optimizations: header 120s, body 60s, peer drop 15x
# Binary: XDC (real XDC fork, NOT upstream geth)
# Source: AnilChinchawale/go-ethereum feature/xdpos-consensus
# ============================================================

# ── Stage 1: Build ─────────────────────────────────────────
FROM golang:1.24-alpine AS builder

RUN apk add --no-cache git make gcc musl-dev linux-headers

WORKDIR /build

# Copy source (run from repo root: docker build -f Dockerfile.unified .)
COPY . .

# Build XDC binary
RUN go run build/ci.go install ./cmd/geth \
  && cp build/bin/geth build/bin/XDC \
  && echo "XDC binary built:" && ./build/bin/XDC version 2>&1 | head -3

# ── Stage 2: Runtime ───────────────────────────────────────
FROM alpine:3.19

RUN apk add --no-cache \
  ca-certificates \
  wget \
  curl \
  bash \
  tzdata \
  && rm -rf /var/cache/apk/*

COPY --from=builder /build/build/bin/XDC /usr/local/bin/XDC
RUN chmod +x /usr/local/bin/XDC && ln -s /usr/local/bin/XDC /usr/local/bin/geth

# ── Bake in both genesis files ─────────────────────────────
# Mainnet genesis (chainId: 50)
COPY mainnet-genesis.json /genesis/mainnet.json
# Apothem genesis (chainId: 51)
COPY apothem-genesis.json /genesis/apothem.json

# ── Unified start script ───────────────────────────────────
COPY docker/start-unified.sh /start.sh
RUN chmod +x /start.sh

# ── Defaults ───────────────────────────────────────────────
ENV NETWORK=mainnet \
    STATE_SCHEME=hash \
    GC_MODE=full \
    SYNCMODE=full \
    CACHE=4096 \
    MAXPEERS=50 \
    HTTP_PORT=8545 \
    WS_PORT=8546 \
    P2P_PORT=30303 \
    AUTHRPC_PORT=8561 \
    VERBOSITY=3 \
    EXTERNAL_IP="" \
    BOOTNODES_FILE="" \
    INSTANCE_NAME=""

EXPOSE 8545 8546 30303/tcp 30303/udp

VOLUME ["/work/xdcchain"]

ENTRYPOINT ["/start.sh"]
