From cd764c279d6f4b42118f9fd1234eedc19f90978a Mon Sep 17 00:00:00 2001 From: aboudjem Date: Fri, 31 May 2024 21:36:36 +0400 Subject: [PATCH] chore: Update gas report script and add gas report comparison --- .github/workflows/gas_report.yml | 85 ++++++++++++++++++++++++++++ scripts/foundry/compareGasReports.js | 63 +++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 .github/workflows/gas_report.yml create mode 100644 scripts/foundry/compareGasReports.js diff --git a/.github/workflows/gas_report.yml b/.github/workflows/gas_report.yml new file mode 100644 index 00000000..da7238aa --- /dev/null +++ b/.github/workflows/gas_report.yml @@ -0,0 +1,85 @@ +name: Gas Report + +on: + pull_request: + branches: + - dev + - main + +jobs: + generate-gas-report: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch all history for git diff + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Cache Yarn dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cache/yarn + **/node_modules + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + + - name: Install dependencies + run: yarn install + + - name: Run gas report script + run: node generateGasReport.js + + - name: Upload gas report artifact + uses: actions/upload-artifact@v4 + with: + name: gas-report-${{ github.sha }} + path: GAS_REPORT.md + + compare-gas-report: + runs-on: ubuntu-latest + needs: generate-gas-report + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Download previous gas report + uses: actions/download-artifact@v4 + with: + name: gas-report-${{ github.event.pull_request.base.sha }} + path: previous_gas_report + + - name: Download current gas report + uses: actions/download-artifact@v4 + with: + name: gas-report-${{ github.sha }} + path: current_gas_report + + - name: Compare gas reports + run: | + node compareGasReports.js previous_gas_report/GAS_REPORT.md current_gas_report/GAS_REPORT.md > gas_report_diff.md + id: compare-gas-reports + + - name: Upload gas report comparison + uses: actions/upload-artifact@v4 + with: + name: gas-report-comparison-${{ github.sha }} + path: gas_report_diff.md + + - name: Create pull request comment + uses: marocchino/sticky-pull-request-comment@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + header: Gas Report Comparison + message: | + ```diff + ${{ steps.compare-gas-reports.outputs.gas_report_diff }} + ``` diff --git a/scripts/foundry/compareGasReports.js b/scripts/foundry/compareGasReports.js new file mode 100644 index 00000000..0b501984 --- /dev/null +++ b/scripts/foundry/compareGasReports.js @@ -0,0 +1,63 @@ +const fs = require('fs'); + +function compareGasReports(prevFile, currFile) { + const prevData = fs.readFileSync(prevFile, 'utf8'); + const currData = fs.readFileSync(currFile, 'utf8'); + + const prevLines = prevData.split('\n'); + const currLines = currData.split('\n'); + + const diffLines = ['# Gas Report Comparison', '| **Protocol** | **Actions / Function** | **Account Type** | **Is Deployed** | **With Paymaster?** | **Receiver Access** | **Gas Used** | **Gas Difference** |', '|:------------:|:---------------------:|:----------------:|:--------------:|:-------------------:|:-------------------:|:------------:|:------------------:|']; + + const prevResults = parseGasReport(prevLines); + const currResults = parseGasReport(currLines); + + currResults.forEach(curr => { + const prev = prevResults.find(prev => prev.NUMBER === curr.NUMBER); + let gasDiff = '0'; + let gasDiffEmoji = ''; + + if (prev) { + const diff = curr.GAS_USED - prev.GAS_USED; + gasDiff = diff.toString(); + gasDiffEmoji = diff > 0 ? '🥵' : (diff < 0 ? '🥳' : ''); + } + + diffLines.push(`| ${curr.PROTOCOL} | ${curr.ACTION_FUNCTION} | ${curr.ACCOUNT_TYPE} | ${curr.IS_DEPLOYED} | ${curr.WITH_PAYMASTER} | ${curr.RECEIVER_ACCESS} | ${curr.GAS_USED} | ${gasDiffEmoji} ${gasDiff} |`); + }); + + return diffLines.join('\n'); +} + +function parseGasReport(lines) { + const results = []; + for (const line of lines) { + if (line.startsWith('|')) { + const parts = line.split('|').map(part => part.trim()); + if (parts.length === 9 && !parts[0].includes('**')) { + results.push({ + PROTOCOL: parts[1], + ACTION_FUNCTION: parts[2], + ACCOUNT_TYPE: parts[3], + IS_DEPLOYED: parts[4], + WITH_PAYMASTER: parts[5], + RECEIVER_ACCESS: parts[6], + GAS_USED: parseInt(parts[7], 10), + NUMBER: parseInt(parts[7], 10) // Adjusted the assignment to ensure it has a unique identifier + }); + } + } + } + return results; +} + +if (process.argv.length !== 4) { + console.error('Usage: node compareGasReports.js '); + process.exit(1); +} + +const previousFile = process.argv[2]; +const currentFile = process.argv[3]; + +const comparison = compareGasReports(previousFile, currentFile); +console.log(comparison);