Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hardhat): prevent race conditions in contract deployment #704

Merged
merged 7 commits into from
Dec 10, 2024
13 changes: 12 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ services:
- ./backend:/app/backend
- ./hardhat/artifacts:/app/hardhat/artifacts
depends_on:
hardhat:
condition: service_healthy
database-migration:
condition: service_completed_successfully
webrequest:
Expand Down Expand Up @@ -199,7 +201,16 @@ services:
- ./hardhat/scripts:/app/scripts
- ./hardhat/test:/app/test
- ./hardhat/hardhat.config.js:/app/hardhat.config.js
- ./hardhat/artifacts:/app/artifacts
- hardhat_artifacts:/app/artifacts
environment:
- HARDHAT_NETWORK=hardhat
healthcheck:
test: ["CMD", "curl", "-X", "POST", "-H", "Content-Type: application/json", "--fail", "http://localhost:8545", "-d", '{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}']
interval: 10s
timeout: 5s
retries: 10
start_period: 10s

volumes:
hardhat_artifacts:

20 changes: 13 additions & 7 deletions docker/Dockerfile.hardhat
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ FROM node:20.11-alpine3.19
RUN addgroup -S hardhat-group && adduser -S hardhat-user -G hardhat-group
WORKDIR /app

RUN apk add --no-cache g++ make netcat-openbsd python3
# Install necessary packages and set up the environment
RUN apk add --no-cache curl g++ make netcat-openbsd python3 && \
# Copy and install npm packages
mkdir -p /app && \
chown -R hardhat-user:hardhat-group /app

COPY ./hardhat/package*.json ./
RUN npm install --ignore-scripts

COPY ./hardhat .

# Change ownership of the app directory to the non-root user
RUN chown -R hardhat-user:hardhat-group /app
# Set up directories and permissions
RUN mkdir -p /app/artifacts/build-info && \
mkdir -p /app/artifacts/contracts && \
chown -R hardhat-user:hardhat-group /app && \
chmod -R 755 /app/artifacts && \
# Create start script
echo -e '#!/bin/sh\necho "Compiling contracts..."\nnpx hardhat compile --force\necho "Starting Hardhat node..."\nexec ./node_modules/.bin/hardhat node --network hardhat' > /app/start.sh && \
chmod +x /app/start.sh

ENV PATH="/app/node_modules/.bin:${PATH}"

RUN echo -e '#!/bin/sh\necho "Compiling contracts..."\nnpx hardhat compile --force\necho "Starting Hardhat node..."\nexec ./node_modules/.bin/hardhat node --network hardhat' > /app/start.sh && \
chmod +x /app/start.sh && \
ls -la /app/start.sh

EXPOSE 8545

# Switch to non-root user
Expand Down
Loading