Skip to content

Commit

Permalink
fix(hardhat): prevent race conditions in contract deployment (#704)
Browse files Browse the repository at this point in the history
* fix(hardhat): Add contract deployment and healthcheck to prevent race conditions

- Added healthcheck to hardhat service in docker-compose.yml
- Modified Dockerfile.hardhat to deploy contracts after node is ready
- Added proper service dependency in docker-compose.yml to ensure jsonrpc waits for hardhat

This fixes the race condition where jsonrpc would try to access hardhat accounts before they were available, causing the finality window to crash.

* fix: use named volume for hardhat artifacts to fix permissions issue

- Changed hardhat artifacts from bind mount to named volume
- This ensures correct permissions are maintained
- Fixes CI/CD pipeline issues with hardhat compilation

* chore(docker): add curl to Dockerfile.hardhat and update healthcheck method

* chore(docker): merge RUN instructions and sort package names in Dockerfile

* fix: change health check command to nc instead of curl

* fix: healthcheck trying curl with increased retries

---------

Co-authored-by: kstroobants <[email protected]>
  • Loading branch information
mpaya5 and kstroobants authored Dec 10, 2024
1 parent e6ec066 commit d31d258
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
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

0 comments on commit d31d258

Please sign in to comment.