# Synor ZK-Rollup Sequencer Dockerfile
# Multi-stage build for optimized production image

# Stage 1: Build
# Using latest stable Rust (edition2024 requires 1.85+)
FROM rust:latest AS builder

WORKDIR /build

# Install dependencies for arkworks cryptography
RUN apt-get update && apt-get install -y \
    cmake \
    libclang-dev \
    libssl-dev \
    pkg-config \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy workspace files
COPY Cargo.toml Cargo.lock ./
COPY crates/ ./crates/
COPY apps/ ./apps/
COPY contracts/ ./contracts/

# Build the ZK rollup binary with node features (async runtime, logging)
RUN cargo build --release -p synor-zk --features node --bin zk-sequencer

# Stage 2: Runtime
FROM debian:bookworm-slim

RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user
RUN useradd -m -u 1000 synor

# Create data directories
RUN mkdir -p /data/zk /config /proofs && chown -R synor:synor /data /config /proofs

WORKDIR /app

# Copy the built binary
COPY --from=builder /build/target/release/zk-sequencer /app/zk-sequencer

# Copy configuration template
COPY docker/zk-rollup/config.toml /config/config.toml

# Make binary executable
RUN chmod +x /app/zk-sequencer && chown synor:synor /app/zk-sequencer

USER synor

# ZK Rollup ports
# 3001: Sequencer API
# 3002: Prover RPC
# 9001: Metrics
EXPOSE 3001 3002 9001

# Environment defaults
ENV RUST_LOG=info
ENV DATA_DIR=/data/zk
ENV PROOF_DIR=/proofs
ENV L1_RPC=http://synor-node-1:8545
ENV MAX_BATCH_SIZE=1000
ENV PROOF_BACKEND=groth16

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD curl -f http://localhost:3001/health || exit 1

# Data volumes
VOLUME ["/data/zk", "/proofs"]

# Entry point
ENTRYPOINT ["/app/zk-sequencer"]
CMD ["--config", "/config/config.toml"]
