# This Dockerfile builds all the dependencies needed by the monorepo, and should
# be used to build any of the follow-on services
#
# ### BASE: Install deps
# We do not use Alpine because there's a regression causing it to be very slow
# when used with typescript/hardhat: https://github.com/nomiclabs/hardhat/issues/1219
FROM node:16-buster-slim as node
RUN apt-get update -y && apt-get install -y git

# Pre-download the compilers so that they do not need to be downloaded inside
# the image when building
FROM alpine as downloader
ARG VERSION=v0.8.9
ARG SOLC_VERSION=${VERSION}+commit.e5eed63a
ARG SOLC_UPSTREAM=https://github.com/ethereum/solc-bin/raw/gh-pages/linux-amd64/solc-linux-amd64-${SOLC_VERSION}

ADD $SOLC_UPSTREAM ./solc
ADD https://github.com/ethereum/solc-bin/raw/gh-pages/linux-amd64/solc-linux-amd64-v0.5.17+commit.d19bba13 ./solc

FROM node as builder
# copy over the needed configs to run the dep installation
# note: this approach can be a bit unhandy to maintain, but it allows
# us to cache the installation steps
WORKDIR /optimism
COPY .git ./.git
COPY *.json yarn.lock ./
COPY packages/sdk/package.json ./packages/sdk/package.json
COPY packages/core-utils/package.json ./packages/core-utils/package.json
COPY packages/common-ts/package.json ./packages/common-ts/package.json
COPY packages/contracts/package.json ./packages/contracts/package.json
COPY packages/data-transport-layer/package.json ./packages/data-transport-layer/package.json
COPY packages/batch-submitter/package.json ./packages/batch-submitter/package.json
COPY packages/message-relayer/package.json ./packages/message-relayer/package.json
COPY packages/replica-healthcheck/package.json ./packages/replica-healthcheck/package.json
COPY packages/regenesis-surgery/package.json ./packages/regenesis-surgery/package.json
COPY integration-tests/package.json ./integration-tests/package.json

RUN yarn install --frozen-lockfile

### BUILDER: Builds the typescript
FROM node:16

WORKDIR /optimism

# cache the node_modules copying step since it's expensive
# we run this before copying over any source files to avoid re-copying anytime the
# code changes
COPY --from=builder /optimism/node_modules ./node_modules
COPY --from=builder /optimism/packages ./packages
COPY --from=builder /optimism/integration-tests ./integration-tests
COPY --from=builder /optimism/.git ./.git

# the following steps are cheap
COPY *.json yarn.lock ./
# copy over the source
COPY ./packages ./packages
COPY ./integration-tests ./integration-tests
# copy over solc to save time building (35+ seconds vs not doing this step)
COPY --from=downloader solc /root/.cache/hardhat-nodejs/compilers/linux-amd64/solc-linux-amd64-${SOLC_VERSION}
COPY --from=downloader solc /root/.cache/hardhat-nodejs/compilers/linux-amd64/solc-linux-amd64-v0.5.17+commit.d19bba13

# build it!
RUN yarn build
# build integration tests' contracts
RUN yarn workspace @eth-optimism/integration-tests build

# TODO: Consider thinning up the container by trimming non-production
# dependencies
# so that it can be used in docker-compose
CMD ["true"]
