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

Conversation

mpaya5
Copy link
Contributor

@mpaya5 mpaya5 commented Dec 9, 2024

What

  • Added healthcheck to hardhat service in docker-compose.yml to verify node availability
  • Modified Dockerfile.hardhat to ensure contracts are deployed after node is ready
  • Added proper service dependency in docker-compose.yml to ensure jsonrpc waits for hardhat
  • Added healthcheck script directly in Dockerfile to maintain Windows compatibility

Why

A race condition was causing the jsonrpc service to attempt accessing hardhat accounts before they were available. This would cause:

  • Failed transaction insertions due to unavailable accounts
  • Finality window crashes with inability to recover
  • Inconsistent contract deployment state

Fixes #702

Testing done

  • Tested multiple docker compose up cycles to verify consistent initialization
  • Verified hardhat node is fully ready before jsonrpc attempts connections
  • Confirmed finality window operates correctly after changes
  • Tested contract deployment order and account availability
  • Verified Windows compatibility by embedding healthcheck in Dockerfile

Decisions made

  • Embedded healthcheck script directly in Dockerfile instead of separate file for Windows compatibility
  • Used netcat for healthcheck as it's lightweight and already available in the base image
  • Added explicit contract deployment step in start script to ensure proper initialization order

Checks

  • I have tested this code
  • I have reviewed my own PR
  • I have created an issue for this PR
  • I have set a descriptive PR title compliant with conventional commits

Reviewing tips

  • Key files to review are Dockerfile.hardhat and docker-compose.yml
  • The healthcheck implementation is critical for proper service ordering
  • Pay special attention to the start.sh script modifications in Dockerfile.hardhat

User facing release notes

Fixed an initialization issue that could cause the system to fail when starting up. The service now properly ensures all blockchain components are ready before beginning operations, providing a more stable environment for transactions and consensus operations.

… 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.
@mpaya5 mpaya5 linked an issue Dec 9, 2024 that may be closed by this pull request
@mpaya5 mpaya5 self-assigned this Dec 9, 2024
@mpaya5 mpaya5 requested a review from cristiam86 December 9, 2024 11:32
- Changed hardhat artifacts from bind mount to named volume
- This ensures correct permissions are maintained
- Fixes CI/CD pipeline issues with hardhat compilation
@mpaya5 mpaya5 added documentation Improvements or additions to documentation run-tests labels Dec 9, 2024
@mpaya5
Copy link
Contributor Author

mpaya5 commented Dec 9, 2024

I’m not sure why I’m encountering these errors. I’ve been running docker compose build and docker compose up -d with this branch without any issues so far.

My Docker Versions:

Docker version 27.3.1, build ce12230
Docker Compose version v2.24.6

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@mpaya5 mpaya5 merged commit d31d258 into main Dec 10, 2024
18 of 19 checks passed
cristiam86 added a commit that referenced this pull request Dec 10, 2024
* chore(deps): update dependency @vue/eslint-config-prettier to v10

* fix: adding Hardhat Dockerfile in GitHub Workflow (#693)

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

* 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]>

