From 432abb2d021c431799b0ae3672dee18f3ccadbb3 Mon Sep 17 00:00:00 2001 From: Dan Oved Date: Thu, 9 Nov 2023 15:37:43 -0800 Subject: [PATCH] Cache foundry build for contracts (#338) Significantly speeds up builds by: * for each contracts package, caches its build based on the files and foundry config For better visibility, breaks out each contracts package into its own CI task For protocol-deployments, fork-tests have just become the standard tests We can see the speed up in-upstream prs: https://github.com/ourzora/zora-protocol/actions/runs/6817103631?pr=262 Screenshot 2023-11-09 at 12 36 14 PM --- .../actions/cache_foundry_build/action.yml | 29 ++++ .github/workflows/1155.yml | 21 +++ .github/workflows/build.yml | 28 ++++ .github/workflows/contracts.yml | 17 ++ .github/workflows/coverage.yml | 8 +- .github/workflows/foundry.yml | 150 ------------------ .github/workflows/protocol-deployments.yml | 16 ++ .github/workflows/protocol-rewards.yml | 21 +++ .github/workflows/test.yml | 30 ++++ packages/protocol-deployments/package.json | 3 +- 10 files changed, 170 insertions(+), 153 deletions(-) create mode 100644 .github/actions/cache_foundry_build/action.yml create mode 100644 .github/workflows/1155.yml create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/contracts.yml delete mode 100644 .github/workflows/foundry.yml create mode 100644 .github/workflows/protocol-deployments.yml create mode 100644 .github/workflows/protocol-rewards.yml create mode 100644 .github/workflows/test.yml diff --git a/.github/actions/cache_foundry_build/action.yml b/.github/actions/cache_foundry_build/action.yml new file mode 100644 index 000000000..e4aaf45ba --- /dev/null +++ b/.github/actions/cache_foundry_build/action.yml @@ -0,0 +1,29 @@ +name: 'Cache foundry build' +description: 'Caches foundry build by path' + +inputs: + package_folder: # id of input + description: 'contracts package folder' + required: true + foundry_profile: + description: 'foundry profile for caching' + +runs: + using: "composite" + + steps: + - name: Cache contracts build + uses: actions/cache@v3 + with: + # npm cache files are stored in out and cache + path: | + ${{ inputs.package_folder }}/cache + ${{ inputs.package_folder }}/out + # cache key is based on foundry config, contracts source tests, and scripts + key: ${{ inputs.package_folder }}${{ inputs.foundry_profile}}-${{ hashFiles(format('{0}/foundry.toml', inputs.package_folder)) }}-${{ hashFiles(format('{0}/src/**/*.sol', inputs.package_folder)) }}-${{ hashFiles(format('{0}/test/**/*.sol', inputs.package_folder)) }}-${{ hashFiles(format('{0}/script/**/*.sol', inputs.package_folder)) }} + # the following backups cache keys are searched, in case an exact match is not found + # see https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key + restore-keys: | + ${{ inputs.package_folder }}${{ inputs.foundry_profile}}-${{ hashFiles(format('{0}/foundry.toml', inputs.package_folder)) }}-${{ hashFiles(format('{0}/src/**/*.sol', inputs.package_folder)) }}-${{ hashFiles(format('{0}/test/**/*.sol', inputs.package_folder)) }}- + ${{ inputs.package_folder }}${{ inputs.foundry_profile}}-${{ hashFiles(format('{0}/foundry.toml', inputs.package_folder)) }}-${{ hashFiles(format('{0}/src/**/*.sol', inputs.package_folder)) }}- + ${{ inputs.package_folder }}${{ inputs.foundry_profile}}-${{ hashFiles(format('{0}/foundry.toml', inputs.package_folder)) }}- diff --git a/.github/workflows/1155.yml b/.github/workflows/1155.yml new file mode 100644 index 000000000..0eba44f0a --- /dev/null +++ b/.github/workflows/1155.yml @@ -0,0 +1,21 @@ +name: "1155" +on: + workflow_call: + +jobs: + build: + uses: ./.github/workflows/build.yml + with: + package_folder: packages/1155-contracts + + test: + needs: build + uses: ./.github/workflows/test.yml + with: + package_folder: packages/1155-contracts + + coverage: + uses: ./.github/workflows/coverage.yml + with: + package: "1155-contracts" + files_to_ignore: '"*DeploymentConfig*" "*Redeem*" "*deployment*" "*packages*"' diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 000000000..71a558d58 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,28 @@ +on: + workflow_call: + inputs: + package_folder: + required: true + type: string + +jobs: + check: + strategy: + fail-fast: true + + name: Build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Install node deps and founry + uses: ./.github/actions/setup_deps + + - name: Cache build + uses: ./.github/actions/cache_foundry_build + with: + package_folder: ${{ inputs.package_folder }} + + - name: Build contracts + run: | + cd ${{ inputs.package_folder}} && forge build diff --git a/.github/workflows/contracts.yml b/.github/workflows/contracts.yml new file mode 100644 index 000000000..ae500c2cd --- /dev/null +++ b/.github/workflows/contracts.yml @@ -0,0 +1,17 @@ +name: "Contracts" + +on: "push" + +jobs: + contracts-1155: + name: 1155 + uses: ./.github/workflows/1155.yml + + contracts-protocol-rewards: + name: Protocol Rewards + uses: ./.github/workflows/protocol-rewards.yml + + contracts-protocol-deployments: + name: Protocol Deployments + uses: ./.github/workflows/protocol-deployments.yml + secrets: inherit diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 966b7ba79..58e251a8c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -19,7 +19,7 @@ jobs: strategy: fail-fast: true - name: Check solidity test coverage + name: Coverage runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -29,6 +29,12 @@ jobs: - name: Install node deps and founry uses: ./.github/actions/setup_deps + - name: Cache build + uses: ./.github/actions/cache_foundry_build + with: + package_folder: packages/${{ inputs.package }} + foundry_profile: coverage + - name: Run Forge coverage run: cd $package_folder && forge coverage --report lcov diff --git a/.github/workflows/foundry.yml b/.github/workflows/foundry.yml deleted file mode 100644 index ad7a2eb47..000000000 --- a/.github/workflows/foundry.yml +++ /dev/null @@ -1,150 +0,0 @@ -name: "Foundry" - -on: "push" - -env: - # Setting an environment variable with the value of a configuration variable - $build_artifact: foundry_artifact - -jobs: - build: - name: Build contracts and cache output - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Install node deps and founry - uses: ./.github/actions/setup_deps - - - name: Build contracts - run: | - npx turbo run build:contracts - - - name: Archive built artifacts - uses: actions/upload-artifact@v3 - with: - name: $build_artifact - path: | - **/*/cache - **/*/out - retention-days: 1 - - contract_size_check: - needs: build - strategy: - fail-fast: true - - name: Check contract sizes - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Install node deps and founry - uses: ./.github/actions/setup_deps - - - name: Download foundry artifacts - uses: actions/download-artifact@v3 - with: - name: $build_artifact - - - name: Check contract sizes - run: | - npx turbo run build:sizes - - test: - needs: build - strategy: - fail-fast: true - - name: Tests - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Install node deps and founry - uses: ./.github/actions/setup_deps - - - name: Download foundry artifacts - uses: actions/download-artifact@v3 - with: - name: $build_artifact - - - name: Run tests - run: | - npx turbo run test - - test_fork: - needs: build - strategy: - fail-fast: true - - name: Fork tests - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Install node deps and founry - uses: ./.github/actions/setup_deps - - - name: Download foundry artifacts - uses: actions/download-artifact@v3 - with: - name: $build_artifact - - - name: Run fork tests - run: | - npx turbo run test:fork - env: - ALCHEMY_KEY: ${{ secrets.ALCHEMY_KEY }} - - storage_layout: - needs: build - strategy: - fail-fast: true - - name: Inpect storage layout - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Install node deps and founry - uses: ./.github/actions/setup_deps - - - name: Download foundry artifacts - uses: actions/download-artifact@v3 - with: - name: $build_artifact - - - name: "Inspect Storage Layout" - continue-on-error: false - run: npx turbo run storage-inspect:check - - build_js: - strategy: - fail-fast: true - - name: Build js package - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Install node deps and founry - uses: ./.github/actions/setup_deps - - - name: Build js package - run: | - npx turbo run build - - coverage-1155: - uses: ./.github/workflows/coverage.yml - name: "Test coverage - 1155" - with: - package: "1155-contracts" - files_to_ignore: '"*DeploymentConfig*" "*Redeem*" "*deployment*" "*packages*"' - - coverage-protocol-rewards: - uses: ./.github/workflows/coverage.yml - name: "Test coverage - protocol rewards" - with: - package: "protocol-rewards" - files_to_ignore: '"*lib*"' diff --git a/.github/workflows/protocol-deployments.yml b/.github/workflows/protocol-deployments.yml new file mode 100644 index 000000000..44ba3db37 --- /dev/null +++ b/.github/workflows/protocol-deployments.yml @@ -0,0 +1,16 @@ +name: "1155" +on: + workflow_call: + +jobs: + build: + uses: ./.github/workflows/build.yml + with: + package_folder: packages/protocol-deployments + + test: + needs: build + uses: ./.github/workflows/test.yml + with: + package_folder: packages/protocol-deployments + secrets: inherit diff --git a/.github/workflows/protocol-rewards.yml b/.github/workflows/protocol-rewards.yml new file mode 100644 index 000000000..2e30c8fae --- /dev/null +++ b/.github/workflows/protocol-rewards.yml @@ -0,0 +1,21 @@ +name: "Protocol Rewards" +on: + workflow_call: + +jobs: + build: + uses: ./.github/workflows/build.yml + with: + package_folder: packages/protocol-rewards + + test: + needs: build + uses: ./.github/workflows/test.yml + with: + package_folder: packages/protocol-rewards + + coverage: + uses: ./.github/workflows/coverage.yml + with: + package: "protocol-rewards" + files_to_ignore: '"*lib*"' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 000000000..90e2c7d7d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,30 @@ +on: + workflow_call: + inputs: + package_folder: + required: true + type: string + +jobs: + check: + strategy: + fail-fast: true + + name: Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Install node deps and founry + uses: ./.github/actions/setup_deps + + - name: Load foundry cache + uses: ./.github/actions/cache_foundry_build + with: + package_folder: ${{ inputs.package_folder }} + + - name: Test + run: | + cd ${{ inputs.package_folder}} && yarn test + env: + ALCHEMY_KEY: ${{ secrets.ALCHEMY_KEY }} diff --git a/packages/protocol-deployments/package.json b/packages/protocol-deployments/package.json index 57b5c6c0e..828c39f6c 100644 --- a/packages/protocol-deployments/package.json +++ b/packages/protocol-deployments/package.json @@ -8,8 +8,7 @@ "type": "module", "scripts": { "dev": "FOUNDRY_PROFILE=dev forge test --watch -vvv", - "test:fork": "FORK_TEST_CHAINS=mainnet,goerli,optimism,optimism_goerli,zora,zora_goerli,base_goerli,base,zora_sepolia forge test -vvv", - "test": "forge test -vvv", + "test": "FORK_TEST_CHAINS=mainnet,goerli,optimism,optimism_goerli,zora,zora_goerli,base_goerli,base,zora_sepolia forge test -vvv", "build": "yarn wagmi && yarn bundle-configs && tsup", "bundle-configs": "node script/bundle-chainConfigs.mjs && yarn prettier", "prettier": "prettier --write 'package/**/*.ts' 'script/**/*.ts' 'wagmi.config.ts'",