# XDC Pruned Node Dockerfile  
# State pruning enabled - target disk usage <300GB
# Recommended for most use cases

FROM golang:1.24-alpine AS builder

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

WORKDIR /build
COPY . /build

RUN make XDC && chmod +x /build/build/bin/XDC

# Runtime stage
FROM alpine:3.19

LABEL maintainer="anil@xinfin.org"
LABEL description="XDC Pruned Node - State pruning enabled (<300GB)"
LABEL version="1.0.0"

RUN apk add --no-cache ca-certificates bash curl jq

WORKDIR /xdc

# Copy binary
COPY --from=builder /build/build/bin/XDC /usr/local/bin/XDC

# Copy genesis files
COPY genesis/ ./genesis/

# Create custom entrypoint for pruned mode
COPY <<'EOF' /xdc/entrypoint-pruned.sh
#!/bin/bash
set -e

DATA_DIR="${DATA_DIR:-/xdc/data}"
NETWORK_ID="${NETWORK_ID:-50}"
GENESIS_FILE="${GENESIS_FILE:-genesis/xdc_mainnet.json}"

# Initialize if no data
if [[ ! -d "$DATA_DIR/XDC" ]]; then
    echo "Initializing pruned node with genesis..."
    XDC init "$GENESIS_FILE" --datadir "$DATA_DIR"
fi

# Pruned mode specific settings
# - gcmode: archive (keeps recent state)
# - state.scheme: hash (default, works with pruning)
# - cache: optimized for pruned operation
# - txlookuplimit: 90 days (~7776000 blocks at 1s/block) 

PRUNED_ARGS="--syncmode ${SYNC_MODE:-full} \
    --gcmode archive \
    --txlookuplimit 7776000 \
    --cache 4096 \
    --cache.database 50 \
    --cache.trie 30 \
    --cache.gc 20 \
    --cache.snapshot 0"

# Build common args
COMMON_ARGS="--datadir $DATA_DIR \
    --networkid $NETWORK_ID \
    --verbosity ${VERBOSITY:-3} \
    --maxpeers ${MAXPEERS:-50} \
    --port 30303 \
    --http \
    --http.addr 0.0.0.0 \
    --http.port 8545 \
    --http.corsdomain '*' \
    --http.vhosts '*' \
    --http.api eth,net,web3,txpool,debug \
    --ws \
    --ws.addr 0.0.0.0 \
    --ws.port 8546 \
    --ws.origins '*'"

# Add bootnodes if specified
if [[ -n "$BOOTNODES" ]]; then
    COMMON_ARGS="$COMMON_ARGS --bootnodes $BOOTNODES"
fi

# Add external IP if specified
if [[ -n "$EXTIP" ]]; then
    COMMON_ARGS="$COMMON_ARGS --nat extip:$EXTIP"
fi

# Add identity if specified
if [[ -n "$IDENTITY" ]]; then
    COMMON_ARGS="$COMMON_ARGS --identity $IDENTITY"
fi

echo "Starting XDC pruned node..."
echo "Data dir: $DATA_DIR"
echo "Network: $NETWORK_ID"

exec XDC $PRUNED_ARGS $COMMON_ARGS "$@"
EOF

RUN chmod +x /usr/local/bin/XDC /xdc/entrypoint-pruned.sh

# Environment defaults for pruned node
ENV DATA_DIR='/xdc/data'
ENV NETWORK_ID='50'
ENV VERBOSITY='3'
ENV MAXPEERS='50'
ENV SYNC_MODE='full'
ENV BOOTNODES=''
ENV EXTIP=''
ENV IDENTITY=''
ENV GENESIS_FILE='genesis/xdc_mainnet.json'
ENV NODE_TYPE='pruned'

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  CMD curl -sf http://localhost:8545 -X POST -H "Content-Type: application/json" \
      -d '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}' || exit 1

EXPOSE 8545 8546 30303 30303/udp

VOLUME ["/xdc/data"]

ENTRYPOINT ["/xdc/entrypoint-pruned.sh"]
