#syntax=docker/dockerfile:1.2@sha256:e2a8561e419ab1ba6b2fe6cbdf49fd92b95912df1cf7d313c3e2230a333fdbcc
FROM pythfoundation/pyth-client:devnet-v2.12.0 as pyth-oracle-copy

FROM docker.io/library/rust:1.49@sha256:a50165ea96983c21832578afb1c8c028674c965bc1ed43b607871b1f362e06a5 as build

RUN apt-get update && \
    apt-get install -y \
    clang \
    libssl-dev \
    libudev-dev \
    llvm \
    pkg-config \
    zlib1g-dev \
    && \
    rm -rf /var/lib/apt/lists/* && \
    rustup component add rustfmt

RUN sh -c "$(curl -sSfL https://release.solana.com/v1.10.31/install)"

ENV PATH="/root/.local/share/solana/install/active_release/bin:$PATH"


ADD solana/pyth2wormhole/rust-toolchain /rust-toolchain
# Solana does a questionable download at the beginning of a *first* build-bpf call. Trigger and layer-cache it explicitly.
RUN cargo init --lib /tmp/decoy-crate && \
    cd /tmp/decoy-crate && cargo build-bpf && \
    rm -rf /tmp/decoy-crate

WORKDIR /usr/src/bridge

ARG WORMHOLE_REV=2.8.9
ADD https://github.com/wormhole-foundation/wormhole/archive/refs/tags/v${WORMHOLE_REV}.tar.gz .
RUN tar -xvf v${WORMHOLE_REV}.tar.gz
RUN mv wormhole-${WORMHOLE_REV} wormhole
# RUN mkdir -p /usr/src/bridge/wormhole/solana/target

WORKDIR /usr/src/squads

ARG SQUADS_REV=1.2.0
ADD https://github.com/Squads-Protocol/squads-mpl/archive/refs/tags/v${SQUADS_REV}.tar.gz .

RUN tar -xvf v${SQUADS_REV}.tar.gz
RUN mv squads-mpl-${SQUADS_REV} squads-mpl

WORKDIR /usr/src/bridge

ADD solana solana
ADD pythnet pythnet
ADD third_party/pyth/p2w-sdk/rust third_party/pyth/p2w-sdk/rust
RUN mkdir -p /opt/solana/deps

ENV EMITTER_ADDRESS="11111111111111111111111111111115"
ENV BRIDGE_ADDRESS="Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o"

WORKDIR /usr/src/bridge/solana

# Build Wormhole Solana programs
RUN --mount=type=cache,target=/usr/src/bridge/wormhole/solana/target \
    --mount=type=cache,target=/usr/src/bridge/solana/pyth2wormhole/target \
    --mount=type=cache,target=/usr/src/squads/squads-mpl/target \
    --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/root/.cache \
    cargo build-bpf --manifest-path "../wormhole/solana/bridge/program/Cargo.toml" -- --locked && \
    cargo build-bpf --manifest-path "../wormhole/solana/bridge/cpi_poster/Cargo.toml" -- --locked && \
    cargo build-bpf --manifest-path "pyth2wormhole/program/Cargo.toml" -- --locked && \
    cargo build-bpf --manifest-path "/usr/src/squads/squads-mpl/programs/mesh/Cargo.toml" -- --locked && \
    cp ../wormhole/solana/target/deploy/bridge.so /opt/solana/deps/bridge.so && \
    cp ../wormhole/solana/target/deploy/cpi_poster.so /opt/solana/deps/cpi_poster.so && \
    cp pyth2wormhole/target/deploy/pyth2wormhole.so /opt/solana/deps/pyth2wormhole.so && \
    cp /usr/src/squads/squads-mpl/target/deploy/mesh.so /opt/solana/deps/mesh.so

COPY --from=pyth-oracle-copy /home/pyth/pyth-client/target/oracle.so /opt/solana/deps/pyth_oracle.so

ENV RUST_LOG="solana_runtime::system_instruction_processor=trace,solana_runtime::message_processor=trace,solana_bpf_loader=debug,solana_rbpf=debug"
ENV RUST_BACKTRACE=1
