From 661d155b36ee76333cad53cc9d23051837ed67e7 Mon Sep 17 00:00:00 2001 From: Miguel Paya Date: Mon, 9 Dec 2024 12:22:53 +0100 Subject: [PATCH 1/6] 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. --- docker-compose.yml | 8 ++++++++ docker/Dockerfile.hardhat | 29 ++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a9fcda34..ce2f24a8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: @@ -202,4 +204,10 @@ services: - ./hardhat/artifacts:/app/artifacts environment: - HARDHAT_NETWORK=hardhat + healthcheck: + test: ["CMD", "/app/healthcheck.sh"] + interval: 5s + timeout: 3s + retries: 5 + start_period: 10s diff --git a/docker/Dockerfile.hardhat b/docker/Dockerfile.hardhat index ff1b66f6..731dbeb7 100644 --- a/docker/Dockerfile.hardhat +++ b/docker/Dockerfile.hardhat @@ -16,12 +16,35 @@ RUN chown -R hardhat-user:hardhat-group /app 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 +# Add healthcheck directly in Dockerfile +RUN echo -e '#!/bin/sh\nnc -z localhost 8545 || exit 1' > /app/healthcheck.sh && \ + chmod +x /app/healthcheck.sh + +# Modify start script to ensure node is ready +RUN echo -e '#!/bin/sh\n\ +echo "Compiling contracts..."\n\ +npx hardhat compile --force\n\ +echo "Starting Hardhat node..."\n\ +npx hardhat node --network hardhat &\n\ +\n\ +# Wait for node to be ready\n\ +while ! nc -z localhost 8545; do\n\ + echo "Waiting for Hardhat node..."\n\ + sleep 1\n\ +done\n\ +\n\ +echo "Hardhat node is ready"\n\ +\n\ +# Keep container running\n\ +tail -f /dev/null\n\ +' > /app/start.sh && \ +chmod +x /app/start.sh EXPOSE 8545 +HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=5 \ + CMD /app/healthcheck.sh + # Switch to non-root user USER hardhat-user From bed327fac507e3927da30c6d89fd16a94f43de87 Mon Sep 17 00:00:00 2001 From: Miguel Paya Date: Mon, 9 Dec 2024 12:52:13 +0100 Subject: [PATCH 2/6] 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 --- docker-compose.yml | 5 ++++- docker/Dockerfile.hardhat | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index ce2f24a8..04e71ff6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -201,7 +201,7 @@ 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: @@ -211,3 +211,6 @@ services: retries: 5 start_period: 10s +volumes: + hardhat_artifacts: + diff --git a/docker/Dockerfile.hardhat b/docker/Dockerfile.hardhat index 731dbeb7..8c0207bf 100644 --- a/docker/Dockerfile.hardhat +++ b/docker/Dockerfile.hardhat @@ -11,8 +11,12 @@ 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 +# Create artifacts directory and set permissions before volume mount +RUN mkdir -p /app/artifacts && \ + mkdir -p /app/artifacts/build-info && \ + mkdir -p /app/artifacts/contracts && \ + chown -R hardhat-user:hardhat-group /app && \ + chmod -R 755 /app/artifacts ENV PATH="/app/node_modules/.bin:${PATH}" From 66eda97b3e1913b5fc249595e1b42d634621a5c2 Mon Sep 17 00:00:00 2001 From: Miguel Paya Date: Mon, 9 Dec 2024 15:55:25 +0100 Subject: [PATCH 3/6] chore(docker): add curl to Dockerfile.hardhat and update healthcheck method --- docker-compose.yml | 8 ++++---- docker/Dockerfile.hardhat | 35 ++++++++--------------------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 04e71ff6..89e9659b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -205,10 +205,10 @@ services: environment: - HARDHAT_NETWORK=hardhat healthcheck: - test: ["CMD", "/app/healthcheck.sh"] - interval: 5s - timeout: 3s - retries: 5 + 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: 3 start_period: 10s volumes: diff --git a/docker/Dockerfile.hardhat b/docker/Dockerfile.hardhat index 8c0207bf..48dcff27 100644 --- a/docker/Dockerfile.hardhat +++ b/docker/Dockerfile.hardhat @@ -4,13 +4,17 @@ 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 including curl +RUN apk add --no-cache g++ make netcat-openbsd python3 curl 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 + # Create artifacts directory and set permissions before volume mount RUN mkdir -p /app/artifacts && \ mkdir -p /app/artifacts/build-info && \ @@ -20,35 +24,12 @@ RUN mkdir -p /app/artifacts && \ ENV PATH="/app/node_modules/.bin:${PATH}" -# Add healthcheck directly in Dockerfile -RUN echo -e '#!/bin/sh\nnc -z localhost 8545 || exit 1' > /app/healthcheck.sh && \ - chmod +x /app/healthcheck.sh - -# Modify start script to ensure node is ready -RUN echo -e '#!/bin/sh\n\ -echo "Compiling contracts..."\n\ -npx hardhat compile --force\n\ -echo "Starting Hardhat node..."\n\ -npx hardhat node --network hardhat &\n\ -\n\ -# Wait for node to be ready\n\ -while ! nc -z localhost 8545; do\n\ - echo "Waiting for Hardhat node..."\n\ - sleep 1\n\ -done\n\ -\n\ -echo "Hardhat node is ready"\n\ -\n\ -# Keep container running\n\ -tail -f /dev/null\n\ -' > /app/start.sh && \ -chmod +x /app/start.sh +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 -HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=5 \ - CMD /app/healthcheck.sh - # Switch to non-root user USER hardhat-user From 0895895fbae7e72765af6a9ab57f699b26007c1b Mon Sep 17 00:00:00 2001 From: Miguel Paya Date: Mon, 9 Dec 2024 16:04:33 +0100 Subject: [PATCH 4/6] chore(docker): merge RUN instructions and sort package names in Dockerfile --- docker/Dockerfile.hardhat | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/docker/Dockerfile.hardhat b/docker/Dockerfile.hardhat index 48dcff27..003a427a 100644 --- a/docker/Dockerfile.hardhat +++ b/docker/Dockerfile.hardhat @@ -4,30 +4,28 @@ FROM node:20.11-alpine3.19 RUN addgroup -S hardhat-group && adduser -S hardhat-user -G hardhat-group WORKDIR /app -# Install necessary packages including curl -RUN apk add --no-cache g++ make netcat-openbsd python3 curl +# 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 - -# Create artifacts directory and set permissions before volume mount -RUN mkdir -p /app/artifacts && \ - mkdir -p /app/artifacts/build-info && \ +# 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 + 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 From ed7a4cb5cd04008518a05b3248b567098ed3c574 Mon Sep 17 00:00:00 2001 From: kstroobants Date: Tue, 10 Dec 2024 11:47:39 +0000 Subject: [PATCH 5/6] fix: change health check command to nc instead of curl --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 89e9659b..752f81fa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -205,10 +205,10 @@ services: 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}'] + test: ["CMD", "nc", "-z", "localhost", "8545"] interval: 10s timeout: 5s - retries: 3 + retries: 10 start_period: 10s volumes: From 407647a89174d1dc156056dd5bdfdf75b8bab4fa Mon Sep 17 00:00:00 2001 From: kstroobants Date: Tue, 10 Dec 2024 11:57:42 +0000 Subject: [PATCH 6/6] fix: healthcheck trying curl with increased retries --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 752f81fa..e32845af 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -205,7 +205,7 @@ services: environment: - HARDHAT_NETWORK=hardhat healthcheck: - test: ["CMD", "nc", "-z", "localhost", "8545"] + 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