* fix: catch exception to keep loop running (#705)

Co-authored-by: Cristiam Da Silva <[email protected]>

* fix: add BACKEND_BUILD_TARGET used in docker compose file in .env.example to run in debug (#707)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: Update reviewers for renovate (#694)

Co-authored-by: Cristiam Da Silva <[email protected]>

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Edinaldo Pereira da Silva Junior <[email protected]>
Co-authored-by: Miguel Paya <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: Cristiam Da Silva <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>
cristiam86 added a commit that referenced this pull request Dec 10, 2024
* chore(deps): update dependency @vue/eslint-config-prettier to v10

* fix: adding Hardhat Dockerfile in GitHub Workflow (#693)

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

* 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]>

* fix: catch exception to keep loop running (#705)

Co-authored-by: Cristiam Da Silva <[email protected]>

* fix: add BACKEND_BUILD_TARGET used in docker compose file in .env.example to run in debug (#707)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: Update reviewers for renovate (#694)

Co-authored-by: Cristiam Da Silva <[email protected]>

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Edinaldo Pereira da Silva Junior <[email protected]>
Co-authored-by: Miguel Paya <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: Cristiam Da Silva <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>
cristiam86 added a commit that referenced this pull request Dec 10, 2024
…698)

* chore(deps): update dependency @vue/eslint-config-typescript to v14

* fix: adding Hardhat Dockerfile in GitHub Workflow (#693)

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

* 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]>

* fix: catch exception to keep loop running (#705)

Co-authored-by: Cristiam Da Silva <[email protected]>

* fix: add BACKEND_BUILD_TARGET used in docker compose file in .env.example to run in debug (#707)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: Update reviewers for renovate (#694)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore(deps): update dependency @vue/eslint-config-typescript to v14

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Edinaldo Pereira da Silva Junior <[email protected]>
Co-authored-by: Miguel Paya <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: Cristiam Da Silva <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>
Copy link
Contributor

🎉 This PR is included in version 0.29.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

cristiam86 added a commit that referenced this pull request Dec 11, 2024
* chore(deps): update dependency @vue/eslint-config-prettier to v10 (#697)

* chore(deps): update dependency @vue/eslint-config-prettier to v10

* fix: adding Hardhat Dockerfile in GitHub Workflow (#693)

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

* 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]>

* fix: catch exception to keep loop running (#705)

Co-authored-by: Cristiam Da Silva <[email protected]>

* fix: add BACKEND_BUILD_TARGET used in docker compose file in .env.example to run in debug (#707)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: Update reviewers for renovate (#694)

Co-authored-by: Cristiam Da Silva <[email protected]>

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Edinaldo Pereira da Silva Junior <[email protected]>
Co-authored-by: Miguel Paya <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: Cristiam Da Silva <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>

* chore(deps): update sonarsource/sonarcloud-github-action action to v4 (#696)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* chore(deps): update node.js to v22 (#695)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* chore(deps): update codecov/codecov-action action to v5 (#660)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* chore(deps): update dependency pre-commit to v4 (#557)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* chore(deps): update dependency @types/chai to v5 (#551)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* fix(deps): update all non-major dependencies (#534)

* fix(deps): update all non-major dependencies

* remove trailing whitespace

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Den <[email protected]>
Co-authored-by: Cristiam Da Silva <[email protected]>

* chore(deps): update dependency @vitejs/plugin-vue-jsx to v4 (#514)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: updated target branch to staging

* chore: update dependencies

* chore(deps): update dependency @vue/eslint-config-prettier to v10 (#716)

* chore(deps): update dependency @vue/eslint-config-prettier to v10

* fix: adding Hardhat Dockerfile in GitHub Workflow (#693)

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

* 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]>

* fix: catch exception to keep loop running (#705)

Co-authored-by: Cristiam Da Silva <[email protected]>

* fix: add BACKEND_BUILD_TARGET used in docker compose file in .env.example to run in debug (#707)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: Update reviewers for renovate (#694)

Co-authored-by: Cristiam Da Silva <[email protected]>

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Edinaldo Pereira da Silva Junior <[email protected]>
Co-authored-by: Miguel Paya <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: Cristiam Da Silva <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>

* chore(deps): update node.js to v22 (#717)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: updated frontend dependencies config

* fix: dependencies

* chore(deps): update dependency @vue/eslint-config-typescript to v14 (#698)

* chore(deps): update dependency @vue/eslint-config-typescript to v14

* fix: adding Hardhat Dockerfile in GitHub Workflow (#693)

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

* 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]>

* fix: catch exception to keep loop running (#705)

Co-authored-by: Cristiam Da Silva <[email protected]>

* fix: add BACKEND_BUILD_TARGET used in docker compose file in .env.example to run in debug (#707)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore: Update reviewers for renovate (#694)

Co-authored-by: Cristiam Da Silva <[email protected]>

* chore(deps): update dependency @vue/eslint-config-typescript to v14

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Edinaldo Pereira da Silva Junior <[email protected]>
Co-authored-by: Miguel Paya <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: Cristiam Da Silva <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>

* chore(deps): update codecov/codecov-action action to v5 (#719)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Cristiam Da Silva <[email protected]>

* fix: docker python images version

* fix: removed unused dependencies

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Edinaldo Pereira da Silva Junior <[email protected]>
Co-authored-by: Miguel Paya <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: kstroobants <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>
Co-authored-by: Den <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation released run-tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix hardhat network stability
3 participants